Merge pull request #7195 from weihanglo/insteadof

fix: apply insteadOf from global config for detached remotes
This commit is contained in:
Edward Thomson
2026-04-24 11:19:47 +01:00
committed by GitHub
2 changed files with 70 additions and 1 deletions

View File

@@ -236,6 +236,9 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
if (opts->repository) {
if ((error = git_repository_config_snapshot(&config_ro, opts->repository)) < 0)
goto on_error;
} else if (!(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
if ((error = git_config_open_default(&config_ro)) < 0)
goto on_error;
}
remote = git__calloc(1, sizeof(git_remote));
@@ -247,7 +250,7 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
(error = canonicalize_url(&canonical_url, url)) < 0)
goto on_error;
if (opts->repository && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
if (config_ro && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
if ((error = apply_insteadof(&remote->url, config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH, true)) < 0 ||
(error = apply_insteadof(&remote->pushurl, config_ro, canonical_url.ptr, GIT_DIRECTION_PUSH, false)) < 0)
goto on_error;

View File

@@ -152,3 +152,69 @@ void test_remote_insteadof__anonymous_remote_both(void)
git_remote_pushurl(g_remote),
"git@github.com:url/both/libgit2");
}
void test_remote_insteadof__detached_remote_fetch_insteadof(void)
{
git_config *cfg;
cl_fake_globalconfig(NULL);
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_set_string(cfg, "url.http://github.com/url/fetch.insteadOf",
"http://example.com/url/fetch"));
git_config_free(cfg);
cl_git_pass(git_remote_create_detached(&g_remote,
"http://example.com/url/fetch/libgit2"));
cl_assert_equal_s(
git_remote_url(g_remote),
"http://github.com/url/fetch/libgit2");
cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
}
void test_remote_insteadof__detached_remote_push_insteadof(void)
{
git_config *cfg;
cl_fake_globalconfig(NULL);
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_set_string(cfg, "url.git@github.com:url/push.pushInsteadOf",
"http://example.com/url/push"));
git_config_free(cfg);
cl_git_pass(git_remote_create_detached(&g_remote,
"http://example.com/url/push/libgit2"));
cl_assert_equal_s(
git_remote_url(g_remote),
"http://example.com/url/push/libgit2");
cl_assert_equal_s(
git_remote_pushurl(g_remote),
"git@github.com:url/push/libgit2");
}
void test_remote_insteadof__detached_remote_both_insteadof(void)
{
git_config *cfg;
cl_fake_globalconfig(NULL);
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_set_string(cfg, "url.http://github.com/url/both.insteadOf",
"http://example.com/url/both"));
cl_git_pass(git_config_set_string(cfg, "url.git@github.com:url/both.pushInsteadOf",
"http://example.com/url/both"));
git_config_free(cfg);
cl_git_pass(git_remote_create_detached(&g_remote,
"http://example.com/url/both/libgit2"));
cl_assert_equal_s(
git_remote_url(g_remote),
"http://github.com/url/both/libgit2");
cl_assert_equal_s(
git_remote_pushurl(g_remote),
"git@github.com:url/both/libgit2");
}