credential: make relevance of http path configurable

When parsing a URL into a credential struct, we carefully record each part of the URL, including the path on the remote host, and use the result as part of the credential context. This had two practical implications: 1. Credential helpers which store a credential for later access are likely to use the "path" portion as part of the storage key. That means that a request to https://example.com/foo.git would not use the same credential that was stored in an earlier request for: https://example.com/bar.git 2. The prompt shown to the user includes all relevant context, including the path. In most cases, however, users will have a single password per host. The behavior in (1) will be inconvenient, and the prompt in (2) will be overly long. This patch introduces a config option to toggle the relevance of http paths. When turned on, we use the path as before. When turned off, we drop the path component from the context: helpers don't see it, and it does not appear in the prompt. This is nothing you couldn't do with a clever credential helper at the start of your stack, like: [credential "http://"] helper = "!f() { grep -v ^path= ; }; f" helper = your_real_helper But doing this: [credential] useHttpPath = false is way easier and more readable. Furthermore, since most users will want the "off" behavior, that is the new default. Users who want it "on" can set the variable (either for all credentials, or just for a subset using credential.*.useHttpPath). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 10, 2011 at 05:31 UTC a78fbb4fb6c69d96cb91dee9d4733d2326c6d019
4 files changed +46 -2
credential.c
+14
@@ -69,16 +69,30 @@ static int credential_config_callback(const char *var, const char *value,
69 if (!c->username)
70 c->username = xstrdup(value);
71 }
72 + else if (!strcmp(key, "usehttppath"))
73 + c->use_http_path = git_config_bool(var, value);
74
75 return 0;
76 }
77
78 +static int proto_is_http(const char *s)
79 +{
80 + if (!s)
81 + return 0;
82 + return !strcmp(s, "https") || !strcmp(s, "http");
83 +}
84 +
85 static void credential_apply_config(struct credential *c)
86 {
87 if (c->configured)
88 return;
89 git_config(credential_config_callback, c);
90 c->configured = 1;
91 +
92 + if (!c->use_http_path && proto_is_http(c->protocol)) {
93 + free(c->path);
94 + c->path = NULL;
95 + }
96 }
97
98 static void credential_describe(struct credential *c, struct strbuf *out)
credential.h
+2 -1
@@ -6,7 +6,8 @@
6 struct credential {
7 struct string_list helpers;
8 unsigned approved:1,
9 - configured:1;
9 + configured:1,
10 + use_http_path:1;
11
12 char *username;
13 char *password;
t/t0300-credentials.sh
+29
@@ -247,4 +247,33 @@ test_expect_success 'pull username from config' '
247 EOF
248 '
249
250 +test_expect_success 'http paths can be part of context' '
251 + check fill "verbatim foo bar" <<-\EOF &&
252 + protocol=https
253 + host=example.com
254 + path=foo.git
255 + --
256 + username=foo
257 + password=bar
258 + --
259 + verbatim: get
260 + verbatim: protocol=https
261 + verbatim: host=example.com
262 + EOF
263 + test_config credential.https://example.com.useHttpPath true &&
264 + check fill "verbatim foo bar" <<-\EOF
265 + protocol=https
266 + host=example.com
267 + path=foo.git
268 + --
269 + username=foo
270 + password=bar
271 + --
272 + verbatim: get
273 + verbatim: protocol=https
274 + verbatim: host=example.com
275 + verbatim: path=foo.git
276 + EOF
277 +'
278 +
279 test_done
t/t5550-http-fetch.sh
+1 -1
@@ -53,7 +53,7 @@ test_expect_success 'setup askpass helpers' '
53 '
54
55 expect_askpass() {
56 - dest=$HTTPD_DEST/auth/repo.git
56 + dest=$HTTPD_DEST
57 {
58 case "$1" in
59 none)