diff --git a/plugins/git4idea/src/git4idea/GitUtil.java b/plugins/git4idea/src/git4idea/GitUtil.java index ffa394211faf..eca2b0f14503 100644 --- a/plugins/git4idea/src/git4idea/GitUtil.java +++ b/plugins/git4idea/src/git4idea/GitUtil.java @@ -687,8 +687,13 @@ public class GitUtil { @Nullable - public static GitRemote findRemoteByName(@NotNull GitRepository repository, @Nullable final String name) { - return ContainerUtil.find(repository.getRemotes(), new Condition() { + public static GitRemote findRemoteByName(@NotNull GitRepository repository, @NotNull final String name) { + return findRemoteByName(repository.getRemotes(), name); + } + + @Nullable + public static GitRemote findRemoteByName(Collection remotes, @NotNull final String name) { + return ContainerUtil.find(remotes, new Condition() { @Override public boolean value(GitRemote remote) { return remote.getName().equals(name); diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java index 6dfa3f23dd36..b996dea69e5f 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java @@ -30,7 +30,6 @@ import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; -import com.intellij.vcs.log.Hash; import com.intellij.vcsUtil.VcsUtil; import git4idea.*; import git4idea.commands.GitCommand; @@ -209,46 +208,6 @@ public class GitBranchUtil { return remote; } - /** - * - * @return {@link git4idea.GitStandardRemoteBranch} or {@link GitSvnRemoteBranch}, or null in case of an error. The error is logged in this method. - * @deprecated Should be used only in the GitRepositoryReader, i. e. moved there once all other usages are removed. - */ - @Deprecated - @Nullable - public static GitRemoteBranch parseRemoteBranch(@NotNull String fullBranchName, @NotNull Hash hash, - @NotNull Collection remotes) { - String stdName = stripRefsPrefix(fullBranchName); - - int slash = stdName.indexOf('/'); - if (slash == -1) { // .git/refs/remotes/my_branch => git-svn - return new GitSvnRemoteBranch(fullBranchName, hash); - } - else { - String remoteName = stdName.substring(0, slash); - String branchName = stdName.substring(slash + 1); - GitRemote remote = findRemoteByName(remoteName, remotes); - if (remote == null) { - // user may remove the remote section from .git/config, but leave remote refs untouched in .git/refs/remotes - LOG.info(String.format("No remote found with the name [%s]. All remotes: %s", remoteName, remotes)); - GitRemote fakeRemote = new GitRemote(remoteName, ContainerUtil.emptyList(), Collections.emptyList(), - Collections.emptyList(), Collections.emptyList()); - return new GitStandardRemoteBranch(fakeRemote, branchName, hash); - } - return new GitStandardRemoteBranch(remote, branchName, hash); - } - } - - @Nullable - private static GitRemote findRemoteByName(@NotNull String remoteName, @NotNull Collection remotes) { - for (GitRemote remote : remotes) { - if (remote.getName().equals(remoteName)) { - return remote; - } - } - return null; - } - /** * Convert {@link git4idea.GitRemoteBranch GitRemoteBranches} to their names, and remove remote HEAD pointers: origin/HEAD. */ diff --git a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java index 576f326a8ea3..66ce94172794 100644 --- a/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java +++ b/plugins/git4idea/src/git4idea/repo/GitRepositoryReader.java @@ -25,9 +25,7 @@ import com.intellij.util.Processor; import com.intellij.util.containers.ContainerUtil; import com.intellij.vcs.log.Hash; import com.intellij.vcs.log.impl.HashImpl; -import git4idea.GitBranch; -import git4idea.GitLocalBranch; -import git4idea.GitRemoteBranch; +import git4idea.*; import git4idea.branch.GitBranchUtil; import git4idea.branch.GitBranchesCollection; import org.jetbrains.annotations.NonNls; @@ -349,7 +347,7 @@ class GitRepositoryReader { String hash = loadHashFromBranchFile(file); Hash h = createHash(hash); if (h != null) { - GitRemoteBranch remoteBranch = GitBranchUtil.parseRemoteBranch(branchName, h, remotes); + GitRemoteBranch remoteBranch = parseRemoteBranch(branchName, h, remotes); if (remoteBranch != null) { branches.add(remoteBranch); } @@ -386,7 +384,7 @@ class GitRepositoryReader { localBranches.add(new GitLocalBranch(branchName, hash)); } else if (branchName.startsWith(REFS_REMOTES_PREFIX)) { - GitRemoteBranch remoteBranch = GitBranchUtil.parseRemoteBranch(branchName, hash, remotes); + GitRemoteBranch remoteBranch = parseRemoteBranch(branchName, hash, remotes); if (remoteBranch != null) { remoteBranches.add(remoteBranch); } @@ -395,6 +393,31 @@ class GitRepositoryReader { return new GitBranchesCollection(localBranches, remoteBranches); } + @Nullable + private static GitRemoteBranch parseRemoteBranch(@NotNull String fullBranchName, + @NotNull Hash hash, + @NotNull Collection remotes) { + String stdName = GitBranchUtil.stripRefsPrefix(fullBranchName); + + int slash = stdName.indexOf('/'); + if (slash == -1) { // .git/refs/remotes/my_branch => git-svn + return new GitSvnRemoteBranch(fullBranchName, hash); + } + else { + String remoteName = stdName.substring(0, slash); + String branchName = stdName.substring(slash + 1); + GitRemote remote = GitUtil.findRemoteByName(remotes, remoteName); + if (remote == null) { + // user may remove the remote section from .git/config, but leave remote refs untouched in .git/refs/remotes + LOG.debug(String.format("No remote found with the name [%s]. All remotes: %s", remoteName, remotes)); + GitRemote fakeRemote = new GitRemote(remoteName, ContainerUtil.emptyList(), Collections.emptyList(), + Collections.emptyList(), Collections.emptyList()); + return new GitStandardRemoteBranch(fakeRemote, branchName, hash); + } + return new GitStandardRemoteBranch(remote, branchName, hash); + } + } + @NotNull private static String readBranchFile(@NotNull File branchFile) { return RepositoryUtil.tryLoadFile(branchFile);