]> source.dussan.org Git - jgit.git/commitdiff
Fix branch ref exist check 74/204974/3
authorflorian.signoret <florian.signoret@ibm.com>
Mon, 16 Oct 2023 14:08:14 +0000 (16:08 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Fri, 17 Nov 2023 23:05:35 +0000 (00:05 +0100)
When a tag with the same name as the branch exists, the branch creation
process should work too. We should detect that the branch already
exists, and allow to force create it when the force option is used.

Bug: 582538
Change-Id: I3b350d03be8edcde10e97b2318343240ca896cb0

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java

index 87be813c8526bc9812238dbb4a13b4a9e10e2803..7c1cbc37d6fd7dce9f371815bbb9a2f7ba1cfb95 100644 (file)
@@ -12,6 +12,7 @@ package org.eclipse.jgit.api;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.fail;
 
 import java.util.List;
@@ -160,6 +161,20 @@ public class BranchCommandTest extends RepositoryTestCase {
                                - allBefore);
        }
 
+  @Test
+  public void testExistingNameInBothBranchesAndTags() throws Exception {
+    git.branchCreate().setName("test").call();
+    git.tag().setName("test").call();
+
+    // existing name not allowed w/o force
+       assertThrows("Create branch with existing ref name should fail",
+                       RefAlreadyExistsException.class,
+                       () -> git.branchCreate().setName("test").call());
+
+    // existing name allowed with force option
+    git.branchCreate().setName("test").setForce(true).call();
+  }
+
        @Test(expected = InvalidRefNameException.class)
        public void testInvalidBranchHEAD() throws Exception {
                git.branchCreate().setName("HEAD").call();
index e1efb86cdcfb7995f24298430193858d2682c306..013e0ff131891506b6738180b7a8198d3d1123d6 100644 (file)
@@ -88,9 +88,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                checkCallable();
                processOptions();
                try (RevWalk revWalk = new RevWalk(repo)) {
-                       Ref refToCheck = repo.findRef(name);
-                       boolean exists = refToCheck != null
-                                       && refToCheck.getName().startsWith(R_HEADS);
+                       boolean exists = repo.findRef(R_HEADS + name) != null;
                        if (!force && exists)
                                throw new RefAlreadyExistsException(MessageFormat.format(
                                                JGitText.get().refAlreadyExists1, name));