Skip to content

Commit 82eb249

Browse files
bk2204gitster
authored andcommitted
credential: use the last matching username in the config
Everywhere else in the codebase, we use the rule that the last matching configuration option is the one that takes effect. This is helpful because it allows more specific configuration settings (e.g., per-repo configuration) to override less specific settings (e.g., per-user configuration). However, in the credential code, we didn't honor this setting, and instead picked the first setting we had, and stuck with it. This was likely to ensure we picked the value from the URL, which we want to honor over the configuration. It's possible to do both, though, so let's check if the value is the one we've gotten over our protocol connection, which if present will have come from the URL, and keep it if so. Otherwise, let's overwrite the value with the latest version we've got from the configuration, so we keep the last configuration value. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 588c70e commit 82eb249

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

credential.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ static int credential_config_callback(const char *var, const char *value,
7171
else
7272
string_list_clear(&c->helpers, 0);
7373
} else if (!strcmp(key, "username")) {
74-
if (!c->username)
74+
if (!c->username_from_proto) {
75+
free(c->username);
7576
c->username = xstrdup(value);
77+
}
7678
}
7779
else if (!strcmp(key, "usehttppath"))
7880
c->use_http_path = git_config_bool(var, value);
@@ -163,6 +165,7 @@ int credential_read(struct credential *c, FILE *fp)
163165
if (!strcmp(key, "username")) {
164166
free(c->username);
165167
c->username = xstrdup(value);
168+
c->username_from_proto = 1;
166169
} else if (!strcmp(key, "password")) {
167170
free(c->password);
168171
c->password = xstrdup(value);
@@ -349,10 +352,14 @@ void credential_from_url(struct credential *c, const char *url)
349352
else if (!colon || at <= colon) {
350353
/* Case (2) */
351354
c->username = url_decode_mem(cp, at - cp);
355+
if (c->username && *c->username)
356+
c->username_from_proto = 1;
352357
host = at + 1;
353358
} else {
354359
/* Case (3) */
355360
c->username = url_decode_mem(cp, colon - cp);
361+
if (c->username && *c->username)
362+
c->username_from_proto = 1;
356363
c->password = url_decode_mem(colon + 1, at - (colon + 1));
357364
host = at + 1;
358365
}

credential.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ struct credential {
208208
unsigned approved:1,
209209
configured:1,
210210
quit:1,
211-
use_http_path:1;
211+
use_http_path:1,
212+
username_from_proto:1;
212213

213214
char *username;
214215
char *password;

t/t0300-credentials.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ test_expect_success 'honors username from URL over helper (components)' '
344344
EOF
345345
'
346346

347-
test_expect_failure 'last matching username wins' '
347+
test_expect_success 'last matching username wins' '
348348
test_config credential.https://example.com/path.git.username bob &&
349349
test_config credential.https://example.com.username alice &&
350350
test_config credential.https://example.com.helper "verbatim \"\" bar" &&

0 commit comments

Comments
 (0)