]> git.ipfire.org Git - thirdparty/git.git/commitdiff
tag: allow idempotent "git tag" without "--force"
authorJunio C Hamano <gitster@pobox.com>
Tue, 10 Jun 2025 14:51:33 +0000 (07:51 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 10 Jun 2025 16:48:53 +0000 (09:48 -0700)
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 <gitster@pobox.com>
builtin/tag.c
t/t7004-tag.sh

index c4bd14583189001b0952eb3443d32bbc4be9de33..1dd69b3447a6458477d284688d83843a01744656 100644 (file)
@@ -647,6 +647,8 @@ int cmd_tag(int argc,
 
        if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev))
                oidclr(&prev, the_repository->hash_algo);
+       else if (!create_tag_object && oideq(&object, &prev))
+               exit(0);
        else if (!force)
                die(_("tag '%s' already exists"), tag);
 
index 10835631ca76444436fb5aef267082fb226b70c6..9a253a44a8e46fadfc693035d44859e367e760c0 100755 (executable)
@@ -126,7 +126,7 @@ test_expect_success 'annotated tag with --create-reflog has correct message' '
 '
 
 test_expect_success '--create-reflog does not create reflog on failure' '
-       test_must_fail git tag --create-reflog mytag &&
+       test_must_fail git tag --create-reflog mytag no-such-object &&
        test_must_fail git reflog exists refs/tags/mytag
 '
 
@@ -183,8 +183,14 @@ test_expect_success 'listing tags using a non-matching pattern should output not
 
 # special cases for creating tags:
 
-test_expect_success 'trying to create a tag with the name of one existing should fail' '
-       test_must_fail git tag mytag
+test_expect_success 'recreating a tag without --force' '
+       # light-weight tag pointing at the same thing
+       # now succeeds
+       git tag mytag HEAD &&
+       # light-weight tag pointing at a different thing
+       test_must_fail git tag mytag HEAD: &&
+       # creating annotated tag, pointing at the same object.
+       test_must_fail git tag -a -m anno mytag $taggedobject
 '
 
 test_expect_success 'trying to create a tag with a non-valid name should fail' '