Skip to content

Commit bcf2fd2

Browse files
committed
Merge branch 'ng/submodule-default-remote' into seen
Instead of hardcoded 'origin', use the configured default remote when fetching from submodules. Ejected from 'seen' due to test flakyness. cf. <2e62dc94-b821-4815-8dd2-f806580d2027@ramsayjones.plus.com> * ng/submodule-default-remote: submodule: fetch missing objects from default remote
2 parents 8543489 + 3b5fb32 commit bcf2fd2

File tree

6 files changed

+330
-4
lines changed

6 files changed

+330
-4
lines changed

builtin/submodule--helper.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,43 @@ static int get_default_remote_submodule(const char *module_path, char **default_
113113
return 0;
114114
}
115115

116+
static int module_get_default_remote(int argc, const char **argv, const char *prefix,
117+
struct repository *repo UNUSED)
118+
{
119+
const char *path;
120+
char *resolved_path = NULL;
121+
char *default_remote = NULL;
122+
int code;
123+
struct option options[] = {
124+
OPT_END()
125+
};
126+
const char *const usage[] = {
127+
N_("git submodule--helper get-default-remote <path>"),
128+
NULL
129+
};
130+
131+
argc = parse_options(argc, argv, prefix, options, usage, 0);
132+
if (argc != 1)
133+
usage_with_options(usage, options);
134+
135+
path = argv[0];
136+
if (prefix && *prefix && !is_absolute_path(path)) {
137+
resolved_path = xstrfmt("%s%s", prefix, path);
138+
path = resolved_path;
139+
}
140+
141+
code = get_default_remote_submodule(path, &default_remote);
142+
if (code) {
143+
free(resolved_path);
144+
return code;
145+
}
146+
147+
printf("%s\n", default_remote);
148+
free(default_remote);
149+
free(resolved_path);
150+
return 0;
151+
}
152+
116153
/* the result should be freed by the caller. */
117154
static char *get_submodule_displaypath(const char *path, const char *prefix,
118155
const char *super_prefix)
@@ -3789,6 +3826,7 @@ int cmd_submodule__helper(int argc,
37893826
OPT_SUBCOMMAND("set-url", &fn, module_set_url),
37903827
OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
37913828
OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3829+
OPT_SUBCOMMAND("get-default-remote", &fn, module_get_default_remote),
37923830
OPT_END()
37933831
};
37943832
argc = parse_options(argc, argv, prefix, options, usage, 0);

submodule.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,8 @@ static int get_next_submodule(struct child_process *cp, struct strbuf *err,
17081708
if (spf->oid_fetch_tasks_nr) {
17091709
struct fetch_task *task =
17101710
spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1711+
struct child_process cp_remote = CHILD_PROCESS_INIT;
1712+
struct strbuf remote_name = STRBUF_INIT;
17111713
spf->oid_fetch_tasks_nr--;
17121714

17131715
child_process_init(cp);
@@ -1721,8 +1723,19 @@ static int get_next_submodule(struct child_process *cp, struct strbuf *err,
17211723
strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
17221724
spf->prefix, task->sub->path);
17231725

1724-
/* NEEDSWORK: have get_default_remote from submodule--helper */
1725-
strvec_push(&cp->args, "origin");
1726+
cp_remote.git_cmd = 1;
1727+
strvec_pushl(&cp_remote.args, "submodule--helper",
1728+
"get-default-remote", task->sub->path, NULL);
1729+
1730+
if (!capture_command(&cp_remote, &remote_name, 0)) {
1731+
strbuf_trim_trailing_newline(&remote_name);
1732+
strvec_push(&cp->args, remote_name.buf);
1733+
} else {
1734+
/* Fallback to "origin" if the helper fails */
1735+
strvec_push(&cp->args, "origin");
1736+
}
1737+
strbuf_release(&remote_name);
1738+
17261739
oid_array_for_each_unique(task->commits,
17271740
append_oid_to_argv, &cp->args);
17281741

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ integration_tests = [
902902
't7423-submodule-symlinks.sh',
903903
't7424-submodule-mixed-ref-formats.sh',
904904
't7425-submodule-gitdir-path-extension.sh',
905+
't7426-submodule-get-default-remote.sh',
905906
't7450-bad-git-dotfiles.sh',
906907
't7500-commit-template-squash-signoff.sh',
907908
't7501-commit-basic-functionality.sh',

t/t5526-fetch-submodules.sh

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,11 +834,18 @@ test_expect_success "fetch new submodule commits on-demand outside standard refs
834834
git commit -m "updated submodules outside of refs/heads" &&
835835
E=$(git rev-parse HEAD) &&
836836
git update-ref refs/changes/3 $E &&
837+
FETCH_TRACE="$(pwd)/trace.out" &&
838+
test_when_finished "rm -f \"$FETCH_TRACE\"" &&
837839
(
838840
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 &&
840843
git -C submodule cat-file -t $C &&
841844
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" &&
842849
git checkout --recurse-submodules FETCH_HEAD
843850
)
844851
'
@@ -929,6 +936,68 @@ test_expect_success 'fetch new submodule commit intermittently referenced by sup
929936
)
930937
'
931938

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+
9321001
add_commit_push () {
9331002
dir="$1" &&
9341003
msg="$2" &&

t/t5572-pull-submodule.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,26 @@ test_expect_success 'fetch submodule remote of different name from superproject'
257257
git -C a-submodule reset --hard HEAD^^ &&
258258
259259
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
261280
'
262281

263282
test_done
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)