From a31eeae27f38246ba35000fa603d884cdd1a81ae Mon Sep 17 00:00:00 2001 From: Thomas Gummerer Date: Tue, 16 Feb 2016 10:47:52 +0100 Subject: [PATCH] remote: use remote_is_configured() for add and rename Both remote add and remote rename use a slightly different hand-rolled check if the remote exits. The hand-rolled check may have some subtle cases in which it might fail to detect when a remote already exists. One such case was fixed in fb86e32 ("git remote: allow adding remotes agreeing with url.<...>.insteadOf"). Another case is when a remote is configured as follows: [remote "foo"] vcs = bar If we try to run `git remote add foo bar` with the above remote configuration, git segfaults. This change fixes it. In addition, git remote rename $existing foo with the configuration for foo as above silently succeeds, even though foo already exists, modifying its configuration. With this patch it fails with "remote foo already exists". Signed-off-by: Thomas Gummerer Reviewed-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/remote.c | 7 ++----- t/t5505-remote.sh | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/builtin/remote.c b/builtin/remote.c index 981c487ef9..bd57f1b29b 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -186,10 +186,7 @@ static int add(int argc, const char **argv) url = argv[1]; remote = remote_get(name); - if (remote && (remote->url_nr > 1 || - (strcmp(name, remote->url[0]) && - strcmp(url, remote->url[0])) || - remote->fetch_refspec_nr)) + if (remote_is_configured(remote)) die(_("remote %s already exists."), name); strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); @@ -641,7 +638,7 @@ static int mv(int argc, const char **argv) return migrate_file(oldremote); newremote = remote_get(rename.new); - if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr)) + if (remote_is_configured(newremote)) die(_("remote %s already exists."), rename.new); strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new); diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index f1d073f1ba..94079a0e63 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -157,6 +157,21 @@ test_expect_success 'rename errors out early when deleting non-existent branch' ) ' +test_expect_success 'add existing foreign_vcs remote' ' + test_config remote.foo.vcs bar && + echo "fatal: remote foo already exists." >expect && + test_must_fail git remote add foo bar 2>actual && + test_i18ncmp expect actual +' + +test_expect_success 'add existing foreign_vcs remote' ' + test_config remote.foo.vcs bar && + test_config remote.bar.vcs bar && + echo "fatal: remote bar already exists." >expect && + test_must_fail git remote rename foo bar 2>actual && + test_i18ncmp expect actual +' + cat >test/expect <