Git development
 help / color / mirror / Atom feed
From: Abhishek Agarwal <agab0323@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>,
	Jacob Keller <jacob.keller@gmail.com>,
	Adrian Ratiu <adrian.ratiu@collabora.com>
Subject: [PATCH 2/2] fetch: reject a negative --jobs
Date: Wed,  2 Dec 2026 22:43:28 +0530	[thread overview]
Message-ID: <20261202171328.805150-3-agab0323@gmail.com> (raw)
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 <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


      parent reply	other threads:[~2026-12-02 17:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202171328.805150-3-agab0323@gmail.com \
    --to=agab0323@gmail.com \
    --cc=adrian.ratiu@collabora.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jacob.keller@gmail.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox