threads / patch / 66973

patch, 2 partsclone, fetch: reject a negative --jobs

Subject: [PATCH 0/2] clone, fetch: reject a negative --jobs

## tl;dr

Follow-up to a review comment by Junio C Hamano: "--jobs=-1" was silently dropped by clone, and fetch and pull treated any negative value as not given. Read the story.

replies: 4people: 2as markdown or json

Abhishek Agarwal· Dec 2, 2026, 17:13 UTC · lore

In the review of aa/submodule-update-jobs [1], Junio pointed out that "git clone --recurse-submodules --jobs=-1" is ignored without a word, because clone uses -1 to mean "not given" and does not pass it on, while "--jobs=-2" is passed on and only rejected by "submodule update" after the clone is done. He wondered if "fetch" has a similar issue. It does: "git fetch" and "git pull" treat any negative "--jobs" as if the option was not given.

These two patches make both commands reject a negative value while the command line is parsed, with the same message "submodule update" now gives. The series is built on top of aa/submodule-update-jobs, because the first patch adds its test next to the tests from that topic.

Two things I was not sure about:
 - Both patches add the same small callback. I kept a copy in each
   command so that the two stay independent, but I can move it to
   parse-options-cb.c if that is preferred.
 - "git hook run -j-1" uses -1 to mean "all CPUs" (495b7d54dc). I did
   not do that here, because the configuration variables clone and
   fetch fall back to (submodule.fetchJobs and fetch.parallel) reject
   negative values and use 0 for "all CPUs", and so does "submodule
   update --jobs" after the topic above.
[1] https://lore.kernel.org/git/xmqqtsl4zlyl.fsf@gitster.g/
Abhishek Agarwal (2):
  clone: reject a negative --jobs before cloning
  fetch: reject a negative --jobs
 builtin/clone.c             | 18 ++++++++++++++++--
 builtin/fetch.c             | 18 ++++++++++++++++--
 t/t5514-fetch-multiple.sh   |  8 ++++++++
 t/t7406-submodule-update.sh | 10 ++++++++++
 4 files changed, 50 insertions(+), 4 deletions(-)
base-commit: 5d6ad3960bf5d6e577b706d8c642429b504b065d
-- 
2.56.0
Abhishek Agarwal· Dec 2, 2026, 17:13 UTC · re: Abhishek Agarwal · lore

[PATCH 1/2] clone: reject a negative --jobs before cloning

"git clone --recurse-submodules --jobs=<n>" passes "--jobs=<n>" on to "git submodule update", but only when <n> is not -1, because max_jobs starts at -1 to mean that the option was not given. So "--jobs=-1" is dropped without a word and the submodules are fetched with the default number of jobs, while "--jobs=-2" is passed on and rejected by "submodule update". That happens only after the superproject has been cloned, so the user waits for the whole clone and is left with one whose submodules are not checked out.

Parse the option with a callback that rejects a negative value, like "git submodule update --jobs" and "submodule.fetchJobs" already do. The error now comes before anything is cloned, and -1 can again only mean that the option was not given. "--no-jobs" still means the same as "--jobs=0". A value that is not a number is still rejected, now with the "invalid value for" message that other options already use.

Reported-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Abhishek Agarwal <agab0323@gmail.com>
---
 builtin/clone.c             | 18 ++++++++++++++++--
 t/t7406-submodule-update.sh | 10 ++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)
Show changes to 2 files +26 −2

builtin/clone.c, t/t7406-submodule-update.sh

diff --git a/builtin/clone.c b/builtin/clone.c
index ac40250d25..f9ecbede1b 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -94,6 +94,19 @@ static int recurse_submodules_cb(const struct option *opt,
 	return 0;
 }
 
+static int parse_jobs_cb(const struct option *opt, const char *arg, int unset)
+{
+	int *jobs = opt->value;
+
+	if (unset)
+		*jobs = 0;
+	else if (!git_parse_int(arg, jobs))
+		return error(_("invalid value for '%s': '%s'"), "--jobs", arg);
+	else if (*jobs < 0)
+		return error(_("'%s' cannot be negative"), "--jobs");
+	return 0;
+}
+
 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
 {
 	static const char *suffix[] = { "/.git", "", ".git/.git", ".git" };
@@ -949,8 +962,9 @@ int cmd_clone(int argc,
 			.defval = (intptr_t)".",
 		},
 		OPT_ALIAS(0, "recursive", "recurse-submodules"),
-		OPT_INTEGER('j', "jobs", &max_jobs,
-			    N_("number of submodules cloned in parallel")),
+		OPT_CALLBACK('j', "jobs", &max_jobs, N_("n"),
+			     N_("number of submodules cloned in parallel"),
+			     parse_jobs_cb),
 		OPT_STRING(0, "template", &option_template, N_("template-directory"),
 			   N_("directory from which templates will be used")),
 		OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index 2b5551044a..7a440709aa 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -1116,6 +1116,16 @@ test_expect_success 'git clone --recurse-submodules --jobs 0 uses the number of
 	test_grep "up to $(test-tool online-cpus) tasks" trace-jobs.out
 '
 
+test_expect_success 'git clone --recurse-submodules rejects a negative --jobs' '
+	test_when_finished "rm -rf super6" &&
+	for jobs in -1 -2
+	do
+		test_must_fail git clone --recurse-submodules --jobs $jobs . super6 2>err &&
+		test_grep "${SQ}--jobs${SQ} cannot be negative" err &&
+		test_path_is_missing super6 || return 1
+	done
+'
+
 test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
 	(cd super &&
 	 test_commit -C rebasing message &&
-- 
2.56.0
Patrick Steinhardt· Dec 3, 2026, 09:56 UTC · re: Abhishek Agarwal · lore

Re: [PATCH 1/2] clone: reject a negative --jobs before cloning

On Wed, Dec 02, 2026 at 10:43:27PM +0530, Abhishek Agarwal wrote:
Show 33 quoted lines
> diff --git a/builtin/clone.c b/builtin/clone.c
> index ac40250d25..f9ecbede1b 100644
> --- a/builtin/clone.c
> +++ b/builtin/clone.c
> @@ -94,6 +94,19 @@ static int recurse_submodules_cb(const struct option *opt,
>  	return 0;
>  }
>  
> +static int parse_jobs_cb(const struct option *opt, const char *arg, int unset)
> +{
> +	int *jobs = opt->value;
> +
> +	if (unset)
> +		*jobs = 0;
> +	else if (!git_parse_int(arg, jobs))
> +		return error(_("invalid value for '%s': '%s'"), "--jobs", arg);
> +	else if (*jobs < 0)
> +		return error(_("'%s' cannot be negative"), "--jobs");
> +	return 0;
> +}
> +
>  static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
>  {
>  	static const char *suffix[] = { "/.git", "", ".git/.git", ".git" };
> @@ -949,8 +962,9 @@ int cmd_clone(int argc,
>  			.defval = (intptr_t)".",
>  		},
>  		OPT_ALIAS(0, "recursive", "recurse-submodules"),
> -		OPT_INTEGER('j', "jobs", &max_jobs,
> -			    N_("number of submodules cloned in parallel")),
> +		OPT_CALLBACK('j', "jobs", &max_jobs, N_("n"),
> +			     N_("number of submodules cloned in parallel"),
> +			     parse_jobs_cb),

Does this really warrant `OPT_CALLBACK()`? Can't we use `OPT_UNSIGNED()` instead?

Well, the answer is "not trivially" because of the sentinel value, which is "-1". But we could convert `max_jobs` to an unsigned value and use a sentinel value of `UINT_MAX`. I don't think anyone would ever configure a maximum job count of multiple billions of jobs, so that might be an alternative that requires less plumbing overall.

The status quo isn't all that bad either, though, so I'll leave it up to you to decide whether you want to take this suggestion.

Show 17 quoted lines
> diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
> index 2b5551044a..7a440709aa 100755
> --- a/t/t7406-submodule-update.sh
> +++ b/t/t7406-submodule-update.sh
> @@ -1116,6 +1116,16 @@ test_expect_success 'git clone --recurse-submodules --jobs 0 uses the number of
>  	test_grep "up to $(test-tool online-cpus) tasks" trace-jobs.out
>  '
>  
> +test_expect_success 'git clone --recurse-submodules rejects a negative --jobs' '
> +	test_when_finished "rm -rf super6" &&
> +	for jobs in -1 -2
> +	do
> +		test_must_fail git clone --recurse-submodules --jobs $jobs . super6 2>err &&
> +		test_grep "${SQ}--jobs${SQ} cannot be negative" err &&
> +		test_path_is_missing super6 || return 1
> +	done
> +'

Okay, and here you cover both of the different failure scenarios that you mention in the commit message. Makes sense.

Patrick
Abhishek Agarwal· Dec 2, 2026, 17:13 UTC · re: Abhishek Agarwal · lore

[PATCH 2/2] fetch: reject a negative --jobs

"git fetch" starts max_jobs at -1 to mean that "--jobs" was not given, and both places that use it, fetching from several remotes and fetching submodules, replace any negative value with "fetch.parallel" or "submodule.fetchJobs", or with one job when neither is set. So "--jobs=-1", "--jobs=-5" and the like are accepted without a word, even though both of those configuration variables reject a negative value. "git pull" passes "--jobs" on to "git fetch" and behaves the same way.

Parse the option with the same callback the previous commit added to "git clone", so that a negative value is rejected while the command line is parsed. "--no-jobs" still means the same as "--jobs=0".

Signed-off-by: Abhishek Agarwal <agab0323@gmail.com>
---
 builtin/fetch.c           | 18 ++++++++++++++++--
 t/t5514-fetch-multiple.sh |  8 ++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
Show changes to 2 files +24 −2

builtin/fetch.c, t/t5514-fetch-multiple.sh

diff --git a/builtin/fetch.c b/builtin/fetch.c
index 533fdfe7d8..12140f6a87 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -207,6 +207,19 @@ static int parse_refmap_arg(const struct option *opt, const char *arg, int unset
 	return 0;
 }
 
+static int parse_jobs_cb(const struct option *opt, const char *arg, int unset)
+{
+	int *jobs = opt->value;
+
+	if (unset)
+		*jobs = 0;
+	else if (!git_parse_int(arg, jobs))
+		return error(_("invalid value for '%s': '%s'"), "--jobs", arg);
+	else if (*jobs < 0)
+		return error(_("'%s' cannot be negative"), "--jobs");
+	return 0;
+}
+
 static void unlock_pack(unsigned int flags)
 {
 	if (gtransport)
@@ -2555,8 +2568,9 @@ int cmd_fetch(int argc,
 			    N_("fetch all tags and associated objects"), TAGS_SET),
 		OPT_SET_INT('n', NULL, &tags,
 			    N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
-		OPT_INTEGER('j', "jobs", &max_jobs,
-			    N_("number of submodules fetched in parallel")),
+		OPT_CALLBACK('j', "jobs", &max_jobs, N_("n"),
+			     N_("number of submodules fetched in parallel"),
+			     parse_jobs_cb),
 		OPT_BOOL(0, "prefetch", &prefetch,
 			 N_("modify the refspec to place all refs within refs/prefetch/")),
 		OPT_BOOL('p', "prune", &prune,
diff --git a/t/t5514-fetch-multiple.sh b/t/t5514-fetch-multiple.sh
index 5d79472c39..85c2da185c 100755
--- a/t/t5514-fetch-multiple.sh
+++ b/t/t5514-fetch-multiple.sh
@@ -227,6 +227,14 @@ test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
 	 git fetch --multiple --jobs=0)
 '
 
+test_expect_success 'git fetch rejects a negative --jobs' '
+	for jobs in -1 -2
+	do
+		test_must_fail git -C test fetch --multiple --jobs=$jobs 2>err &&
+		test_grep "${SQ}--jobs${SQ} cannot be negative" err || return 1
+	done
+'
+
 create_fetch_all_expect () {
 	cat >expect <<-\EOF
 	  one/HEAD -> one/main
-- 
2.56.0
Patrick Steinhardt· Dec 3, 2026, 09:56 UTC · re: Abhishek Agarwal · lore

Re: [PATCH 2/2] fetch: reject a negative --jobs

On Wed, Dec 02, 2026 at 10:43:28PM +0530, Abhishek Agarwal wrote:
Show 36 quoted lines
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index 533fdfe7d8..12140f6a87 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -207,6 +207,19 @@ static int parse_refmap_arg(const struct option *opt, const char *arg, int unset
>  	return 0;
>  }
>  
> +static int parse_jobs_cb(const struct option *opt, const char *arg, int unset)
> +{
> +	int *jobs = opt->value;
> +
> +	if (unset)
> +		*jobs = 0;
> +	else if (!git_parse_int(arg, jobs))
> +		return error(_("invalid value for '%s': '%s'"), "--jobs", arg);
> +	else if (*jobs < 0)
> +		return error(_("'%s' cannot be negative"), "--jobs");
> +	return 0;
> +}
> +
>  static void unlock_pack(unsigned int flags)
>  {
>  	if (gtransport)
> @@ -2555,8 +2568,9 @@ int cmd_fetch(int argc,
>  			    N_("fetch all tags and associated objects"), TAGS_SET),
>  		OPT_SET_INT('n', NULL, &tags,
>  			    N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
> -		OPT_INTEGER('j', "jobs", &max_jobs,
> -			    N_("number of submodules fetched in parallel")),
> +		OPT_CALLBACK('j', "jobs", &max_jobs, N_("n"),
> +			     N_("number of submodules fetched in parallel"),
> +			     parse_jobs_cb),
>  		OPT_BOOL(0, "prefetch", &prefetch,
>  			 N_("modify the refspec to place all refs within refs/prefetch/")),
>  		OPT_BOOL('p', "prune", &prune,

And this is essentially the equivalent change, just for git-fetch(1). The same suggestion as for git-clone(1) applies here of course.

Overall, these changes look good to me though, thanks!
Patrick

← back to recent threads