Skip to content

Commit

Permalink
Merge branch 'jc/forbid-symbolic-ref-d-HEAD' into maint
Browse files Browse the repository at this point in the history
"git symbolic-ref -d HEAD" happily removes the symbolic ref, but
the resulting repository becomes an invalid one.  Teach the command
to forbid removal of HEAD.

* jc/forbid-symbolic-ref-d-HEAD:
  symbolic-ref -d: do not allow removal of HEAD
  • Loading branch information
gitster committed Sep 19, 2016
2 parents 4c10c31 + 12cfa79 commit d664531
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions builtin/symbolic-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
21 changes: 14 additions & 7 deletions t/t1401-symbolic-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d664531

Please sign in to comment.