]> source.dussan.org Git - jgit.git/commitdiff
TagCommand: make -f work with lightweight tags for NO_CHANGE 33/173533/3
authorThomas Wolf <thomas.wolf@paranor.ch>
Tue, 8 Dec 2020 13:52:00 +0000 (14:52 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 8 Dec 2020 21:20:45 +0000 (22:20 +0100)
JGit treated a NO_CHANGE RefUpdate as an error in all cases. But when
updating a lightweight tag, this is a successful result if -f was
specified.

Change-Id: Iddfa6d6a6dc8bf8fed81138a008ebc32d5f960bd
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/TagCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java

index 9630474b87ce72fbc25aee34d724bb853b004f80..21de1d4517cb563d1615e2eef30fd028d8f4345d 100644 (file)
@@ -12,6 +12,7 @@ package org.eclipse.jgit.api;
 import static org.eclipse.jgit.lib.Constants.R_TAGS;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -21,6 +22,7 @@ import java.util.List;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.api.errors.InvalidTagNameException;
 import org.eclipse.jgit.api.errors.JGitInternalException;
+import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
 import org.eclipse.jgit.junit.RepositoryTestCase;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
@@ -121,6 +123,27 @@ public class TagCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void testForceNoChangeLightweight() throws GitAPIException {
+               try (Git git = new Git(db)) {
+                       git.commit().setMessage("initial commit").call();
+                       RevCommit commit = git.commit().setMessage("second commit").call();
+                       git.commit().setMessage("third commit").call();
+                       Ref tagRef = git.tag().setObjectId(commit).setName("tag")
+                                       .setAnnotated(false).call();
+                       assertEquals(commit.getId(), tagRef.getObjectId());
+                       // Without force, we want to get a RefAlreadyExistsException
+                       assertThrows(RefAlreadyExistsException.class,
+                                       () -> git.tag().setObjectId(commit).setName("tag")
+                                                       .setAnnotated(false).call());
+                       // With force the call should work
+                       assertEquals(commit.getId(),
+                                       git.tag().setObjectId(commit).setName("tag")
+                                                       .setAnnotated(false).setForceUpdate(true).call()
+                                                       .getObjectId());
+               }
+       }
+
        @Test
        public void testEmptyTagName() throws GitAPIException {
                try (Git git = new Git(db)) {
index c8d4e413fb4a3c052717cf596b68e60d08fc7b4f..0f7fda01b9688fd10b0c40b5d02a5234622a6a47 100644 (file)
@@ -175,6 +175,12 @@ public class TagCommand extends GitCommand<Ref> {
                        throw new ConcurrentRefUpdateException(
                                        JGitText.get().couldNotLockHEAD, tagRef.getRef(),
                                        updateResult);
+               case NO_CHANGE:
+                       if (forceUpdate) {
+                               return repo.exactRef(refName);
+                       }
+                       throw new RefAlreadyExistsException(MessageFormat
+                                       .format(JGitText.get().tagAlreadyExists, newTagToString));
                case REJECTED:
                        throw new RefAlreadyExistsException(MessageFormat.format(
                                        JGitText.get().tagAlreadyExists, newTagToString));