remote: handle rename of remote without fetch refspec

We return an error when trying to rename a remote that has no fetch refspec: $ git config --unset-all remote.origin.fetch $ git remote rename origin foo fatal: could not unset 'remote.foo.fetch' To make things even more confusing, we actually _do_ complete the config modification, via git_config_rename_section(). After that we try to rewrite the fetch refspec (to say refs/remotes/foo instead of origin). But our call to git_config_set_multivar() to remove the existing entries fails, since there aren't any, and it calls die(). We could fix this by using the "gently" form of the config call, and checking the error code. But there is an even simpler fix: if we know that there are no refspecs to rewrite, then we can skip that part entirely. Reported-by: John A. Leuenhagen <john@zlima12.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 22, 2022 at 01:33 UTC 5a97b3810946440764a0a9a2292fcfdfa11391d5
2 files changed +36 -23
builtin/remote.c
+25 -23
@@ -726,29 +726,31 @@ static int mv(int argc, const char **argv)
726 return error(_("Could not rename config section '%s' to '%s'"),
727 buf.buf, buf2.buf);
728
729 - strbuf_reset(&buf);
730 - strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
731 - git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
732 - strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
733 - for (i = 0; i < oldremote->fetch.raw_nr; i++) {
734 - char *ptr;
735 -
736 - strbuf_reset(&buf2);
737 - strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
738 - ptr = strstr(buf2.buf, old_remote_context.buf);
739 - if (ptr) {
740 - refspec_updated = 1;
741 - strbuf_splice(&buf2,
742 - ptr-buf2.buf + strlen(":refs/remotes/"),
743 - strlen(rename.old_name), rename.new_name,
744 - strlen(rename.new_name));
745 - } else
746 - warning(_("Not updating non-default fetch refspec\n"
747 - "\t%s\n"
748 - "\tPlease update the configuration manually if necessary."),
749 - buf2.buf);
750 -
751 - git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
729 + if (oldremote->fetch.raw_nr) {
730 + strbuf_reset(&buf);
731 + strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
732 + git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
733 + strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
734 + for (i = 0; i < oldremote->fetch.raw_nr; i++) {
735 + char *ptr;
736 +
737 + strbuf_reset(&buf2);
738 + strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
739 + ptr = strstr(buf2.buf, old_remote_context.buf);
740 + if (ptr) {
741 + refspec_updated = 1;
742 + strbuf_splice(&buf2,
743 + ptr-buf2.buf + strlen(":refs/remotes/"),
744 + strlen(rename.old_name), rename.new_name,
745 + strlen(rename.new_name));
746 + } else
747 + warning(_("Not updating non-default fetch refspec\n"
748 + "\t%s\n"
749 + "\tPlease update the configuration manually if necessary."),
750 + buf2.buf);
751 +
752 + git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
753 + }
754 }
755
756 read_branches();
t/t5505-remote.sh
+11
@@ -836,6 +836,17 @@ test_expect_success 'rename a remote renames repo remote.pushDefault but keeps g
836 )
837 '
838
839 +test_expect_success 'rename handles remote without fetch refspec' '
840 + git clone --bare one no-refspec.git &&
841 + # confirm assumption that bare clone does not create refspec
842 + test_expect_code 5 \
843 + git -C no-refspec.git config --unset-all remote.origin.fetch &&
844 + git -C no-refspec.git config remote.origin.url >expect &&
845 + git -C no-refspec.git remote rename origin foo &&
846 + git -C no-refspec.git config remote.foo.url >actual &&
847 + test_cmp expect actual
848 +'
849 +
850 test_expect_success 'rename does not update a non-default fetch refspec' '
851 git clone one four.one &&
852 (