]> git.ipfire.org Git - thirdparty/git.git/blobdiff - credential.c
Merge branch 'js/checkout-p-new-file'
[thirdparty/git.git] / credential.c
index c1a9ca4e4852bd752f70d7d6ec39388bcd709a6a..d8d226b97e34805735cc14bdc3e549d012b33f8d 100644 (file)
@@ -5,6 +5,8 @@
 #include "run-command.h"
 #include "url.h"
 #include "prompt.h"
+#include "sigchain.h"
+#include "urlmatch.h"
 
 void credential_init(struct credential *c)
 {
@@ -43,7 +45,7 @@ static int credential_config_callback(const char *var, const char *value,
                                      void *data)
 {
        struct credential *c = data;
-       const char *key, *dot;
+       const char *key;
 
        if (!skip_prefix(var, "credential.", &key))
                return 0;
@@ -51,37 +53,16 @@ static int credential_config_callback(const char *var, const char *value,
        if (!value)
                return config_error_nonbool(var);
 
-       dot = strrchr(key, '.');
-       if (dot) {
-               struct credential want = CREDENTIAL_INIT;
-               char *url = xmemdupz(key, dot - key);
-               int matched;
-
-               if (credential_from_potentially_partial_url(&want, url) < 0) {
-                       warning(_("skipping credential lookup for key: %s"),
-                               var);
-                       credential_clear(&want);
-                       free(url);
-                       return 0;
-               }
-               matched = credential_match(&want, c);
-
-               credential_clear(&want);
-               free(url);
-
-               if (!matched)
-                       return 0;
-               key = dot + 1;
-       }
-
        if (!strcmp(key, "helper")) {
                if (*value)
                        string_list_append(&c->helpers, 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);
@@ -96,8 +77,37 @@ static int proto_is_http(const char *s)
        return !strcmp(s, "https") || !strcmp(s, "http");
 }
 
+static void credential_describe(struct credential *c, struct strbuf *out);
+static void credential_format(struct credential *c, struct strbuf *out);
+
+static int select_all(const struct urlmatch_item *a,
+                     const struct urlmatch_item *b)
+{
+       return 0;
+}
+
+static int match_partial_url(const char *url, void *cb)
+{
+       struct credential *c = cb;
+       struct credential want = CREDENTIAL_INIT;
+       int matches = 0;
+
+       if (credential_from_potentially_partial_url(&want, url) < 0)
+               warning(_("skipping credential lookup for key: credential.%s"),
+                       url);
+       else
+               matches = credential_match(&want, c);
+       credential_clear(&want);
+
+       return matches;
+}
+
 static void credential_apply_config(struct credential *c)
 {
+       char *normalized_url;
+       struct urlmatch_config config = { STRING_LIST_INIT_DUP };
+       struct strbuf url = STRBUF_INIT;
+
        if (!c->host)
                die(_("refusing to work with credential missing host field"));
        if (!c->protocol)
@@ -105,7 +115,22 @@ static void credential_apply_config(struct credential *c)
 
        if (c->configured)
                return;
-       git_config(credential_config_callback, c);
+
+       config.section = "credential";
+       config.key = NULL;
+       config.collect_fn = credential_config_callback;
+       config.cascade_fn = NULL;
+       config.select_fn = select_all;
+       config.fallback_match_fn = match_partial_url;
+       config.cb = c;
+
+       credential_format(c, &url);
+       normalized_url = url_normalize(url.buf, &config.url);
+
+       git_config(urlmatch_config_entry, &config);
+       free(normalized_url);
+       strbuf_release(&url);
+
        c->configured = 1;
 
        if (!c->use_http_path && proto_is_http(c->protocol)) {
@@ -126,6 +151,23 @@ static void credential_describe(struct credential *c, struct strbuf *out)
                strbuf_addf(out, "/%s", c->path);
 }
 
+static void credential_format(struct credential *c, struct strbuf *out)
+{
+       if (!c->protocol)
+               return;
+       strbuf_addf(out, "%s://", c->protocol);
+       if (c->username && *c->username) {
+               strbuf_add_percentencode(out, c->username, STRBUF_ENCODE_SLASH);
+               strbuf_addch(out, '@');
+       }
+       if (c->host)
+               strbuf_addstr(out, c->host);
+       if (c->path) {
+               strbuf_addch(out, '/');
+               strbuf_add_percentencode(out, c->path, 0);
+       }
+}
+
 static char *credential_ask_one(const char *what, struct credential *c,
                                int flags)
 {
@@ -177,6 +219,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);
@@ -247,8 +290,10 @@ static int run_credential_helper(struct credential *c,
                return -1;
 
        fp = xfdopen(helper.in, "w");
+       sigchain_push(SIGPIPE, SIG_IGN);
        credential_write(c, fp);
        fclose(fp);
+       sigchain_pop(SIGPIPE);
 
        if (want_output) {
                int r;
@@ -398,7 +443,14 @@ static int credential_from_url_1(struct credential *c, const char *url,
        cp = proto_end ? proto_end + 3 : url;
        at = strchr(cp, '@');
        colon = strchr(cp, ':');
-       slash = strchrnul(cp, '/');
+
+       /*
+        * A query or fragment marker before the slash ends the host portion.
+        * We'll just continue to call this "slash" for simplicity. Notably our
+        * "trim leading slashes" part won't skip over this part of the path,
+        * but that's what we'd want.
+        */
+       slash = cp + strcspn(cp, "/?#");
 
        if (!at || slash <= at) {
                /* Case (1) */
@@ -407,10 +459,14 @@ static int credential_from_url_1(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;
        }