]> git.ipfire.org Git - thirdparty/git.git/commitdiff
remote: allow resetting url list
authorJeff King <peff@peff.net>
Fri, 14 Jun 2024 10:31:22 +0000 (06:31 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Jun 2024 16:34:38 +0000 (09:34 -0700)
Because remote.*.url is treated as a multi-valued key, there is no way
to override previous config. So for example if you have
remote.origin.url set to some wrong value, doing:

  git -c remote.origin.url=right fetch

would not work. It would append "right" to the list, which means we'd
still fetch from "wrong" (since subsequent values are used only as push
urls).

Let's provide a mechanism to reset the list, like we do for other
multi-valued keys (e.g., credential.helper, http.extraheaders, and
merge.suppressDest all use this "empty string means reset" pattern).

Reported-by: Mathew George <mathewegeorge@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config/remote.txt
remote.c
t/t5505-remote.sh

index eef0bf4f622c7baa76b323824bc187deee1346b6..8efc53e836d20bc1719e74fe6b3970f043584968 100644 (file)
@@ -8,13 +8,16 @@ remote.<name>.url::
        linkgit:git-push[1]. A configured remote can have multiple URLs;
        in this case the first is used for fetching, and all are used
        for pushing (assuming no `remote.<name>.pushurl` is defined).
+       Setting this key to the empty string clears the list of urls,
+       allowing you to override earlier config.
 
 remote.<name>.pushurl::
        The push URL of a remote repository.  See linkgit:git-push[1].
        If a `pushurl` option is present in a configured remote, it
        is used for pushing instead of `remote.<name>.url`. A configured
        remote can have multiple push URLs; in this case a push goes to
-       all of them.
+       all of them. Setting this key to the empty string clears the
+       list of urls, allowing you to override earlier config.
 
 remote.<name>.proxy::
        For remotes that require curl (http, https and ftp), the URL to
index 9417d83e516dfac2fe48279565ab37924eb853a6..b7262964fb0a891d14a2edf72796cf4d5cad61b8 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -63,12 +63,18 @@ static char *alias_url(const char *url, struct rewrites *r)
 
 static void add_url(struct remote *remote, const char *url)
 {
-       strvec_push(&remote->url, url);
+       if (*url)
+               strvec_push(&remote->url, url);
+       else
+               strvec_clear(&remote->url);
 }
 
 static void add_pushurl(struct remote *remote, const char *pushurl)
 {
-       strvec_push(&remote->pushurl, pushurl);
+       if (*pushurl)
+               strvec_push(&remote->pushurl, pushurl);
+       else
+               strvec_clear(&remote->pushurl);
 }
 
 static void add_pushurl_alias(struct remote_state *remote_state,
index 7789ff12c4b8f80f8ad4385932c342c7c9ae7d00..08424e878e104cc19a43960b987cf868f542cad2 100755 (executable)
@@ -1492,4 +1492,40 @@ test_expect_success 'refs/remotes/* <src> refspec and unqualified <dst> DWIM and
        )
 '
 
+test_expect_success 'empty config clears remote.*.url list' '
+       test_when_finished "git config --remove-section remote.multi" &&
+       git config --add remote.multi.url wrong-one &&
+       git config --add remote.multi.url wrong-two &&
+       git -c remote.multi.url= \
+           -c remote.multi.url=right-one \
+           -c remote.multi.url=right-two \
+           remote show -n multi >actual.raw &&
+       grep URL actual.raw >actual &&
+       cat >expect <<-\EOF &&
+         Fetch URL: right-one
+         Push  URL: right-one
+         Push  URL: right-two
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'empty config clears remote.*.pushurl list' '
+       test_when_finished "git config --remove-section remote.multi" &&
+       git config --add remote.multi.url right &&
+       git config --add remote.multi.url will-be-ignored &&
+       git config --add remote.multi.pushurl wrong-push-one &&
+       git config --add remote.multi.pushurl wrong-push-two &&
+       git -c remote.multi.pushurl= \
+           -c remote.multi.pushurl=right-push-one \
+           -c remote.multi.pushurl=right-push-two \
+           remote show -n multi >actual.raw &&
+       grep URL actual.raw >actual &&
+       cat >expect <<-\EOF &&
+         Fetch URL: right
+         Push  URL: right-push-one
+         Push  URL: right-push-two
+       EOF
+       test_cmp expect actual
+'
+
 test_done