{"thread":{"id":"65800","subject":"[PATCH] clone: accept DEPTH env var as fallback for --depth","startedAt":"2026-06-13T01:39:42Z","lastAt":"2026-06-13T15:20:08Z","messageCount":3,"participants":["h8d13 via GitGitGadget","Matt Hunter","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"545427","messageId":"pull.2333.git.git.1781314780645.gitgitgadget@gmail.com","threadId":"65800","inReplyTo":null,"subject":"[PATCH] clone: accept DEPTH env var as fallback for --depth","fromName":"h8d13 via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-06-13T01:39:40Z","receivedAt":"2026-06-13T01:39:42Z","isPatch":true,"body":"From: h8d13 <hadean-eon-dev@proton.me>\n\nWhen git clone is run by a tool the user does not control directly\n(CI runners, package build scripts such as makepkg, or any wrapper\nthat spawns nested clones), there is no way to request a shallow\nclone: --depth only exists as a command-line option on the process\nthat invokes git clone, and unlike url.*.insteadOf there is no\nconfiguration key that could be injected via GIT_CONFIG_* to achieve\nthe same effect.\n\nTeach git clone to read a DEPTH environment variable when --depth is\nnot given on the command line. Since environment variables propagate\nto child processes, exporting DEPTH=1 once makes every nested clone\nunderneath shallow, which is useful in CI pipelines and recursive\nbuild tools. An explicit --depth on the command line still takes\nprecedence, and the value goes through the existing validation, so a\nnon-positive DEPTH dies with the same error as a non-positive\n--depth.\n\nSigned-off-by: h8d13 <hadean-eon-dev@proton.me>\n---\n    clone: accept DEPTH env var as fallback for --depth\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2333%2Fh8d13%2Fdepth-env-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2333/h8d13/depth-env-v1\nPull-Request: https://github.com/git/git/pull/2333\n\n builtin/clone.c | 6 ++++++\n 1 file changed, 6 insertions(+)\n\ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex d60d1b60bc..549506f672 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -1022,6 +1022,12 @@ int cmd_clone(int argc,\n \t\tusage_msg_opt(_(\"You must specify a repository to clone.\"),\n \t\t\tbuiltin_clone_usage, builtin_clone_options);\n \n+\tif (!option_depth) {\n+\t\tconst char *env_depth = getenv(\"DEPTH\");\n+\t\tif (env_depth && *env_depth)\n+\t\t\toption_depth = xstrdup(env_depth);\n+\t}\n+\n \tif (option_depth || option_since || option_not.nr)\n \t\tdeepen = 1;\n \tif (option_single_branch == -1)\n\nbase-commit: 3e65291872de10c3f0bf05ea8c24187e7a71ebf0\n-- \ngitgitgadget\n"},{"id":"545438","messageId":"DJ7MJMIFZR5N.2SG1RWB46WPQB@lfurio.us","threadId":"65800","inReplyTo":"pull.2333.git.git.1781314780645.gitgitgadget@gmail.com","subject":"Re: [PATCH] clone: accept DEPTH env var as fallback for --depth","fromName":"Matt Hunter","fromEmail":"m@lfurio.us","sentAt":"2026-06-13T04:08:02Z","receivedAt":"2026-06-13T04:08:03Z","isPatch":true,"body":"On Fri Jun 12, 2026 at 9:39 PM EDT, h8d13 via GitGitGadget wrote:\n> @@ -1022,6 +1022,12 @@ int cmd_clone(int argc,\n>  \t\tusage_msg_opt(_(\"You must specify a repository to clone.\"),\n>  \t\t\tbuiltin_clone_usage, builtin_clone_options);\n>  \n> +\tif (!option_depth) {\n> +\t\tconst char *env_depth = getenv(\"DEPTH\");\n\nNearly all of the non-standard environment variables used by git start\nwith \"GIT_\".  \"GIT_CLONE_DEPTH\" may be a better choice.\n\n> +\t\tif (env_depth && *env_depth)\n> +\t\t\toption_depth = xstrdup(env_depth);\n\nFollowing normal command-line option parsing, if --depth is given, then\noption_depth points to the parsed string from cmd_clone's argv array\ndirectly and is not freed.  Therefore, the string copy returned via\nxstrdup also goes unfreed before it is lost.\n\nOne might argue this isn't very impactful, since we would expect the\nprocess to exit after git-clone completes, but there are already several\nexplicit calls to free and related functions at the end of cmd_clone.\n> +\t}\n> +\n>  \tif (option_depth || option_since || option_not.nr)\n>  \t\tdeepen = 1;\n>  \tif (option_single_branch == -1)\n>\n> base-commit: 3e65291872de10c3f0bf05ea8c24187e7a71ebf0\n\n"},{"id":"545455","messageId":"xmqqeciafowq.fsf@gitster.g","threadId":"65800","inReplyTo":"DJ7MJMIFZR5N.2SG1RWB46WPQB@lfurio.us","subject":"Re: [PATCH] clone: accept DEPTH env var as fallback for --depth","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-13T15:20:05Z","receivedAt":"2026-06-13T15:20:08Z","isPatch":true,"body":"\"Matt Hunter\" <m@lfurio.us> writes:\n\n> On Fri Jun 12, 2026 at 9:39 PM EDT, h8d13 via GitGitGadget wrote:\n>> @@ -1022,6 +1022,12 @@ int cmd_clone(int argc,\n>>  \t\tusage_msg_opt(_(\"You must specify a repository to clone.\"),\n>>  \t\t\tbuiltin_clone_usage, builtin_clone_options);\n>>  \n>> +\tif (!option_depth) {\n>> +\t\tconst char *env_depth = getenv(\"DEPTH\");\n>\n> Nearly all of the non-standard environment variables used by git start\n> with \"GIT_\".  \"GIT_CLONE_DEPTH\" may be a better choice.\n\nIsn't it sufficient to add a new configuration variable in the\nclone.* namespace?  Unless there is a reason why it does not work, I\nwon't accept a patch that adds a random environment support like\nthis.  We do not want to end up having to add other random\nenvironment variables like GIT_CLONE_DEFAULTREMOTENAME,\nCLONE_REJECTSHALLOW, CLONE_FILTERSUBMODULES for consistency.\n"}]}