Skip to content

Commit d574242

Browse files
peffgitster
authored andcommitted
credential: add credential.*.username
Credential helpers can help users avoid having to type their username and password over and over. However, some users may not want a helper for their password, or they may be running a helper which caches for a short time. In this case, it is convenient to provide the non-secret username portion of their credential via config. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1182507 commit d574242

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

credential.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ static int credential_config_callback(const char *var, const char *value,
6565

6666
if (!strcmp(key, "helper"))
6767
string_list_append(&c->helpers, value);
68+
else if (!strcmp(key, "username")) {
69+
if (!c->username)
70+
c->username = xstrdup(value);
71+
}
6872

6973
return 0;
7074
}

t/t0300-credentials.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,17 @@ test_expect_success 'do not match configured credential' '
234234
EOF
235235
'
236236

237+
test_expect_success 'pull username from config' '
238+
test_config credential.https://example.com.username foo &&
239+
check fill <<-\EOF
240+
protocol=https
241+
host=example.com
242+
--
243+
username=foo
244+
password=askpass-password
245+
--
246+
askpass: Password for '\''https://[email protected]'\'':
247+
EOF
248+
'
249+
237250
test_done

t/t5550-http-fetch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ test_expect_success 'http auth respects credential helper config' '
113113
expect_askpass none
114114
'
115115

116+
test_expect_success 'http auth can get username from config' '
117+
test_config_global "credential.$HTTPD_URL.username" user@host &&
118+
>askpass-query &&
119+
echo user@host >askpass-response &&
120+
git clone "$HTTPD_URL/auth/repo.git" clone-auth-user &&
121+
expect_askpass pass user@host
122+
'
123+
124+
test_expect_success 'configured username does not override URL' '
125+
test_config_global "credential.$HTTPD_URL.username" wrong &&
126+
>askpass-query &&
127+
echo user@host >askpass-response &&
128+
git clone "$HTTPD_URL_USER/auth/repo.git" clone-auth-user2 &&
129+
expect_askpass pass user@host
130+
'
131+
116132
test_expect_success 'fetch changes via http' '
117133
echo content >>file &&
118134
git commit -a -m two &&

0 commit comments

Comments
 (0)