]> git.ipfire.org Git - thirdparty/git.git/commitdiff
symbolic-ref -d: do not allow removal of HEAD
authorJunio C Hamano <gitster@pobox.com>
Thu, 1 Sep 2016 22:38:02 +0000 (15:38 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2016 16:01:38 +0000 (09:01 -0700)
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>
builtin/symbolic-ref.c
t/t1401-symbolic-ref.sh

index 9c29a64e4331e1e45b4467829ba950006b673818..96eed944683a45c33fa5d4a05414ae56406fafca 100644 (file)
@@ -56,6 +56,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
                ret = check_symref(argv[0], 1, 0, 0);
                if (ret)
                        die("Cannot delete %s, not a symbolic ref", argv[0]);
+               if (!strcmp(argv[0], "HEAD"))
+                       die("deleting '%s' is not allowed", argv[0]);
                return delete_ref(argv[0], NULL, REF_NODEREF);
        }
 
index ca3fa406c34f1d41ac5d2be67804af234b996654..eec3e90f9c04e9b88ebb8c2ee6f12b96b75bac2b 100755 (executable)
@@ -33,18 +33,25 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
 '
 reset_to_sane
 
-test_expect_success 'symbolic-ref deletes HEAD' '
-       git symbolic-ref -d HEAD &&
+test_expect_success 'HEAD cannot be removed' '
+       test_must_fail git symbolic-ref -d HEAD
+'
+
+reset_to_sane
+
+test_expect_success 'symbolic-ref can be deleted' '
+       git symbolic-ref NOTHEAD refs/heads/foo &&
+       git symbolic-ref -d NOTHEAD &&
        test_path_is_file .git/refs/heads/foo &&
-       test_path_is_missing .git/HEAD
+       test_path_is_missing .git/NOTHEAD
 '
 reset_to_sane
 
-test_expect_success 'symbolic-ref deletes dangling HEAD' '
-       git symbolic-ref HEAD refs/heads/missing &&
-       git symbolic-ref -d HEAD &&
+test_expect_success 'symbolic-ref can delete dangling symref' '
+       git symbolic-ref NOTHEAD refs/heads/missing &&
+       git symbolic-ref -d NOTHEAD &&
        test_path_is_missing .git/refs/heads/missing &&
-       test_path_is_missing .git/HEAD
+       test_path_is_missing .git/NOTHEAD
 '
 reset_to_sane