Skip to content

Commit 5a97b38

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a0feb86 commit 5a97b38

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

builtin/remote.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -726,29 +726,31 @@ static int mv(int argc, const char **argv)
726726
return error(_("Could not rename config section '%s' to '%s'"),
727727
buf.buf, buf2.buf);
728728

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+
}
752754
}
753755

754756
read_branches();

t/t5505-remote.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,17 @@ test_expect_success 'rename a remote renames repo remote.pushDefault but keeps g
836836
)
837837
'
838838

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+
839850
test_expect_success 'rename does not update a non-default fetch refspec' '
840851
git clone one four.one &&
841852
(

0 commit comments

Comments
 (0)