Skip to content

Commit d375ab7

Browse files
committed
fix(remote): apply insteadOf from global config for detached remotes
Detached remotes already read global/system config for http proxy settings, but did not apply url.*.insteadOf or url.*.pushInsteadOf rules. This inconsistency meant that `git_remote_create_detached` behaved differently from git's `ls-remote`, which was the primary use case for detached remotes. This fixes it by loading the default config when no repository is provided and apply insteadOf rules consistently. While this is a behavior change, it still respects `GIT_REMOTE_CREATE_SKIP_INSTEADOF`, meaning that user can restore the previous behavior with minimal effort Fixes #5469
1 parent aa5d988 commit d375ab7

2 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/libgit2/remote.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ int git_remote_create_with_opts(git_remote **out, const char *url, const git_rem
236236
if (opts->repository) {
237237
if ((error = git_repository_config_snapshot(&config_ro, opts->repository)) < 0)
238238
goto on_error;
239+
} else if (!(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
240+
if ((error = git_config_open_default(&config_ro)) < 0)
241+
goto on_error;
239242
}
240243

241244
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
247250
(error = canonicalize_url(&canonical_url, url)) < 0)
248251
goto on_error;
249252

250-
if (opts->repository && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
253+
if (config_ro && !(opts->flags & GIT_REMOTE_CREATE_SKIP_INSTEADOF)) {
251254
if ((error = apply_insteadof(&remote->url, config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH, true)) < 0 ||
252255
(error = apply_insteadof(&remote->pushurl, config_ro, canonical_url.ptr, GIT_DIRECTION_PUSH, false)) < 0)
253256
goto on_error;

tests/libgit2/remote/insteadof.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,9 @@ void test_remote_insteadof__detached_remote_fetch_insteadof(void)
167167
cl_git_pass(git_remote_create_detached(&g_remote,
168168
"http://example.com/url/fetch/libgit2"));
169169

170-
/*
171-
* TODO: this should be "http://github.com/url/fetch/libgit2" once
172-
* git_remote_create_detached applies insteadOf from global config.
173-
* See: https://github.com/libgit2/libgit2/issues/5469
174-
*/
175170
cl_assert_equal_s(
176171
git_remote_url(g_remote),
177-
"http://example.com/url/fetch/libgit2");
172+
"http://github.com/url/fetch/libgit2");
178173
cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
179174
}
180175

@@ -195,12 +190,9 @@ void test_remote_insteadof__detached_remote_push_insteadof(void)
195190
cl_assert_equal_s(
196191
git_remote_url(g_remote),
197192
"http://example.com/url/push/libgit2");
198-
/*
199-
* TODO: this should be "git@github.com:url/push/libgit2" once
200-
* git_remote_create_detached applies pushInsteadOf from global config.
201-
* See: https://github.com/libgit2/libgit2/issues/5469
202-
*/
203-
cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
193+
cl_assert_equal_s(
194+
git_remote_pushurl(g_remote),
195+
"git@github.com:url/push/libgit2");
204196
}
205197

206198
void test_remote_insteadof__detached_remote_both_insteadof(void)
@@ -219,13 +211,10 @@ void test_remote_insteadof__detached_remote_both_insteadof(void)
219211
cl_git_pass(git_remote_create_detached(&g_remote,
220212
"http://example.com/url/both/libgit2"));
221213

222-
/*
223-
* TODO: these should be the rewritten URLs once
224-
* git_remote_create_detached applies insteadOf from global config.
225-
* See: https://github.com/libgit2/libgit2/issues/5469
226-
*/
227214
cl_assert_equal_s(
228215
git_remote_url(g_remote),
229-
"http://example.com/url/both/libgit2");
230-
cl_assert_equal_p(git_remote_pushurl(g_remote), NULL);
216+
"http://github.com/url/both/libgit2");
217+
cl_assert_equal_s(
218+
git_remote_pushurl(g_remote),
219+
"git@github.com:url/both/libgit2");
231220
}

0 commit comments

Comments
 (0)