Volume XXII, number 280Wednesday, October 7, 2026Latest message 4 hours ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchclone: accept DEPTH env var as fallback for --depth

3 messages between Jun 13, 2026 and Jun 13, 2026, from h8d13 via GitGitGadget, Matt Hunter, Junio C Hamano.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

h8d13 via GitGitGadgetJun 13, 2026, 01:39 UTC on lore
From: h8d13 <hadean-eon-dev@proton.me>

When git clone is run by a tool the user does not control directly (CI runners, package build scripts such as makepkg, or any wrapper that spawns nested clones), there is no way to request a shallow clone: --depth only exists as a command-line option on the process that invokes git clone, and unlike url.*.insteadOf there is no configuration key that could be injected via GIT_CONFIG_* to achieve the same effect.

Teach git clone to read a DEPTH environment variable when --depth is not given on the command line. Since environment variables propagate to child processes, exporting DEPTH=1 once makes every nested clone underneath shallow, which is useful in CI pipelines and recursive build tools. An explicit --depth on the command line still takes precedence, and the value goes through the existing validation, so a non-positive DEPTH dies with the same error as a non-positive --depth.

Signed-off-by: h8d13 <hadean-eon-dev@proton.me>
---
    clone: accept DEPTH env var as fallback for --depth
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2333%2Fh8d13%2Fdepth-env-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2333/h8d13/depth-env-v1
Pull-Request: https://github.com/git/git/pull/2333
 builtin/clone.c | 6 ++++++
 1 file changed, 6 insertions(+)
Show changes to builtin/clone.c +6 −0
diff --git a/builtin/clone.c b/builtin/clone.c
index d60d1b60bc..549506f672 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -1022,6 +1022,12 @@ int cmd_clone(int argc,
 		usage_msg_opt(_("You must specify a repository to clone."),
 			builtin_clone_usage, builtin_clone_options);
 
+	if (!option_depth) {
+		const char *env_depth = getenv("DEPTH");
+		if (env_depth && *env_depth)
+			option_depth = xstrdup(env_depth);
+	}
+
 	if (option_depth || option_since || option_not.nr)
 		deepen = 1;
 	if (option_single_branch == -1)

base-commit: 3e65291872de10c3f0bf05ea8c24187e7a71ebf0
-- 
gitgitgadget
Matt HunterJun 13, 2026, 04:08 UTC in reply to h8d13 via GitGitGadget on lore

Re: [PATCH] clone: accept DEPTH env var as fallback for --depth

On Fri Jun 12, 2026 at 9:39 PM EDT, h8d13 via GitGitGadget wrote:
Show 6 quoted lines
> @@ -1022,6 +1022,12 @@ int cmd_clone(int argc,
>  		usage_msg_opt(_("You must specify a repository to clone."),
>  			builtin_clone_usage, builtin_clone_options);
>  
> +	if (!option_depth) {
> +		const char *env_depth = getenv("DEPTH");

Nearly all of the non-standard environment variables used by git start with "GIT_". "GIT_CLONE_DEPTH" may be a better choice.

> +		if (env_depth && *env_depth)
> +			option_depth = xstrdup(env_depth);

Following normal command-line option parsing, if --depth is given, then option_depth points to the parsed string from cmd_clone's argv array directly and is not freed. Therefore, the string copy returned via xstrdup also goes unfreed before it is lost.

One might argue this isn't very impactful, since we would expect the process to exit after git-clone completes, but there are already several explicit calls to free and related functions at the end of cmd_clone.

Show 7 quoted lines
> +	}
> +
>  	if (option_depth || option_since || option_not.nr)
>  		deepen = 1;
>  	if (option_single_branch == -1)
>
> base-commit: 3e65291872de10c3f0bf05ea8c24187e7a71ebf0
Junio C HamanoJun 13, 2026, 15:20 UTC in reply to Matt Hunter on lore

Re: [PATCH] clone: accept DEPTH env var as fallback for --depth

"Matt Hunter" <m@lfurio.us> writes:
Show 10 quoted lines
> On Fri Jun 12, 2026 at 9:39 PM EDT, h8d13 via GitGitGadget wrote:
>> @@ -1022,6 +1022,12 @@ int cmd_clone(int argc,
>>  		usage_msg_opt(_("You must specify a repository to clone."),
>>  			builtin_clone_usage, builtin_clone_options);
>>  
>> +	if (!option_depth) {
>> +		const char *env_depth = getenv("DEPTH");
>
> Nearly all of the non-standard environment variables used by git start
> with "GIT_".  "GIT_CLONE_DEPTH" may be a better choice.

Isn't it sufficient to add a new configuration variable in the clone.* namespace? Unless there is a reason why it does not work, I won't accept a patch that adds a random environment support like this. We do not want to end up having to add other random environment variables like GIT_CLONE_DEFAULTREMOTENAME, CLONE_REJECTSHALLOW, CLONE_FILTERSUBMODULES for consistency.

Back to recent threads