Skip to content

Commit 2dbd1c8

Browse files
ccjmnegitster
authored andcommitted
submodule: resolve insteadOf aliases when matching remote
When ca62f52 (submodule: look up remotes by URL first, 2025-06-23) introduced a mechanism to identify which remote is to be used by a submodule, it compared the URL stored in the .gitmodules inventory to that of each available remote. The URLs of remotes are rewritten according to url.<base>.insteadOf, whereas those stored in the .gitmodules aren't. When such aliasing applies, no match can be made between the two corresponding sides, and the procedure degrades to its fallback logic electing either the only configured remote if there is only one, or "origin" otherwise. That behaviour is unfortunate when no remote is called "origin", because its last resort will have a submodule update command look for a non-existent remote-tracking reference and fail to proceed, instead of using the remote whose rewritten URL matches. Resolve the alias in the URL inventoried in .gitmodules before comparing it against those of the corresponding submodule's configured remotes. Signed-off-by: Éric NICOLAS <ccjmne@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9a0c470 commit 2dbd1c8

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

remote.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,17 +1821,25 @@ const char *repo_default_remote(struct repository *repo)
18211821

18221822
const char *repo_remote_from_url(struct repository *repo, const char *url)
18231823
{
1824+
char *rewritten_url;
1825+
const char *remote_name = NULL;
1826+
18241827
read_config(repo, 0);
1828+
if ((rewritten_url = alias_url(url, &repo->remote_state->rewrites)))
1829+
url = rewritten_url;
18251830

18261831
for (int i = 0; i < repo->remote_state->remotes_nr; i++) {
18271832
struct remote *remote = repo->remote_state->remotes[i];
18281833
if (!remote)
18291834
continue;
18301835

1831-
if (remote_has_url(remote, url))
1832-
return remote->name;
1836+
if (remote_has_url(remote, url)) {
1837+
remote_name = remote->name;
1838+
break;
1839+
}
18331840
}
1834-
return NULL;
1841+
free(rewritten_url);
1842+
return remote_name;
18351843
}
18361844

18371845
int branch_has_merge_config(struct branch *branch)

t/t7406-submodule-update.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ test_expect_success 'submodule update --remote should fetch upstream changes' '
256256
)
257257
'
258258

259+
test_expect_success 'submodule update --remote resolves URL rewrites' '
260+
test_config_global "url.$(pwd)/.insteadOf" local: &&
261+
mkdir alias-super alias-submodule &&
262+
(
263+
cd alias-submodule &&
264+
git init &&
265+
git commit --allow-empty --message "Initial commit"
266+
) &&
267+
(
268+
cd alias-super &&
269+
git init &&
270+
git submodule add local:alias-submodule submodule &&
271+
git submodule update --force &&
272+
git -C submodule remote rename origin upstream &&
273+
git -C submodule remote add fork user@host &&
274+
git submodule update --remote
275+
)
276+
'
277+
259278
test_expect_success 'submodule update --remote should fetch upstream changes with .' '
260279
(
261280
cd super &&

0 commit comments

Comments
 (0)