Skip to content

Commit 3b8cd88

Browse files
committed
Merge branch 'ng/submodule-default-remote' into seen
Instead of hardcoded 'origin', use the configured default remote when fetching from submodules. * ng/submodule-default-remote: SQUASH??? fixup SQUASH??? fixup submodule: fetch missing objects from default remote
2 parents 68a45c3 + 268eb57 commit 3b8cd88

File tree

6 files changed

+314
-4
lines changed

6 files changed

+314
-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
@@ -899,6 +899,7 @@ integration_tests = [
899899
't7423-submodule-symlinks.sh',
900900
't7424-submodule-mixed-ref-formats.sh',
901901
't7425-submodule-gitdir-path-extension.sh',
902+
't7426-submodule-get-default-remote.sh',
902903
't7450-bad-git-dotfiles.sh',
903904
't7500-commit-template-squash-signoff.sh',
904905
't7501-commit-basic-functionality.sh',

t/t5526-fetch-submodules.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,58 @@ test_expect_success 'fetch new submodule commit intermittently referenced by sup
929929
)
930930
'
931931

932+
test_expect_success 'fetch --recurse-submodules works with custom remote names' '
933+
# depends on the previous test for setup
934+
935+
# Rename the remote in sub1 from "origin" to "custom_remote"
936+
git -C downstream/sub1 remote rename origin custom_remote &&
937+
938+
# Create new commits in the original submodules
939+
C=$(git -C submodule commit-tree -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
940+
git -C submodule update-ref refs/changes/custom1 $C &&
941+
git update-index --cacheinfo 160000 $C submodule &&
942+
test_tick &&
943+
944+
D=$(git -C sub1 commit-tree -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
945+
git -C sub1 update-ref refs/changes/custom2 $D &&
946+
git update-index --cacheinfo 160000 $D sub1 &&
947+
948+
git commit -m "updated submodules outside of refs/heads for custom remote" &&
949+
E=$(git rev-parse HEAD) &&
950+
git update-ref refs/changes/custom3 $E &&
951+
(
952+
cd downstream &&
953+
git fetch --recurse-submodules origin refs/changes/custom3:refs/heads/my_other_branch &&
954+
git -C submodule cat-file -t $C &&
955+
git -C sub1 cat-file -t $D &&
956+
git checkout --recurse-submodules FETCH_HEAD
957+
)
958+
'
959+
960+
test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD from custom remote' '
961+
# depends on the previous test for setup
962+
963+
C=$(git -C submodule commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
964+
git -C submodule update-ref refs/changes/custom4 $C &&
965+
git update-index --cacheinfo 160000 $C submodule &&
966+
test_tick &&
967+
968+
D=$(git -C sub1 commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
969+
git -C sub1 update-ref refs/changes/custom5 $D &&
970+
git update-index --cacheinfo 160000 $D sub1 &&
971+
972+
git commit -m "updated submodules outside of refs/heads" &&
973+
E=$(git rev-parse HEAD) &&
974+
git update-ref refs/changes/custom6 $E &&
975+
(
976+
cd downstream &&
977+
git fetch --recurse-submodules origin refs/changes/custom6 &&
978+
git -C submodule cat-file -t $C &&
979+
git -C sub1 cat-file -t $D &&
980+
git checkout --recurse-submodules FETCH_HEAD
981+
)
982+
'
983+
932984
add_commit_push () {
933985
dir="$1" &&
934986
msg="$2" &&

t/t5572-pull-submodule.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ test_expect_success 'fetch submodule remote of different name from superproject'
246246
git -C child submodule update --init &&
247247
248248
# Needs to create unreachable commit from current master branch.
249-
git -C a-submodule checkout -b newmain HEAD^ &&
249+
git -C a-submodule tag anchorpoint HEAD &&
250+
git -C a-submodule checkout -b newmain anchorpoint^ &&
250251
test_commit -C a-submodule echo &&
251252
test_commit -C a-submodule moreecho &&
252253
subc=$(git -C a-submodule rev-parse --short HEAD) &&
@@ -257,7 +258,26 @@ test_expect_success 'fetch submodule remote of different name from superproject'
257258
git -C a-submodule reset --hard HEAD^^ &&
258259
259260
git -C child pull --no-recurse-submodules &&
260-
git -C child submodule update
261+
git -C child submodule update &&
262+
test_path_is_file child/a-submodule/moreecho.t
263+
'
264+
265+
test_expect_success 'fetch submodule remote of different non-origin name from superproject' '
266+
git -C child/a-submodule remote rename origin o2 &&
267+
268+
# Create commit that is unreachable from current master branch
269+
git -C a-submodule checkout -b newmain2 anchorpoint^ &&
270+
test_commit -C a-submodule echo_o2 &&
271+
test_commit -C a-submodule moreecho_o2 &&
272+
subc=$(git -C a-submodule rev-parse --short HEAD) &&
273+
274+
git -C parent/a-submodule fetch &&
275+
git -C parent/a-submodule checkout "$subc" &&
276+
git -C parent commit -m "update submodule o2" a-submodule &&
277+
git -C a-submodule reset --hard HEAD^^ &&
278+
279+
git -C child pull --recurse-submodules &&
280+
test_path_is_file child/a-submodule/moreecho_o2.t
261281
'
262282

263283
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)