From: Abhishek Agarwal Date: Wed, 02 Dec 2026 17:13:28 GMT Subject: [PATCH 2/2] fetch: reject a negative --jobs Message-ID: <20261202171328.805150-3-agab0323@gmail.com> In-Reply-To: <20261202171328.805150-1-agab0323@gmail.com> "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 --- 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