* [PATCH 0/2] clone, fetch: reject a negative --jobs @ 2026-12-02 17:13 Abhishek Agarwal 2026-12-02 17:13 ` [PATCH 1/2] clone: reject a negative --jobs before cloning Abhishek Agarwal 2026-12-02 17:13 ` [PATCH 2/2] fetch: reject a negative --jobs Abhishek Agarwal 0 siblings, 2 replies; 6+ messages in thread From: Abhishek Agarwal @ 2026-12-02 17:13 UTC (permalink / raw) To: git; +Cc: Junio C Hamano, Patrick Steinhardt, Jacob Keller, Adrian Ratiu 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2] clone: reject a negative --jobs before cloning 2026-12-02 17:13 [PATCH 0/2] clone, fetch: reject a negative --jobs Abhishek Agarwal @ 2026-12-02 17:13 ` Abhishek Agarwal 2026-12-03 9:56 ` Patrick Steinhardt 2026-12-02 17:13 ` [PATCH 2/2] fetch: reject a negative --jobs Abhishek Agarwal 1 sibling, 1 reply; 6+ messages in thread From: Abhishek Agarwal @ 2026-12-02 17:13 UTC (permalink / raw) To: git; +Cc: Junio C Hamano, Patrick Steinhardt, Jacob Keller, Adrian Ratiu "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(-) 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 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] clone: reject a negative --jobs before cloning 2026-12-02 17:13 ` [PATCH 1/2] clone: reject a negative --jobs before cloning Abhishek Agarwal @ 2026-12-03 9:56 ` Patrick Steinhardt 2026-12-05 20:52 ` Abhishek Agarwal 0 siblings, 1 reply; 6+ messages in thread From: Patrick Steinhardt @ 2026-12-03 9:56 UTC (permalink / raw) To: Abhishek Agarwal; +Cc: git, Junio C Hamano, Jacob Keller, Adrian Ratiu On Wed, Dec 02, 2026 at 10:43:27PM +0530, Abhishek Agarwal wrote: > 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. > 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] clone: reject a negative --jobs before cloning 2026-12-03 9:56 ` Patrick Steinhardt @ 2026-12-05 20:52 ` Abhishek Agarwal 0 siblings, 0 replies; 6+ messages in thread From: Abhishek Agarwal @ 2026-12-05 20:52 UTC (permalink / raw) To: Patrick Steinhardt; +Cc: git, Junio C Hamano, Jacob Keller, Adrian Ratiu On Thu, Dec 03, 2026 at 10:56:03AM +0100, Patrick Steinhardt wrote: > 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. Thanks for the review. I tried it. The catch is that the value still ends up in an int: fetch hands it to fetch_multiple() and fetch_submodules(), and clone passes it on to "submodule update", whose --jobs is still OPT_INTEGER(). So a value above INT_MAX gets past OPT_UNSIGNED() and goes wrong later: - "git fetch --jobs=3000000000" turns negative in max_children and is silently replaced by fetch.parallel again. If I compare max_jobs with UINT_MAX instead, it dies with "data too large to fit into virtual memory space". - "git clone --recurse-submodules --jobs=3000000000" clones the superproject first and is only rejected by "submodule update" afterwards. A range check after parse_options() in both commands fixes that, and the result is still a bit smaller than the callback. But it adds a message for the range check, and since OPT_UNSIGNED() sets PARSE_OPT_NONEG, "--no-jobs" (which OPT_INTEGER() parses as 0) would stop working unless I spell the option out by hand. The callback accepts the same values as before, except negative ones, and gives the same message as "submodule update --jobs", so I would prefer to keep it. If you or Junio prefer the unsigned version, I am happy to send a v2. The same goes for 2/2. Thanks, Abhishek ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2] fetch: reject a negative --jobs 2026-12-02 17:13 [PATCH 0/2] clone, fetch: reject a negative --jobs Abhishek Agarwal 2026-12-02 17:13 ` [PATCH 1/2] clone: reject a negative --jobs before cloning Abhishek Agarwal @ 2026-12-02 17:13 ` Abhishek Agarwal 2026-12-03 9:56 ` Patrick Steinhardt 1 sibling, 1 reply; 6+ messages in thread From: Abhishek Agarwal @ 2026-12-02 17:13 UTC (permalink / raw) To: git; +Cc: Junio C Hamano, Patrick Steinhardt, Jacob Keller, Adrian Ratiu "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(-) 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 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] fetch: reject a negative --jobs 2026-12-02 17:13 ` [PATCH 2/2] fetch: reject a negative --jobs Abhishek Agarwal @ 2026-12-03 9:56 ` Patrick Steinhardt 0 siblings, 0 replies; 6+ messages in thread From: Patrick Steinhardt @ 2026-12-03 9:56 UTC (permalink / raw) To: Abhishek Agarwal; +Cc: git, Junio C Hamano, Jacob Keller, Adrian Ratiu On Wed, Dec 02, 2026 at 10:43:28PM +0530, Abhishek Agarwal wrote: > 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-05 20:53 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-02 17:13 [PATCH 0/2] clone, fetch: reject a negative --jobs Abhishek Agarwal 2026-12-02 17:13 ` [PATCH 1/2] clone: reject a negative --jobs before cloning Abhishek Agarwal 2026-12-03 9:56 ` Patrick Steinhardt 2026-12-05 20:52 ` Abhishek Agarwal 2026-12-02 17:13 ` [PATCH 2/2] fetch: reject a negative --jobs Abhishek Agarwal 2026-12-03 9:56 ` Patrick Steinhardt
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox