Re: [PATCH 1/2] clone: reject a negative --jobs before cloning
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Dec 3, 2026, 09:56 UTC
- Message-ID
- <axE8s0A8snwyvyX9@pks.im>
- In-Reply-To
- <20261202171328.805150-2-agab0323@gmail.com>
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