Fixed support for remote branches
This commit is contained in:
@@ -19,10 +19,13 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jgit.api.CheckoutCommand;
|
||||
import org.eclipse.jgit.api.CloneCommand;
|
||||
import org.eclipse.jgit.api.CreateBranchCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.ListBranchCommand;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.util.FileUtils;
|
||||
@@ -109,6 +112,9 @@ class GitProjectRepo {
|
||||
Git git = this.gitFactory.open(projectDir);
|
||||
CheckoutCommand command = git.checkout().setName(branch);
|
||||
try {
|
||||
if (shouldTrack(git, branch)) {
|
||||
trackBranch(command, branch);
|
||||
}
|
||||
return command.call();
|
||||
}
|
||||
catch (GitAPIException e) {
|
||||
@@ -119,6 +125,39 @@ class GitProjectRepo {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldTrack(Git git, String label) throws GitAPIException {
|
||||
return isBranch(git, label) && !isLocalBranch(git, label);
|
||||
}
|
||||
|
||||
private void trackBranch(CheckoutCommand checkout, String label) {
|
||||
checkout.setCreateBranch(true).setName(label)
|
||||
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
|
||||
.setStartPoint("origin/" + label);
|
||||
}
|
||||
|
||||
private boolean isBranch(Git git, String label) throws GitAPIException {
|
||||
return containsBranch(git, label, ListBranchCommand.ListMode.ALL);
|
||||
}
|
||||
|
||||
private boolean isLocalBranch(Git git, String label) throws GitAPIException {
|
||||
return containsBranch(git, label, null);
|
||||
}
|
||||
|
||||
private boolean containsBranch(Git git, String label, ListBranchCommand.ListMode listMode)
|
||||
throws GitAPIException {
|
||||
ListBranchCommand command = git.branchList();
|
||||
if (listMode != null) {
|
||||
command.setListMode(listMode);
|
||||
}
|
||||
List<Ref> branches = command.call();
|
||||
for (Ref ref : branches) {
|
||||
if (ref.getName().endsWith("/" + label)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void deleteBaseDirIfExists() {
|
||||
if (this.basedir.exists()) {
|
||||
try {
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package org.springframework.cloud.release.internal;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -15,6 +11,10 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.assertj.core.api.BDDAssertions.then;
|
||||
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@@ -66,6 +66,16 @@ public class GitProjectRepoTests {
|
||||
then(Files.lines(pom.toPath()).anyMatch(s -> s.contains("<version>Camden.SR3</version>"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_check_out_a_branch_on_cloned_repo2() throws IOException {
|
||||
File project = this.gitProjectRepo.cloneProject(this.springCloudReleaseProject.toURI());
|
||||
this.gitProjectRepo.checkout(project, "Camden.x");
|
||||
|
||||
File pom = new File(this.tmpFolder, "pom.xml");
|
||||
then(pom).exists();
|
||||
then(Files.lines(pom.toPath()).anyMatch(s -> s.contains("<version>Camden.BUILD-SNAPSHOT</version>"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_throw_an_exception_when_checking_out_nonexisting_branch() throws IOException {
|
||||
File project = this.gitProjectRepo.cloneProject(this.springCloudReleaseProject.toURI());
|
||||
|
||||
Reference in New Issue
Block a user