submodule: fetch missing objects from default remote

When be76c21282 (fetch: ensure submodule objects fetched, 2018-12-06) added support for fetching a missing submodule object by id, it hardcoded the remote name as "origin" and deferred anything more complicated for a later patch. Implement the NEEDSWORK item to remove the hardcoded assumption by adding and using a submodule helper subcmd 'get-default-remote'. Fixing this lets 'git fetch --recurse-submodules' succeed when the fetched commit(s) in the superproject trigger a submodule fetch, and that submodule's default remote name is not "origin". Add non-"origin" remote tests to t5526-fetch-submodules.sh and t5572-pull-submodule.sh demonstrating this works as expected and add dedicated tests for get-default-remote. Signed-off-by: Nasser Grainawi <nasser.grainawi@oss.qualcomm.com> Reviewed-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nasser Grainawi committed Mar 3, 2026 at 15:40 UTC 3b5fb32da836f5aead1cef319bc3e0a9b975ea35
6 files changed +330 -4
builtin/submodule--helper.c
+38
@@ -112,6 +112,43 @@ static int get_default_remote_submodule(const char *module_path, char **default_
112 return 0;
113 }
114
115 +static int module_get_default_remote(int argc, const char **argv, const char *prefix,
116 + struct repository *repo UNUSED)
117 +{
118 + const char *path;
119 + char *resolved_path = NULL;
120 + char *default_remote = NULL;
121 + int code;
122 + struct option options[] = {
123 + OPT_END()
124 + };
125 + const char *const usage[] = {
126 + N_("git submodule--helper get-default-remote <path>"),
127 + NULL
128 + };
129 +
130 + argc = parse_options(argc, argv, prefix, options, usage, 0);
131 + if (argc != 1)
132 + usage_with_options(usage, options);
133 +
134 + path = argv[0];
135 + if (prefix && *prefix && !is_absolute_path(path)) {
136 + resolved_path = xstrfmt("%s%s", prefix, path);
137 + path = resolved_path;
138 + }
139 +
140 + code = get_default_remote_submodule(path, &default_remote);
141 + if (code) {
142 + free(resolved_path);
143 + return code;
144 + }
145 +
146 + printf("%s\n", default_remote);
147 + free(default_remote);
148 + free(resolved_path);
149 + return 0;
150 +}
151 +
152 /* the result should be freed by the caller. */
153 static char *get_submodule_displaypath(const char *path, const char *prefix,
154 const char *super_prefix)
@@ -3608,6 +3645,7 @@ int cmd_submodule__helper(int argc,
3645 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3646 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3647 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3648 + OPT_SUBCOMMAND("get-default-remote", &fn, module_get_default_remote),
3649 OPT_END()
3650 };
3651 argc = parse_options(argc, argv, prefix, options, usage, 0);
submodule.c
+15 -2
@@ -1706,6 +1706,8 @@ static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1706 if (spf->oid_fetch_tasks_nr) {
1707 struct fetch_task *task =
1708 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1709 + struct child_process cp_remote = CHILD_PROCESS_INIT;
1710 + struct strbuf remote_name = STRBUF_INIT;
1711 spf->oid_fetch_tasks_nr--;
1712
1713 child_process_init(cp);
@@ -1719,8 +1721,19 @@ static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1721 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1722 spf->prefix, task->sub->path);
1723
1722 - /* NEEDSWORK: have get_default_remote from submodule--helper */
1723 - strvec_push(&cp->args, "origin");
1724 + cp_remote.git_cmd = 1;
1725 + strvec_pushl(&cp_remote.args, "submodule--helper",
1726 + "get-default-remote", task->sub->path, NULL);
1727 +
1728 + if (!capture_command(&cp_remote, &remote_name, 0)) {
1729 + strbuf_trim_trailing_newline(&remote_name);
1730 + strvec_push(&cp->args, remote_name.buf);
1731 + } else {
1732 + /* Fallback to "origin" if the helper fails */
1733 + strvec_push(&cp->args, "origin");
1734 + }
1735 + strbuf_release(&remote_name);
1736 +
1737 oid_array_for_each_unique(task->commits,
1738 append_oid_to_argv, &cp->args);
1739
t/meson.build
+1
@@ -887,6 +887,7 @@ integration_tests = [
887 't7422-submodule-output.sh',
888 't7423-submodule-symlinks.sh',
889 't7424-submodule-mixed-ref-formats.sh',
890 + 't7426-submodule-get-default-remote.sh',
891 't7450-bad-git-dotfiles.sh',
892 't7500-commit-template-squash-signoff.sh',
893 't7501-commit-basic-functionality.sh',
t/t5526-fetch-submodules.sh
+70 -1
@@ -834,11 +834,18 @@ test_expect_success "fetch new submodule commits on-demand outside standard refs
834 git commit -m "updated submodules outside of refs/heads" &&
835 E=$(git rev-parse HEAD) &&
836 git update-ref refs/changes/3 $E &&
837 + FETCH_TRACE="$(pwd)/trace.out" &&
838 + test_when_finished "rm -f \"$FETCH_TRACE\"" &&
839 (
840 cd downstream &&
839 - git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
841 + GIT_TRACE="$FETCH_TRACE" git fetch --recurse-submodules origin \
842 + refs/changes/3:refs/heads/my_branch &&
843 git -C submodule cat-file -t $C &&
844 git -C sub1 cat-file -t $D &&
845 + test_grep "trace: built-in: git submodule--helper get-default-remote sub1" \
846 + "$FETCH_TRACE" &&
847 + test_grep "trace: built-in: git fetch .* --submodule-prefix=sub1/ origin" \
848 + "$FETCH_TRACE" &&
849 git checkout --recurse-submodules FETCH_HEAD
850 )
851 '
@@ -929,6 +936,68 @@ test_expect_success 'fetch new submodule commit intermittently referenced by sup
936 )
937 '
938
939 +test_expect_success 'fetch new submodule commits on-demand outside standard refspec with custom remote name' '
940 + # depends on the previous test for setup
941 +
942 + # Rename the remote in sub1 from "origin" to "custom_remote"
943 + git -C downstream/sub1 remote rename origin custom_remote &&
944 +
945 + # Create new commits in the original submodules
946 + C=$(git -C submodule commit-tree \
947 + -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
948 + git -C submodule update-ref refs/changes/custom1 $C &&
949 + git update-index --cacheinfo 160000 $C submodule &&
950 + test_tick &&
951 +
952 + D=$(git -C sub1 commit-tree \
953 + -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
954 + git -C sub1 update-ref refs/changes/custom2 $D &&
955 + git update-index --cacheinfo 160000 $D sub1 &&
956 +
957 + git commit \
958 + -m "updated submodules outside of refs/heads for custom remote" &&
959 + E=$(git rev-parse HEAD) &&
960 + git update-ref refs/changes/custom3 $E &&
961 + FETCH_TRACE="$(pwd)/trace.out" &&
962 + test_when_finished "rm -f \"$FETCH_TRACE\"" &&
963 + (
964 + cd downstream &&
965 + GIT_TRACE="$FETCH_TRACE" git fetch --recurse-submodules origin \
966 + refs/changes/custom3:refs/heads/my_other_branch &&
967 + git -C submodule cat-file -t $C &&
968 + git -C sub1 cat-file -t $D &&
969 + test_grep "trace: built-in: git submodule--helper get-default-remote sub1" \
970 + "$FETCH_TRACE" &&
971 + test_grep "trace: built-in: git fetch .* --submodule-prefix=sub1/ custom_remote $D" \
972 + "$FETCH_TRACE" &&
973 + git checkout --recurse-submodules FETCH_HEAD
974 + )
975 +'
976 +
977 +test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD from custom remote' '
978 + # depends on the previous test for setup
979 +
980 + C=$(git -C submodule commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
981 + git -C submodule update-ref refs/changes/custom4 $C &&
982 + git update-index --cacheinfo 160000 $C submodule &&
983 + test_tick &&
984 +
985 + D=$(git -C sub1 commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
986 + git -C sub1 update-ref refs/changes/custom5 $D &&
987 + git update-index --cacheinfo 160000 $D sub1 &&
988 +
989 + git commit -m "updated submodules outside of refs/heads" &&
990 + E=$(git rev-parse HEAD) &&
991 + git update-ref refs/changes/custom6 $E &&
992 + (
993 + cd downstream &&
994 + git fetch --recurse-submodules origin refs/changes/custom6 &&
995 + git -C submodule cat-file -t $C &&
996 + git -C sub1 cat-file -t $D &&
997 + git checkout --recurse-submodules FETCH_HEAD
998 + )
999 +'
1000 +
1001 add_commit_push () {
1002 dir="$1" &&
1003 msg="$2" &&
t/t5572-pull-submodule.sh
+20 -1
@@ -257,7 +257,26 @@ test_expect_success 'fetch submodule remote of different name from superproject'
257 git -C a-submodule reset --hard HEAD^^ &&
258
259 git -C child pull --no-recurse-submodules &&
260 - git -C child submodule update
260 + git -C child submodule update &&
261 + test_path_is_file child/a-submodule/moreecho.t
262 +'
263 +
264 +test_expect_success 'fetch non-origin submodule remote named different from superproject' '
265 + git -C child/a-submodule remote rename origin o2 &&
266 +
267 + # Create commit that is unreachable from current master branch
268 + # newmain is already reset in the previous test
269 + test_commit -C a-submodule echo_o2 &&
270 + test_commit -C a-submodule moreecho_o2 &&
271 + subc=$(git -C a-submodule rev-parse --short HEAD) &&
272 +
273 + git -C parent/a-submodule fetch &&
274 + git -C parent/a-submodule checkout "$subc" &&
275 + git -C parent commit -m "update submodule o2" a-submodule &&
276 + git -C a-submodule reset --hard HEAD^^ &&
277 +
278 + git -C child pull --recurse-submodules &&
279 + test_path_is_file child/a-submodule/moreecho_o2.t
280 '
281
282 test_done
t/t7426-submodule-get-default-remote.sh new
+186
@@ -0,0 +1,186 @@
1 +#!/bin/sh
2 +
3 +test_description='git submodule--helper get-default-remote'
4 +
5 +TEST_NO_CREATE_REPO=1
6 +. ./test-lib.sh
7 +
8 +test_expect_success 'setup' '
9 + git config --global protocol.file.allow always
10 +'
11 +
12 +test_expect_success 'setup repositories' '
13 + # Create a repository to be used as submodule
14 + git init sub &&
15 + test_commit --no-tag -C sub "initial commit in sub" file.txt "sub content" &&
16 +
17 + # Create main repository
18 + git init super &&
19 + (
20 + cd super &&
21 + mkdir subdir &&
22 + test_commit --no-tag -C subdir "initial commit in super" main.txt "super content" &&
23 + git submodule add ../sub subpath &&
24 + git commit -m "add submodule 'sub' at subpath"
25 + )
26 +'
27 +
28 +test_expect_success 'get-default-remote returns origin for initialized submodule' '
29 + (
30 + cd super &&
31 + git submodule update --init &&
32 + echo "origin" >expect &&
33 + git submodule--helper get-default-remote subpath >actual &&
34 + test_cmp expect actual
35 + )
36 +'
37 +
38 +test_expect_success 'get-default-remote works from subdirectory' '
39 + (
40 + cd super/subdir &&
41 + echo "origin" >expect &&
42 + git submodule--helper get-default-remote ../subpath >actual &&
43 + test_cmp expect actual
44 + )
45 +'
46 +
47 +test_expect_success 'get-default-remote fails with non-existent path' '
48 + (
49 + cd super &&
50 + test_must_fail git submodule--helper get-default-remote nonexistent 2>err &&
51 + test_grep "could not get a repository handle" err
52 + )
53 +'
54 +
55 +test_expect_success 'get-default-remote fails with non-submodule path' '
56 + (
57 + cd super &&
58 + test_must_fail git submodule--helper get-default-remote subdir 2>err &&
59 + test_grep "could not get a repository handle" err
60 + )
61 +'
62 +
63 +test_expect_success 'get-default-remote fails without path argument' '
64 + (
65 + cd super &&
66 + test_must_fail git submodule--helper get-default-remote 2>err &&
67 + test_grep "usage:" err
68 + )
69 +'
70 +
71 +test_expect_success 'get-default-remote fails with too many arguments' '
72 + (
73 + cd super &&
74 + test_must_fail git submodule--helper get-default-remote subpath subdir 2>err &&
75 + test_grep "usage:" err
76 + )
77 +'
78 +
79 +test_expect_success 'setup submodule with non-origin default remote name' '
80 + # Create another submodule path with a different remote name
81 + (
82 + cd super &&
83 + git submodule add ../sub upstream-subpath &&
84 + git commit -m "add second submodule in upstream-subpath" &&
85 + git submodule update --init upstream-subpath &&
86 +
87 + # Change the remote name in the submodule
88 + cd upstream-subpath &&
89 + git remote rename origin upstream
90 + )
91 +'
92 +
93 +test_expect_success 'get-default-remote returns non-origin remote name' '
94 + (
95 + cd super &&
96 + echo "upstream" >expect &&
97 + git submodule--helper get-default-remote upstream-subpath >actual &&
98 + test_cmp expect actual
99 + )
100 +'
101 +
102 +test_expect_success 'get-default-remote handles submodule with multiple remotes' '
103 + (
104 + cd super/subpath &&
105 + git remote add other-upstream ../../sub &&
106 + git remote add myfork ../../sub
107 + ) &&
108 +
109 + (
110 + cd super &&
111 + echo "origin" >expect &&
112 + git submodule--helper get-default-remote subpath >actual &&
113 + test_cmp expect actual
114 + )
115 +'
116 +
117 +test_expect_success 'get-default-remote handles submodule with multiple remotes and none are origin' '
118 + (
119 + cd super/upstream-subpath &&
120 + git remote add yet-another-upstream ../../sub &&
121 + git remote add yourfork ../../sub
122 + ) &&
123 +
124 + (
125 + cd super &&
126 + echo "upstream" >expect &&
127 + git submodule--helper get-default-remote upstream-subpath >actual &&
128 + test_cmp expect actual
129 + )
130 +'
131 +
132 +test_expect_success 'setup nested submodule with non-origin remote' '
133 + git init innersub &&
134 + test_commit --no-tag -C innersub "initial commit in innersub" inner.txt "innersub content" &&
135 +
136 + (
137 + cd sub &&
138 + git submodule add ../innersub innersubpath &&
139 + git commit -m "add nested submodule at innersubpath"
140 + ) &&
141 +
142 + (
143 + cd super/upstream-subpath &&
144 + git pull upstream &&
145 + git submodule update --init --recursive . &&
146 + (
147 + cd innersubpath &&
148 + git remote rename origin another_upstream
149 + )
150 + )
151 +'
152 +
153 +test_expect_success 'get-default-remote works with nested submodule' '
154 + (
155 + cd super &&
156 + echo "another_upstream" >expect &&
157 + git submodule--helper get-default-remote upstream-subpath/innersubpath >actual &&
158 + test_cmp expect actual
159 + )
160 +'
161 +
162 +test_expect_success 'get-default-remote works with submodule that has no remotes' '
163 + # Create a submodule directory manually without remotes
164 + (
165 + cd super &&
166 + git init no-remote-sub &&
167 + test_commit --no-tag -C no-remote-sub "local commit" local.txt "local content"
168 + ) &&
169 +
170 + # Add it as a submodule
171 + (
172 + cd super &&
173 + git submodule add ./no-remote-sub &&
174 + git commit -m "add local submodule 'no-remote-sub'"
175 + ) &&
176 +
177 + (
178 + cd super &&
179 + # Should fall back to "origin" remote name when no remotes exist
180 + echo "origin" >expect &&
181 + git submodule--helper get-default-remote no-remote-sub >actual &&
182 + test_cmp expect actual
183 + )
184 +'
185 +
186 +test_done