Skip to content

Commit 034a8a0

Browse files
committed
Merge branch 'mz/remote-rename'
* mz/remote-rename: remote: only update remote-tracking branch if updating refspec remote rename: warn when refspec was not updated remote: "rename o foo" should not rename ref "origin/bar" remote: write correct fetch spec when renaming remote 'remote'
2 parents ca3ef81 + b52d00a commit 034a8a0

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

builtin/remote.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ static int read_remote_branches(const char *refname,
570570
unsigned char orig_sha1[20];
571571
const char *symref;
572572

573-
strbuf_addf(&buf, "refs/remotes/%s", rename->old);
573+
strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
574574
if (!prefixcmp(refname, buf.buf)) {
575575
item = string_list_append(rename->remote_branches, xstrdup(refname));
576576
symref = resolve_ref(refname, orig_sha1, 1, &flag);
@@ -621,10 +621,11 @@ static int mv(int argc, const char **argv)
621621
OPT_END()
622622
};
623623
struct remote *oldremote, *newremote;
624-
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
624+
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
625+
old_remote_context = STRBUF_INIT;
625626
struct string_list remote_branches = STRING_LIST_INIT_NODUP;
626627
struct rename_info rename;
627-
int i;
628+
int i, refspec_updated = 0;
628629

629630
if (argc != 3)
630631
usage_with_options(builtin_remote_rename_usage, options);
@@ -659,15 +660,25 @@ static int mv(int argc, const char **argv)
659660
strbuf_addf(&buf, "remote.%s.fetch", rename.new);
660661
if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
661662
return error("Could not remove config section '%s'", buf.buf);
663+
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
662664
for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
663665
char *ptr;
664666

665667
strbuf_reset(&buf2);
666668
strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
667-
ptr = strstr(buf2.buf, rename.old);
668-
if (ptr)
669-
strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
670-
rename.new, strlen(rename.new));
669+
ptr = strstr(buf2.buf, old_remote_context.buf);
670+
if (ptr) {
671+
refspec_updated = 1;
672+
strbuf_splice(&buf2,
673+
ptr-buf2.buf + strlen(":refs/remotes/"),
674+
strlen(rename.old), rename.new,
675+
strlen(rename.new));
676+
} else
677+
warning("Not updating non-default fetch respec\n"
678+
"\t%s\n"
679+
"\tPlease update the configuration manually if necessary.",
680+
buf2.buf);
681+
671682
if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
672683
return error("Could not append '%s'", buf.buf);
673684
}
@@ -685,6 +696,9 @@ static int mv(int argc, const char **argv)
685696
}
686697
}
687698

699+
if (!refspec_updated)
700+
return 0;
701+
688702
/*
689703
* First remove symrefs, then rename the rest, finally create
690704
* the new symrefs.

t/t5505-remote.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,37 @@ test_expect_success 'rename a remote' '
631631
632632
'
633633

634+
test_expect_success 'rename does not update a non-default fetch refspec' '
635+
636+
git clone one four.one &&
637+
(cd four.one &&
638+
git config remote.origin.fetch +refs/heads/*:refs/heads/origin/* &&
639+
git remote rename origin upstream &&
640+
test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/heads/origin/*" &&
641+
git rev-parse -q origin/master)
642+
643+
'
644+
645+
test_expect_success 'rename a remote with name part of fetch spec' '
646+
647+
git clone one four.two &&
648+
(cd four.two &&
649+
git remote rename origin remote &&
650+
git remote rename remote upstream &&
651+
test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*")
652+
653+
'
654+
655+
test_expect_success 'rename a remote with name prefix of other remote' '
656+
657+
git clone one four.three &&
658+
(cd four.three &&
659+
git remote add o git://example.com/repo.git &&
660+
git remote rename o upstream &&
661+
test "$(git rev-parse origin/master)" = "$(git rev-parse master)")
662+
663+
'
664+
634665
cat > remotes_origin << EOF
635666
URL: $(pwd)/one
636667
Push: refs/heads/master:refs/heads/upstream

0 commit comments

Comments
 (0)