Skip to content

Commit 12cfa79

Browse files
committed
symbolic-ref -d: do not allow removal of HEAD
If you delete the symbolic-ref HEAD from a repository, Git no longer considers the repository valid, and even "git symbolic-ref HEAD refs/heads/master" would not be able to recover from that state (although "git init" can, but that is a sure sign that you are talking about a "broken" repository). In the spirit similar to afe5d3d ("symbolic ref: refuse non-ref targets in HEAD", 2009-01-29), forbid removal of HEAD to avoid corrupting a repository. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e0c1cea commit 12cfa79

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

builtin/symbolic-ref.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
5656
ret = check_symref(argv[0], 1, 0, 0);
5757
if (ret)
5858
die("Cannot delete %s, not a symbolic ref", argv[0]);
59+
if (!strcmp(argv[0], "HEAD"))
60+
die("deleting '%s' is not allowed", argv[0]);
5961
return delete_ref(argv[0], NULL, REF_NODEREF);
6062
}
6163

t/t1401-symbolic-ref.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
3333
'
3434
reset_to_sane
3535

36-
test_expect_success 'symbolic-ref deletes HEAD' '
37-
git symbolic-ref -d HEAD &&
36+
test_expect_success 'HEAD cannot be removed' '
37+
test_must_fail git symbolic-ref -d HEAD
38+
'
39+
40+
reset_to_sane
41+
42+
test_expect_success 'symbolic-ref can be deleted' '
43+
git symbolic-ref NOTHEAD refs/heads/foo &&
44+
git symbolic-ref -d NOTHEAD &&
3845
test_path_is_file .git/refs/heads/foo &&
39-
test_path_is_missing .git/HEAD
46+
test_path_is_missing .git/NOTHEAD
4047
'
4148
reset_to_sane
4249

43-
test_expect_success 'symbolic-ref deletes dangling HEAD' '
44-
git symbolic-ref HEAD refs/heads/missing &&
45-
git symbolic-ref -d HEAD &&
50+
test_expect_success 'symbolic-ref can delete dangling symref' '
51+
git symbolic-ref NOTHEAD refs/heads/missing &&
52+
git symbolic-ref -d NOTHEAD &&
4653
test_path_is_missing .git/refs/heads/missing &&
47-
test_path_is_missing .git/HEAD
54+
test_path_is_missing .git/NOTHEAD
4855
'
4956
reset_to_sane
5057

0 commit comments

Comments
 (0)