Skip to content

Commit 665d7e0

Browse files
committed
Merge branch 'jk/remote-rename-without-fetch-refspec' into maint-2.38
"git remote rename" failed to rename a remote without fetch refspec, which has been corrected. * jk/remote-rename-without-fetch-refspec: remote: handle rename of remote without fetch refspec
2 parents 457f863 + 5a97b38 commit 665d7e0

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
@@ -733,29 +733,31 @@ static int mv(int argc, const char **argv, const char *prefix)
733733
return error(_("Could not rename config section '%s' to '%s'"),
734734
buf.buf, buf2.buf);
735735

736-
strbuf_reset(&buf);
737-
strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
738-
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
739-
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
740-
for (i = 0; i < oldremote->fetch.raw_nr; i++) {
741-
char *ptr;
742-
743-
strbuf_reset(&buf2);
744-
strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
745-
ptr = strstr(buf2.buf, old_remote_context.buf);
746-
if (ptr) {
747-
refspec_updated = 1;
748-
strbuf_splice(&buf2,
749-
ptr-buf2.buf + strlen(":refs/remotes/"),
750-
strlen(rename.old_name), rename.new_name,
751-
strlen(rename.new_name));
752-
} else
753-
warning(_("Not updating non-default fetch refspec\n"
754-
"\t%s\n"
755-
"\tPlease update the configuration manually if necessary."),
756-
buf2.buf);
757-
758-
git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
736+
if (oldremote->fetch.raw_nr) {
737+
strbuf_reset(&buf);
738+
strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
739+
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
740+
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
741+
for (i = 0; i < oldremote->fetch.raw_nr; i++) {
742+
char *ptr;
743+
744+
strbuf_reset(&buf2);
745+
strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
746+
ptr = strstr(buf2.buf, old_remote_context.buf);
747+
if (ptr) {
748+
refspec_updated = 1;
749+
strbuf_splice(&buf2,
750+
ptr-buf2.buf + strlen(":refs/remotes/"),
751+
strlen(rename.old_name), rename.new_name,
752+
strlen(rename.new_name));
753+
} else
754+
warning(_("Not updating non-default fetch refspec\n"
755+
"\t%s\n"
756+
"\tPlease update the configuration manually if necessary."),
757+
buf2.buf);
758+
759+
git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
760+
}
759761
}
760762

761763
read_branches();

t/t5505-remote.sh

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

905+
test_expect_success 'rename handles remote without fetch refspec' '
906+
git clone --bare one no-refspec.git &&
907+
# confirm assumption that bare clone does not create refspec
908+
test_expect_code 5 \
909+
git -C no-refspec.git config --unset-all remote.origin.fetch &&
910+
git -C no-refspec.git config remote.origin.url >expect &&
911+
git -C no-refspec.git remote rename origin foo &&
912+
git -C no-refspec.git config remote.foo.url >actual &&
913+
test_cmp expect actual
914+
'
915+
905916
test_expect_success 'rename does not update a non-default fetch refspec' '
906917
git clone one four.one &&
907918
(

0 commit comments

Comments
 (0)