Fix branch ref exist check
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
This commit is contained in:
parent
52af8dbaff
commit
e4a341db43
|
@ -12,6 +12,7 @@
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -160,6 +161,20 @@ public void testCreateAndList() throws Exception {
|
||||||
- allBefore);
|
- 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)
|
@Test(expected = InvalidRefNameException.class)
|
||||||
public void testInvalidBranchHEAD() throws Exception {
|
public void testInvalidBranchHEAD() throws Exception {
|
||||||
git.branchCreate().setName("HEAD").call();
|
git.branchCreate().setName("HEAD").call();
|
||||||
|
|
|
@ -88,9 +88,7 @@ public Ref call() throws GitAPIException, RefAlreadyExistsException,
|
||||||
checkCallable();
|
checkCallable();
|
||||||
processOptions();
|
processOptions();
|
||||||
try (RevWalk revWalk = new RevWalk(repo)) {
|
try (RevWalk revWalk = new RevWalk(repo)) {
|
||||||
Ref refToCheck = repo.findRef(name);
|
boolean exists = repo.findRef(R_HEADS + name) != null;
|
||||||
boolean exists = refToCheck != null
|
|
||||||
&& refToCheck.getName().startsWith(R_HEADS);
|
|
||||||
if (!force && exists)
|
if (!force && exists)
|
||||||
throw new RefAlreadyExistsException(MessageFormat.format(
|
throw new RefAlreadyExistsException(MessageFormat.format(
|
||||||
JGitText.get().refAlreadyExists1, name));
|
JGitText.get().refAlreadyExists1, name));
|
||||||
|
|
Loading…
Reference in New Issue