Skip to content

Commit a78fbb4

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d574242 commit a78fbb4

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

credential.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,30 @@ static int credential_config_callback(const char *var, const char *value,
6969
if (!c->username)
7070
c->username = xstrdup(value);
7171
}
72+
else if (!strcmp(key, "usehttppath"))
73+
c->use_http_path = git_config_bool(var, value);
7274

7375
return 0;
7476
}
7577

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+
7685
static void credential_apply_config(struct credential *c)
7786
{
7887
if (c->configured)
7988
return;
8089
git_config(credential_config_callback, c);
8190
c->configured = 1;
91+
92+
if (!c->use_http_path && proto_is_http(c->protocol)) {
93+
free(c->path);
94+
c->path = NULL;
95+
}
8296
}
8397

8498
static void credential_describe(struct credential *c, struct strbuf *out)

credential.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
struct credential {
77
struct string_list helpers;
88
unsigned approved:1,
9-
configured:1;
9+
configured:1,
10+
use_http_path:1;
1011

1112
char *username;
1213
char *password;

t/t0300-credentials.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,33 @@ test_expect_success 'pull username from config' '
247247
EOF
248248
'
249249

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+
250279
test_done

t/t5550-http-fetch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_expect_success 'setup askpass helpers' '
5353
'
5454

5555
expect_askpass() {
56-
dest=$HTTPD_DEST/auth/repo.git
56+
dest=$HTTPD_DEST
5757
{
5858
case "$1" in
5959
none)

0 commit comments

Comments
 (0)