All of lore.kernel.org
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: <git@vger.kernel.org>
Cc: Jeff King <peff@peff.net>
Subject: [PATCH v2 4/5] credential: use the last matching username in the config
Date: Thu, 20 Feb 2020 02:24:12 +0000	[thread overview]
Message-ID: <20200220022413.258026-5-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20200220022413.258026-1-sandals@crustytoothpaste.net>

From: brian m. carlson <bk2204@github.com>

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 <bk2204@github.com>
---
 credential.c           | 9 ++++++++-
 credential.h           | 3 ++-
 t/t0300-credentials.sh | 2 +-
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/credential.c b/credential.c
index 62be651b03..5701e32792 100644
--- a/credential.c
+++ b/credential.c
@@ -71,8 +71,10 @@ static int credential_config_callback(const char *var, const char *value,
 		else
 			string_list_clear(&c->helpers, 0);
 	} else if (!strcmp(key, "username")) {
-		if (!c->username)
+		if (!c->username_from_proto) {
+			free(c->username);
 			c->username = xstrdup(value);
+		}
 	}
 	else if (!strcmp(key, "usehttppath"))
 		c->use_http_path = git_config_bool(var, value);
@@ -163,6 +165,7 @@ int credential_read(struct credential *c, FILE *fp)
 		if (!strcmp(key, "username")) {
 			free(c->username);
 			c->username = xstrdup(value);
+			c->username_from_proto = 1;
 		} else if (!strcmp(key, "password")) {
 			free(c->password);
 			c->password = xstrdup(value);
@@ -349,10 +352,14 @@ void credential_from_url(struct credential *c, const char *url)
 	else if (!colon || at <= colon) {
 		/* Case (2) */
 		c->username = url_decode_mem(cp, at - cp);
+		if (c->username && *c->username)
+			c->username_from_proto = 1;
 		host = at + 1;
 	} else {
 		/* Case (3) */
 		c->username = url_decode_mem(cp, colon - cp);
+		if (c->username && *c->username)
+			c->username_from_proto = 1;
 		c->password = url_decode_mem(colon + 1, at - (colon + 1));
 		host = at + 1;
 	}
diff --git a/credential.h b/credential.h
index a5a3ee9bb8..fec7815dd0 100644
--- a/credential.h
+++ b/credential.h
@@ -118,7 +118,8 @@ struct credential {
 	unsigned approved:1,
 		 configured:1,
 		 quit:1,
-		 use_http_path:1;
+		 use_http_path:1,
+		 username_from_proto:1;
 
 	char *username;
 	char *password;
diff --git a/t/t0300-credentials.sh b/t/t0300-credentials.sh
index 4593a0cd3d..8f87599056 100755
--- a/t/t0300-credentials.sh
+++ b/t/t0300-credentials.sh
@@ -344,7 +344,7 @@ test_expect_success 'honors username from URL over helper (components)' '
 	EOF
 '
 
-test_expect_failure 'last matching username wins' '
+test_expect_success 'last matching username wins' '
 	test_config credential.https://example.com/path.git.username bob &&
 	test_config credential.https://example.com.username alice &&
 	test_config credential.https://example.com.helper "verbatim \"\" bar" &&

  parent reply	other threads:[~2020-02-20  2:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20  2:24 [PATCH v2 0/5] Wildcard matching for credentials brian m. carlson
2020-02-20  2:24 ` [PATCH v2 1/5] mailmap: add an additional email address for brian m. carlson brian m. carlson
2020-02-20  2:24 ` [PATCH v2 2/5] t1300: add test for urlmatch with multiple wildcards brian m. carlson
2020-02-20  2:24 ` [PATCH v2 3/5] t0300: add tests for some additional cases brian m. carlson
2020-02-20  2:24 ` brian m. carlson [this message]
2020-02-20  2:24 ` [PATCH v2 5/5] credential: allow wildcard patterns when matching config brian m. carlson
2020-02-20 21:20   ` Junio C Hamano
2020-02-21  6:10 ` [PATCH v2 0/5] Wildcard matching for credentials Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200220022413.258026-5-sandals@crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.