Skip to content

Commit 716b64a

Browse files
committed
Merge branch 'mz/remote-rename' into maint-1.7.6
* 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 8371e91 + b52d00a commit 716b64a

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
@@ -580,7 +580,7 @@ static int read_remote_branches(const char *refname,
580580
unsigned char orig_sha1[20];
581581
const char *symref;
582582

583-
strbuf_addf(&buf, "refs/remotes/%s", rename->old);
583+
strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
584584
if (!prefixcmp(refname, buf.buf)) {
585585
item = string_list_append(rename->remote_branches, xstrdup(refname));
586586
symref = resolve_ref(refname, orig_sha1, 1, &flag);
@@ -631,10 +631,11 @@ static int mv(int argc, const char **argv)
631631
OPT_END()
632632
};
633633
struct remote *oldremote, *newremote;
634-
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
634+
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
635+
old_remote_context = STRBUF_INIT;
635636
struct string_list remote_branches = STRING_LIST_INIT_NODUP;
636637
struct rename_info rename;
637-
int i;
638+
int i, refspec_updated = 0;
638639

639640
if (argc != 3)
640641
usage_with_options(builtin_remote_rename_usage, options);
@@ -669,15 +670,25 @@ static int mv(int argc, const char **argv)
669670
strbuf_addf(&buf, "remote.%s.fetch", rename.new);
670671
if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
671672
return error("Could not remove config section '%s'", buf.buf);
673+
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
672674
for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
673675
char *ptr;
674676

675677
strbuf_reset(&buf2);
676678
strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
677-
ptr = strstr(buf2.buf, rename.old);
678-
if (ptr)
679-
strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
680-
rename.new, strlen(rename.new));
679+
ptr = strstr(buf2.buf, old_remote_context.buf);
680+
if (ptr) {
681+
refspec_updated = 1;
682+
strbuf_splice(&buf2,
683+
ptr-buf2.buf + strlen(":refs/remotes/"),
684+
strlen(rename.old), rename.new,
685+
strlen(rename.new));
686+
} else
687+
warning("Not updating non-default fetch respec\n"
688+
"\t%s\n"
689+
"\tPlease update the configuration manually if necessary.",
690+
buf2.buf);
691+
681692
if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
682693
return error("Could not append '%s'", buf.buf);
683694
}
@@ -695,6 +706,9 @@ static int mv(int argc, const char **argv)
695706
}
696707
}
697708

709+
if (!refspec_updated)
710+
return 0;
711+
698712
/*
699713
* First remove symrefs, then rename the rest, finally create
700714
* 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)