From 8ce08e992e7fa73eef0118832a6a5540b2da189b Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 7 Mar 2017 16:34:50 +0100 Subject: [PATCH] Fixed support for remote branches --- .../release/internal/GitProjectRepo.java | 39 +++++++++++++++++++ .../release/internal/GitProjectRepoTests.java | 18 +++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/releaser-core/src/main/java/org/springframework/cloud/release/internal/GitProjectRepo.java b/releaser-core/src/main/java/org/springframework/cloud/release/internal/GitProjectRepo.java index 8820ca81..99fbe6bb 100644 --- a/releaser-core/src/main/java/org/springframework/cloud/release/internal/GitProjectRepo.java +++ b/releaser-core/src/main/java/org/springframework/cloud/release/internal/GitProjectRepo.java @@ -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 branches = command.call(); + for (Ref ref : branches) { + if (ref.getName().endsWith("/" + label)) { + return true; + } + } + return false; + } + private void deleteBaseDirIfExists() { if (this.basedir.exists()) { try { diff --git a/releaser-core/src/test/java/org/springframework/cloud/release/internal/GitProjectRepoTests.java b/releaser-core/src/test/java/org/springframework/cloud/release/internal/GitProjectRepoTests.java index 7f4536c9..2771e432 100644 --- a/releaser-core/src/test/java/org/springframework/cloud/release/internal/GitProjectRepoTests.java +++ b/releaser-core/src/test/java/org/springframework/cloud/release/internal/GitProjectRepoTests.java @@ -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("Camden.SR3"))).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("Camden.BUILD-SNAPSHOT"))).isTrue(); + } + @Test public void should_throw_an_exception_when_checking_out_nonexisting_branch() throws IOException { File project = this.gitProjectRepo.cloneProject(this.springCloudReleaseProject.toURI());