Skip to content

Commit f854093

Browse files
committed
tag: allow idempotent "git tag" without "--force"
When "git tag T O" is told to create a tag pointing at an object O without the "--force" option, it refuses with "tag T already exists", even when T points at O (which makes it a no-op). Let's allow this "idempotent" case by special casing and making it truly a no-op. Signed-off-by: Junio C Hamano <[email protected]>
1 parent f93ff17 commit f854093

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

builtin/tag.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ int cmd_tag(int argc,
647647

648648
if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev))
649649
oidclr(&prev, the_repository->hash_algo);
650+
else if (!create_tag_object && oideq(&object, &prev))
651+
exit(0);
650652
else if (!force)
651653
die(_("tag '%s' already exists"), tag);
652654

t/t7004-tag.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test_expect_success 'annotated tag with --create-reflog has correct message' '
126126
'
127127

128128
test_expect_success '--create-reflog does not create reflog on failure' '
129-
test_must_fail git tag --create-reflog mytag &&
129+
test_must_fail git tag --create-reflog mytag no-such-object &&
130130
test_must_fail git reflog exists refs/tags/mytag
131131
'
132132

@@ -183,8 +183,14 @@ test_expect_success 'listing tags using a non-matching pattern should output not
183183

184184
# special cases for creating tags:
185185

186-
test_expect_success 'trying to create a tag with the name of one existing should fail' '
187-
test_must_fail git tag mytag
186+
test_expect_success 'recreating a tag without --force' '
187+
# light-weight tag pointing at the same thing
188+
# now succeeds
189+
git tag mytag HEAD &&
190+
# light-weight tag pointing at a different thing
191+
test_must_fail git tag mytag HEAD: &&
192+
# creating annotated tag, pointing at the same object.
193+
test_must_fail git tag -a -m anno mytag $taggedobject
188194
'
189195

190196
test_expect_success 'trying to create a tag with a non-valid name should fail' '

0 commit comments

Comments
 (0)