Skip to content

Commit 0644c2e

Browse files
authored
Merge pull request libgit2#6058 from mathworks/proxy_config_with_detached_remote
Allow proxy options when connecting with a detached remote.
2 parents 4f5653a + 3bd462a commit 0644c2e

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/remote.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -884,15 +884,22 @@ static void url_config_trim(git_net_url *url)
884884

885885
static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
886886
{
887-
git_config *cfg;
887+
git_config *cfg = NULL;
888888
git_buf buf = GIT_BUF_INIT;
889889
git_net_url lookup_url = GIT_NET_URL_INIT;
890890
int error;
891891

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

895+
if (remote->repo) {
896+
if ((error = git_repository_config(&cfg, remote->repo)) < 0)
897+
goto done;
898+
} else {
899+
if ((error = git_config_open_default(&cfg)) < 0)
900+
goto done;
901+
}
902+
896903
/* remote.<name>.proxy config setting */
897904
if (remote->name && remote->name[0]) {
898905
git_buf_clear(&buf);
@@ -922,6 +929,7 @@ static int http_proxy_config(char **out, git_remote *remote, git_net_url *url)
922929
error = lookup_config(out, cfg, "http.proxy");
923930

924931
done:
932+
git_config_free(cfg);
925933
git_buf_dispose(&buf);
926934
git_net_url_dispose(&lookup_url);
927935
return error;
@@ -971,7 +979,6 @@ int git_remote__http_proxy(char **out, git_remote *remote, git_net_url *url)
971979

972980
GIT_ASSERT_ARG(out);
973981
GIT_ASSERT_ARG(remote);
974-
GIT_ASSERT_ARG(remote->repo);
975982

976983
*out = NULL;
977984

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)