Skip to content

Commit eb41276

Browse files
committed
Allow proxy options when connecting with a detached remote.
1 parent 7d195b9 commit eb41276

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/remote.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,21 @@ static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
888888
git_buf buf = GIT_BUF_INIT;
889889
git_net_url lookup_url = GIT_NET_URL_INIT;
890890
int error;
891+
int cleanup_config = 0;
891892

892-
if ((error = git_net_url_dup(&lookup_url, url)) < 0 ||
893-
(error = git_repository_config__weakptr(&cfg, remote->repo)) < 0)
893+
if ((error = git_net_url_dup(&lookup_url, url)) < 0)
894894
goto done;
895895

896+
if (remote->repo) {
897+
if ((error = git_repository_config__weakptr(&cfg, remote->repo)) < 0)
898+
goto done;
899+
} else {
900+
if ((error = git_config_open_default(&cfg)) < 0)
901+
goto done;
902+
903+
cleanup_config = 1;
904+
}
905+
896906
/* remote.<name>.proxy config setting */
897907
if (remote->name && remote->name[0]) {
898908
git_buf_clear(&buf);
@@ -922,6 +932,9 @@ static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
922932
error = lookup_config(out, cfg, "http.proxy");
923933

924934
done:
935+
if (cleanup_config)
936+
git_config_free(cfg);
937+
925938
git_buf_dispose(&buf);
926939
git_net_url_dispose(&lookup_url);
927940
return error;
@@ -971,7 +984,6 @@ int git_remote__http_proxy(char **out, git_remote *remote, git_net_url *url)
971984

972985
GIT_ASSERT_ARG(out);
973986
GIT_ASSERT_ARG(remote);
974-
GIT_ASSERT_ARG(remote->repo);
975987

976988
*out = NULL;
977989

tests/remote/httpproxy.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "clar_libgit2.h"
2-
#include "remote.h"
2+
#include "futils.h"
33
#include "net.h"
4+
#include "remote.h"
45

56
static git_repository *repo;
67
static git_net_url url = GIT_NET_URL_INIT;
@@ -105,6 +106,45 @@ void test_remote_httpproxy__config_empty_overrides(void)
105106
assert_config_match("remote.lg2.proxy", "");
106107
}
107108

109+
void assert_global_config_match(const char *config, const char *expected)
110+
{
111+
git_remote *remote;
112+
char *proxy;
113+
git_config* cfg;
114+
115+
if (config) {
116+
cl_git_pass(git_config_open_default(&cfg));
117+
git_config_set_string(cfg, config, expected);
118+
git_config_free(cfg);
119+
}
120+
121+
cl_git_pass(git_remote_create_detached(&remote, "https://github.com/libgit2/libgit2"));
122+
cl_git_pass(git_remote__http_proxy(&proxy, remote, &url));
123+
124+
if (expected)
125+
cl_assert_equal_s(proxy, expected);
126+
else
127+
cl_assert_equal_p(proxy, expected);
128+
129+
git_remote_free(remote);
130+
git__free(proxy);
131+
}
132+
133+
void test_remote_httpproxy__config_overrides_detached_remote(void)
134+
{
135+
cl_fake_home();
136+
137+
assert_global_config_match(NULL, NULL);
138+
assert_global_config_match("http.proxy", "http://localhost:1/");
139+
assert_global_config_match("http.https://github.com.proxy", "http://localhost:2/");
140+
assert_global_config_match("http.https://github.com/.proxy", "http://localhost:3/");
141+
assert_global_config_match("http.https://github.com/libgit2.proxy", "http://localhost:4/");
142+
assert_global_config_match("http.https://github.com/libgit2/.proxy", "http://localhost:5/");
143+
assert_global_config_match("http.https://github.com/libgit2/libgit2.proxy", "http://localhost:6/");
144+
145+
cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
146+
}
147+
108148
void test_remote_httpproxy__env(void)
109149
{
110150
orig_http_proxy = cl_getenv("HTTP_PROXY");

0 commit comments

Comments
 (0)