{"thread":{"id":"66516","subject":"[PATCH] fetch: fix --depth= and --unshallow for multi-remote fetches","startedAt":"2026-10-13T14:44:39Z","lastAt":"2026-10-16T10:26:39Z","messageCount":3,"participants":["Rong Bao","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"554870","messageId":"20261013144441.1632978-1-rong.bao@csmantle.top","threadId":"66516","inReplyTo":null,"subject":"[PATCH] fetch: fix --depth= and --unshallow for multi-remote fetches","fromName":"Rong Bao","fromEmail":"rong.bao@csmantle.top","sentAt":"2026-10-13T14:44:39Z","receivedAt":"2026-10-13T14:44:39Z","isPatch":true,"sender":{"key":"rong.bao@csmantle.top","avatar":null},"body":"When running `git fetch --all --depth=<SOME-DEPTH>` or `git fetch --all\n--unshallow` with Git 2.56.0, the fetch isn't really deepened:\n\n        $ git init remote\n        Initialized empty Git repository in /tmp/test/remote/.git/\n        $ for i in 1 2 3; do echo \"$i\" >remote/\"$i\" && git -C remote add -A && git -C remote commit -m \"$i\"; done\n        [... snip ...]\n        $ git clone --depth 1 --no-local remote local\n        Cloning into 'local'...\n        remote: Enumerating objects: 5, done.\n        remote: Counting objects: 100% (5/5), done.\n        remote: Compressing objects: 100% (2/2), done.\n        remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)\n        Receiving objects: 100% (5/5), done.\n        $ git -C local log --format='%s'\n        3\n        $ git -C local remote add origin2 ../remote\n        $ git -C local fetch --all --unshallow\n        Fetching origin\n        Fetching origin2\n        From ../remote\n         * [new branch]      main       -> origin2/main\n        $ git -C local log --format='%s'\n        3\n        $ git -C local fetch --all --depth=3\n        Fetching origin\n        Fetching origin2\n        $ git -C local log --format='%s'\n        3\n\nThis is because the parent process doesn't pass the --depth= flag to the\nfetcher subprocess.\n\n        [pid 1622822] execve(\"/usr/lib/git-core/git\", [\"/usr/lib/git-core/git\",\n        \"-c\", \"fetch.bundleURI=\", \"fetch\", \"--append\", \"--no-auto-gc\",\n        \"--no-write-commit-graph\", \"origin\"], 0x5555c4b65ae0 /* 54 vars */) = 0\n\nFix this by passing it when specified or implied by --unshallow. After\nfixing, both flags would work on shallow trees in conjunction with\n--all, as demonstrated below:\n\n        > ~/workspace/git/build/git -C local fetch --all --unshallow\n        Fetching origin\n        remote: Enumerating objects: 5, done.\n        remote: Counting objects: 100% (5/5), done.\n        remote: Compressing objects: 100% (3/3), done.\n        remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)\n        Unpacking objects: 100% (4/4), 345 bytes | 345.00 KiB/s, done.\n        Fetching origin2\n        remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)\n        > ~/workspace/git/build/git -C local log --format='%s'\n        3\n        2\n        1\n\nSigned-off-by: Rong Bao <rong.bao@csmantle.top>\n---\n builtin/fetch.c           |  2 ++\n t/t5514-fetch-multiple.sh | 34 ++++++++++++++++++++++++++++++++++\n 2 files changed, 36 insertions(+)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex b2decc6cfdb8ae86b8b1e92caa85f4961ffc47ff..a110a2dcf09c7e6764eb232bff73286753b27009 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -2247,6 +2247,8 @@ static void add_options_to_argv(struct strvec *argv,\n \t\tstrvec_push(argv, \"--tags\");\n \telse if (tags == TAGS_UNSET)\n \t\tstrvec_push(argv, \"--no-tags\");\n+\tif (deepen)\n+\t\tstrvec_pushf(argv, \"--depth=%s\", depth);\n \tif (verbosity >= 2)\n \t\tstrvec_push(argv, \"-v\");\n \tif (verbosity >= 1)\ndiff --git a/t/t5514-fetch-multiple.sh b/t/t5514-fetch-multiple.sh\nindex 5d79472c39f67126079da5432938c6d97f08008a..b6568d33d7a6dd725a72584fbad942d6b3f7c39b 100755\n--- a/t/t5514-fetch-multiple.sh\n+++ b/t/t5514-fetch-multiple.sh\n@@ -383,4 +383,38 @@ test_expect_success 'git fetch --no-all --all (fetch all remotes)' '\n \t)\n '\n \n+test_expect_success 'git fetch --all --depth=2 (fetch all remotes with depth 2)' '\n+\tsetup_repository test16 &&\n+\tgit clone --no-local --no-single-branch --depth=1 test16 test17 &&\n+\t(\n+\t\tcd test17 &&\n+\t\tgit remote add remote2 ../test16 &&\n+\t\ttest_path_is_file .git/shallow &&\n+\t\tgit fetch --all --depth=2 &&\n+\t\ttest_path_is_missing .git/shallow &&\n+\t\tgit log --format=%s origin/side >actual1 &&\n+\t\tgit log --format=%s remote2/side >actual2 &&\n+\t\ttest_write_lines Second Initial >expect &&\n+\t\ttest_cmp expect actual1 &&\n+\t\ttest_cmp expect actual2\n+\t)\n+'\n+\n+test_expect_success 'git fetch --all --unshallow (fetch all remotes with full history)' '\n+\tsetup_repository test18 &&\n+\tgit clone --no-local --no-single-branch --depth=1 test18 test19 &&\n+\t(\n+\t\tcd test19 &&\n+\t\tgit remote add remote2 ../test18 &&\n+\t\ttest_path_is_file .git/shallow &&\n+\t\tgit fetch --all --unshallow &&\n+\t\ttest_path_is_missing .git/shallow &&\n+\t\tgit log --format=%s origin/side >actual1 &&\n+\t\tgit log --format=%s remote2/side >actual2 &&\n+\t\ttest_write_lines Second Initial >expect &&\n+\t\ttest_cmp expect actual1 &&\n+\t\ttest_cmp expect actual2\n+\t)\n+'\n+\n test_done\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n-- \n2.56.0\n\n\n"},{"id":"555190","messageId":"xmqq1p9qn33f.fsf@gitster.g","threadId":"66516","inReplyTo":"20261013144441.1632978-1-rong.bao@csmantle.top","subject":"Re: [PATCH] fetch: fix --depth= and --unshallow for multi-remote fetches","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-15T20:03:32Z","receivedAt":"2026-10-15T20:03:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Rong Bao <rong.bao@csmantle.top> writes:\n\n> When running `git fetch --all --depth=<SOME-DEPTH>` or `git fetch --all\n> --unshallow` with Git 2.56.0, the fetch isn't really deepened:\n> ...\n>\n> This is because the parent process doesn't pass the --depth= flag to the\n> fetcher subprocess.\n> ...\n>\n> Fix this by passing it when specified or implied by --unshallow. After\n> fixing, both flags would work on shallow trees in conjunction with\n> --all, as demonstrated below:\n\nThe above observes that \"--depth=\" given to the outer \"fetch --all\"\nis not propagated down to subfetches.\n\n> diff --git a/builtin/fetch.c b/builtin/fetch.c\n> index b2decc6cfdb8ae86b8b1e92caa85f4961ffc47ff..a110a2dcf09c7e6764eb232bff73286753b27009 100644\n> --- a/builtin/fetch.c\n> +++ b/builtin/fetch.c\n> @@ -2247,6 +2247,8 @@ static void add_options_to_argv(struct strvec *argv,\n\nThis function is used to prepare a command line to run a single\nfetch in a subprocess for 'fetch --all', and also to fetch\nsubmodules when '--recurse-submodules' is in effect.  Does the\nlatter case share the same problem that '--depth=<N>' is not\npropagated down?  Should the depth of submodules be controlled\nindependently by not passing '--depth=<N>' down in the first place,\nin which case, this patch may be a regression for users who depend\non the behavior?\n\n>  \t\tstrvec_push(argv, \"--tags\");\n>  \telse if (tags == TAGS_UNSET)\n>  \t\tstrvec_push(argv, \"--no-tags\");\n> +\tif (deepen)\n> +\t\tstrvec_pushf(argv, \"--depth=%s\", depth);\n\nThe 'deepen' variable checked here is set to true when any of\nthe '--depth=<N>', '--deepen-since=<TIME>', or\n'--shallow-exclude=<REF>' were used.  If the outer\n'fetch --all' was told to deepen with '--deepen-since=<TIME>',\nwithout using '--depth=<N>', what do we have in the variable\n'depth' at this point?  NULL?\n\nIn other words, shouldn't this be written in a more obvious,\nsimple, and straightforward way as three separate additions,\ne.g.,\n\n\tif (depth)\n\t\tstrvec_pushf(argv, \"--depth=%s\", depth);\n\tif (deepen_since)\n\t\tstrvec_pushf(argv, \"--shallow-since=%s\", deepen_since);\n\tif (deepen_not.nr) {\n\t\tstruct string_list_item *it;\n\t\tfor_each_string_list_item(it, &deepen_not) {\n                \tstrvec_pushf(argv, \"--shallow-since=%s\", it->string);\n\t\t}\n\t}\n\nPropagating 'shallow-exclude' would probably not be a good\nidea if this helper function is called within the context of\nrecursing into submodules, as their history and refs have no\nconnection to the history of the top-level project.  Please do\nnot blindly copy and paste the above to an updated patch.\n\n\n"},{"id":"555231","messageId":"e4c6ed6c-a9be-41b9-98e3-2882b6b44102@csmantle.top","threadId":"66516","inReplyTo":"xmqq1p9qn33f.fsf@gitster.g","subject":"Re: [PATCH] fetch: fix --depth= and --unshallow for multi-remote fetches","fromName":"Rong Bao","fromEmail":"rong.bao@csmantle.top","sentAt":"2026-10-16T10:26:39Z","receivedAt":"2026-10-16T10:26:39Z","isPatch":true,"sender":{"key":"rong.bao@csmantle.top","avatar":null},"body":"Hi Junio,\n\nOn 2026-10-16 04:03, Junio C Hamano wrote:\n> Rong Bao <rong.bao@csmantle.top> writes:\n> >> When running `git fetch --all --depth=<SOME-DEPTH>` or `git fetch --all\n>> --unshallow` with Git 2.56.0, the fetch isn't really deepened:\n>> ...\n>>\n>> This is because the parent process doesn't pass the --depth= flag to the\n>> fetcher subprocess.\n>> ...\n>>\n>> Fix this by passing it when specified or implied by --unshallow. After\n>> fixing, both flags would work on shallow trees in conjunction with\n>> --all, as demonstrated below:\n> > The above observes that \"--depth=\" given to the outer \"fetch --all\"\n> is not propagated down to subfetches.\n\nThat's correct and succinct.\n\n>> diff --git a/builtin/fetch.c b/builtin/fetch.c\n>> index b2decc6cfdb8ae86b8b1e92caa85f4961ffc47ff..a110a2dcf09c7e6764eb232bff73286753b27009 100644\n>> --- a/builtin/fetch.c\n>> +++ b/builtin/fetch.c\n>> @@ -2247,6 +2247,8 @@ static void add_options_to_argv(struct strvec *argv,\n> > This function is used to prepare a command line to run a single\n> fetch in a subprocess for 'fetch --all', and also to fetch\n> submodules when '--recurse-submodules' is in effect.  Does the\n> latter case share the same problem that '--depth=<N>' is not\n> propagated down?  Should the depth of submodules be controlled\n> independently by not passing '--depth=<N>' down in the first place,\n> in which case, this patch may be a regression for users who depend\n> on the behavior?\n\nThis *would* change the existing behavior when --recurse-submodules, but I beg to differ on the \"regression\" part. We have [0] which says:\n\n\t--recurse-submodules[=(yes|on-demand|no)]\n\t\tControl if and under what conditions new commits of submodules should be fetched too. [...]\n\nFrom the documented behavior, I don't see any specifics mentioning that options like --depth= should not apply to recursive submodule fetches. Besides, we're already passing other fetch options like --[no-]tags [1] to recursive submodule fetches, which dates back to 85566460897e (\"fetch --all: pass --tags/--no-tags through to each remote\") and also has an impact to the local submodule tree similar to --depth=.\n\nAnd, personally, I would read commands like \"git fetch --unshallow --recurse-submodules\" as \"unshallow the tree, including all submodules\".\n\n>>   \t\tstrvec_push(argv, \"--tags\");\n>>   \telse if (tags == TAGS_UNSET)\n>>   \t\tstrvec_push(argv, \"--no-tags\");\n>> +\tif (deepen)\n>> +\t\tstrvec_pushf(argv, \"--depth=%s\", depth);\n> > The 'deepen' variable checked here is set to true when any of\n> the '--depth=<N>', '--deepen-since=<TIME>', or\n> '--shallow-exclude=<REF>' were used.  If the outer\n> 'fetch --all' was told to deepen with '--deepen-since=<TIME>',\n> without using '--depth=<N>', what do we have in the variable\n> 'depth' at this point?  NULL?\n> > In other words, shouldn't this be written in a more obvious,\n> simple, and straightforward way as three separate additions,\n> e.g.,\n> > \tif (depth)\n> \t\tstrvec_pushf(argv, \"--depth=%s\", depth);\n> \tif (deepen_since)\n> \t\tstrvec_pushf(argv, \"--shallow-since=%s\", deepen_since);\n> \tif (deepen_not.nr) {\n> \t\tstruct string_list_item *it;\n> \t\tfor_each_string_list_item(it, &deepen_not) {\n>                  \tstrvec_pushf(argv, \"--shallow-since=%s\", it->string);\n> \t\t}\n> \t}\n\nI will revise in v2, and ...\n\n> Propagating 'shallow-exclude' would probably not be a good\n> idea if this helper function is called within the context of\n> recursing into submodules, as their history and refs have no\n> connection to the history of the top-level project.  Please do\n> not blindly copy and paste the above to an updated patch.\n\n... I think I'll choose to limit the scope of this patch to --depth= and --shallow-since=, since the former is what I'm originally proposing to fix, and the latter looks minimal enough to implement.\n\n\n[0]: https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---recurse-submodulesyeson-demandno\n[1]: https://github.com/git/git/blob/f57c9a73884400380bddd172acc88b569834f909/builtin/fetch.c#L2246-L2249\n\n-- \nRegards,\nRong Bao\n\n"}]}