Skip to content

Commit bbd445d

Browse files
committed
tag: "git tag" refuses to use HEAD as a tagname
Even though the plumbing level allows you to create refs/tags/HEAD and refs/heads/HEAD, doing so makes it confusing within the context of the UI Git Porcelain commands provides. Just like we prevent a branch from getting called "HEAD" at the Porcelain layer (i.e. "git branch" command), teach "git tag" to refuse to create a tag "HEAD". With a few new tests, we make sure that - "git tag HEAD" and "git tag -a HEAD" are rejected - "git update-ref refs/tags/HEAD" is still allowed (this is a deliberate design decision to allow others to create their own UI on top of Git infrastructure that may be different from our UI). - "git tag -d HEAD" can remove refs/tags/HEAD to recover from an mistake. Helped-by: Jeff King <[email protected]> Helped-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5ce5b0 commit bbd445d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ int check_branch_ref(struct strbuf *sb, const char *name)
735735

736736
int check_tag_ref(struct strbuf *sb, const char *name)
737737
{
738-
if (name[0] == '-')
738+
if (name[0] == '-' || !strcmp(name, "HEAD"))
739739
return -1;
740740

741741
strbuf_reset(sb);

t/t7004-tag.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
9191
test_must_fail git reflog exists refs/tags/mytag
9292
'
9393

94+
test_expect_success 'HEAD is forbidden as a tagname' '
95+
test_when_finished "git update-ref --no-deref -d refs/tags/HEAD || :" &&
96+
test_must_fail git tag HEAD &&
97+
test_must_fail git tag -a -m "useless" HEAD
98+
'
99+
100+
test_expect_success '"git tag" can remove a tag named HEAD' '
101+
test_when_finished "git update-ref --no-deref -d refs/tags/HEAD || :" &&
102+
git update-ref refs/tags/HEAD HEAD &&
103+
git tag -d HEAD
104+
'
105+
94106
test_expect_success 'creating a tag with --create-reflog should create reflog' '
95107
git log -1 \
96108
--format="format:tag: tagging %h (%s, %cd)%n" \

0 commit comments

Comments
 (0)