From 7b4cc2ece5bacefb699a0d0d76bc4832d2f1eed3 Mon Sep 17 00:00:00 2001 From: Anton Korneta Date: Thu, 25 Feb 2016 10:32:56 +0200 Subject: [PATCH] Fix finding of git remote branches Signed-off-by: Anton Korneta --- .../che/api/git/GitProjectImporter.java | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java b/core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java index 651a6c1ce6..68245d7c99 100644 --- a/core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java +++ b/core/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java @@ -28,7 +28,9 @@ import org.eclipse.che.api.git.shared.CloneRequest; import org.eclipse.che.api.git.shared.FetchRequest; import org.eclipse.che.api.git.shared.GitCheckoutEvent; import org.eclipse.che.api.git.shared.InitRequest; +import org.eclipse.che.api.git.shared.Remote; import org.eclipse.che.api.git.shared.RemoteAddRequest; +import org.eclipse.che.api.git.shared.RemoteListRequest; import org.eclipse.che.api.project.server.FolderEntry; import org.eclipse.che.api.project.server.ProjectImporter; import org.eclipse.che.commons.lang.IoUtil; @@ -51,7 +53,9 @@ import java.util.Optional; import static org.eclipse.che.api.core.ErrorCodes.FAILED_CHECKOUT; import static org.eclipse.che.api.core.ErrorCodes.FAILED_CHECKOUT_WITH_START_POINT; +import static org.eclipse.che.api.git.shared.BranchListRequest.LIST_REMOTE; import static org.eclipse.che.api.git.shared.BranchListRequest.LIST_ALL; +import static org.eclipse.che.dto.server.DtoFactory.newDto; /** * @author Vladyslav Zhukovskii @@ -168,10 +172,10 @@ public class GitProjectImporter implements ProjectImporter { git.getConfig().add("remote.origin.fetch", remoteOriginFetch); fetch(git, "origin", dtoFactory); if (branch != null) { - checkoutBranch(git, projectName, branch, startPoint, dtoFactory); + checkoutBranch(git, projectName, branch, startPoint, location, dtoFactory); } } else if (branch != null) { - checkoutBranch(git, projectName, branch, startPoint, dtoFactory); + checkoutBranch(git, projectName, branch, startPoint, location, dtoFactory); } } else { initRepository(git, dtoFactory); @@ -183,14 +187,14 @@ public class GitProjectImporter implements ProjectImporter { git.getConfig().add("remote.origin.fetch", remoteOriginFetch); fetch(git, "origin", dtoFactory); if (branch != null) { - checkoutBranch(git, projectName, branch, startPoint, dtoFactory); + checkoutBranch(git, projectName, branch, startPoint, location, dtoFactory); } } else { fetchBranch(git, "origin", branch == null ? "*" : branch, dtoFactory); List branchList = git.branchList(dtoFactory.createDto(BranchListRequest.class).withListMode("r")); if (!branchList.isEmpty()) { - checkoutBranch(git, projectName, branch == null ? "master" : branch, startPoint, dtoFactory); + checkoutBranch(git, projectName, branch == null ? "master" : branch, startPoint, location, dtoFactory); } } } @@ -271,6 +275,7 @@ public class GitProjectImporter implements ProjectImporter { String projectName, String branchName, String startPoint, + String url, DtoFactory dtoFactory) throws GitException { final CheckoutRequest request = dtoFactory.createDto(CheckoutRequest.class).withName(branchName); final boolean branchExist = git.branchList(dtoFactory.createDto(BranchListRequest.class).withListMode(LIST_ALL)) @@ -283,7 +288,7 @@ public class GitProjectImporter implements ProjectImporter { if (branchExist) { git.checkout(request); eventService.publish(checkout.withCheckoutOnly(true) - .withBranchRef(getRemoteBranch(dtoFactory, git, branchName))); + .withBranchRef(getRemoteBranchRef(git, branchName, url))); } else { checkoutAndRethrow(git, request.withCreateNew(true).withStartPoint(startPoint), FAILED_CHECKOUT_WITH_START_POINT); eventService.publish(checkout.withCheckoutOnly(false)); @@ -291,7 +296,7 @@ public class GitProjectImporter implements ProjectImporter { } else { checkoutAndRethrow(git, request, FAILED_CHECKOUT); eventService.publish(checkout.withCheckoutOnly(true) - .withBranchRef(getRemoteBranch(dtoFactory, git, branchName))); + .withBranchRef(getRemoteBranchRef(git, branchName, url))); } } @@ -330,7 +335,7 @@ public class GitProjectImporter implements ProjectImporter { throw new GitException(e); } fetchBranch(git, "origin", branch, dtoFactory); - checkoutBranch(git, projectName, branch, startPoint, dtoFactory); + checkoutBranch(git, projectName, branch, startPoint, url, dtoFactory); } private void cleanGit(File project) { @@ -338,15 +343,21 @@ public class GitProjectImporter implements ProjectImporter { new File(project, ".gitignore").delete(); } - private String getRemoteBranch(DtoFactory dtoFactory, GitConnection git, String branchName) throws GitException { - final List remotes = git.branchList(dtoFactory.createDto(BranchListRequest.class) - .withListMode(BranchListRequest.LIST_REMOTE)); - final Optional first = remotes.stream() - .filter(br -> branchName.equals(br.getDisplayName())) - .findFirst(); - if (!first.isPresent()) { - throw new GitException("Failed to get remote branch name", FAILED_CHECKOUT); + /** + * Looking for remote branch with specified name if it does not exist null will be returned. + */ + private String getRemoteBranchRef(GitConnection git, String branchName, String url) throws GitException { + final List remotes = git.remoteList(newDto(RemoteListRequest.class)); + final Optional remote = remotes.stream() + .filter(r -> r.getUrl().equals(url)) + .findFirst(); + if (remote.isPresent()) { + final String remoteBranchName = "refs/remotes/" + remote.get().getName() + '/' + branchName; + final List remoteBranches = git.branchList(newDto(BranchListRequest.class).withListMode(LIST_REMOTE)); + if (remoteBranches.stream().anyMatch(br -> remoteBranchName.equals(br.getName()))) { + return remoteBranchName; + } } - return first.get().getName(); + return null; } }