Skip to content

Commit fa58186

Browse files
Miklos Vajnagitster
authored andcommitted
git branch -m: forbid renaming of a symref
There may be cases where one would really want to rename the symbolic ref without changing its value, but "git branch -m" is not such a use-case. Signed-off-by: Miklos Vajna <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 569740b commit fa58186

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

refs.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -964,14 +964,14 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
964964
struct stat loginfo;
965965
int log = !lstat(git_path("logs/%s", oldref), &loginfo);
966966
const char *symref = NULL;
967-
int is_symref = 0;
968967

969968
if (log && S_ISLNK(loginfo.st_mode))
970969
return error("reflog for %s is a symlink", oldref);
971970

972971
symref = resolve_ref(oldref, orig_sha1, 1, &flag);
973972
if (flag & REF_ISSYMREF)
974-
is_symref = 1;
973+
return error("refname %s is a symbolic ref, renaming it is not supported",
974+
oldref);
975975
if (!symref)
976976
return error("refname %s not found", oldref);
977977

@@ -1035,20 +1035,17 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
10351035
}
10361036
logmoved = log;
10371037

1038-
if (!is_symref) {
1039-
lock = lock_ref_sha1_basic(newref, NULL, 0, NULL);
1040-
if (!lock) {
1041-
error("unable to lock %s for update", newref);
1042-
goto rollback;
1043-
}
1044-
lock->force_write = 1;
1045-
hashcpy(lock->old_sha1, orig_sha1);
1046-
if (write_ref_sha1(lock, orig_sha1, logmsg)) {
1047-
error("unable to write current sha1 into %s", newref);
1048-
goto rollback;
1049-
}
1050-
} else
1051-
create_symref(newref, symref, logmsg);
1038+
lock = lock_ref_sha1_basic(newref, NULL, 0, NULL);
1039+
if (!lock) {
1040+
error("unable to lock %s for update", newref);
1041+
goto rollback;
1042+
}
1043+
lock->force_write = 1;
1044+
hashcpy(lock->old_sha1, orig_sha1);
1045+
if (write_ref_sha1(lock, orig_sha1, logmsg)) {
1046+
error("unable to write current sha1 into %s", newref);
1047+
goto rollback;
1048+
}
10521049

10531050
return 0;
10541051

t/t3200-branch.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ test_expect_success 'config information was renamed, too' \
112112
"test $(git config branch.s.dummy) = Hello &&
113113
test_must_fail git config branch.s/s/dummy"
114114

115-
test_expect_success 'renaming a symref' \
115+
test_expect_success 'renaming a symref is not allowed' \
116116
'
117117
git symbolic-ref refs/heads/master2 refs/heads/master &&
118-
git branch -m master2 master3 &&
119-
git symbolic-ref refs/heads/master3 &&
118+
test_must_fail git branch -m master2 master3 &&
119+
git symbolic-ref refs/heads/master2 &&
120120
test -f .git/refs/heads/master &&
121-
! test -f .git/refs/heads/master2
121+
! test -f .git/refs/heads/master3
122122
'
123123

124124
test_expect_success \

0 commit comments

Comments
 (0)