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 <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Miklos Vajna committed
Oct 29, 2008 at 01:05 UTC
fa58186c9ba50514b36ac5ef192cd7e0bc4d7780
2 files changed
+17
-20
refs.c
+13
-16
@@ -964,14 +964,14 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
964
struct stat loginfo;
965
int log = !lstat(git_path("logs/%s", oldref), &loginfo);
966
const char *symref = NULL;
967
- int is_symref = 0;
967
968
if (log && S_ISLNK(loginfo.st_mode))
969
return error("reflog for %s is a symlink", oldref);
970
971
symref = resolve_ref(oldref, orig_sha1, 1, &flag);
972
if (flag & REF_ISSYMREF)
974
- is_symref = 1;
973
+ return error("refname %s is a symbolic ref, renaming it is not supported",
974
+ oldref);
975
if (!symref)
976
return error("refname %s not found", oldref);
977
@@ -1035,20 +1035,17 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
1035
}
1036
logmoved = log;
1037
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
+ }
1049
1050
return 0;
1051
t/t3200-branch.sh
+4
-4
@@ -112,13 +112,13 @@ test_expect_success 'config information was renamed, too' \
112
"test $(git config branch.s.dummy) = Hello &&
113
test_must_fail git config branch.s/s/dummy"
114
115
-test_expect_success 'renaming a symref' \
115
+test_expect_success 'renaming a symref is not allowed' \
116
'
117
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 &&
120
test -f .git/refs/heads/master &&
121
- ! test -f .git/refs/heads/master2
121
+ ! test -f .git/refs/heads/master3
122
'
123
124
test_expect_success \