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 afe5d3d5 ("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>
Junio C Hamano committed
Sep 1, 2016 at 15:38 UTC
12cfa792b8657cfd37523df83df0a83d987570a5
2 files changed
+16
-7
builtin/symbolic-ref.c
+2
@@ -56,6 +56,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
56
ret = check_symref(argv[0], 1, 0, 0);
57
if (ret)
58
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]);
61
return delete_ref(argv[0], NULL, REF_NODEREF);
62
}
63
t/t1401-symbolic-ref.sh
+14
-7
@@ -33,18 +33,25 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
33
'
34
reset_to_sane
35
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 &&
45
test_path_is_file .git/refs/heads/foo &&
39
- test_path_is_missing .git/HEAD
46
+ test_path_is_missing .git/NOTHEAD
47
'
48
reset_to_sane
49
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 &&
53
test_path_is_missing .git/refs/heads/missing &&
47
- test_path_is_missing .git/HEAD
54
+ test_path_is_missing .git/NOTHEAD
55
'
56
reset_to_sane
57