From 602edc29aa217aab8de3123db0960a668a91a772 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 16 Oct 2012 18:20:15 +0400 Subject: [PATCH] GitBranch: move methods to get the tracking branch to GitBranchUtil, deprecate them. --- plugins/git4idea/src/git4idea/GitBranch.java | 82 ------------------- .../src/git4idea/GitBranchesSearcher.java | 2 +- plugins/git4idea/src/git4idea/GitUtil.java | 2 +- .../src/git4idea/branch/GitBranchUtil.java | 65 ++++++++++++++- .../GitCommittedChangeListProvider.java | 2 +- .../src/git4idea/history/GitHistoryUtils.java | 2 +- .../history/browser/LowLevelAccessImpl.java | 2 +- .../src/git4idea/push/GitPushDialog.java | 6 +- .../src/git4idea/push/GitPushResult.java | 5 +- .../github/GithubOpenInBrowserAction.java | 2 +- 10 files changed, 76 insertions(+), 94 deletions(-) diff --git a/plugins/git4idea/src/git4idea/GitBranch.java b/plugins/git4idea/src/git4idea/GitBranch.java index e8b9934b8c0f..7a17b2a0be9d 100644 --- a/plugins/git4idea/src/git4idea/GitBranch.java +++ b/plugins/git4idea/src/git4idea/GitBranch.java @@ -16,18 +16,13 @@ package git4idea; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; -import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; -import git4idea.config.GitConfigUtil; import git4idea.repo.GitRepositoryFiles; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.IOException; -import java.util.HashMap; /** * This data class represents a Git branch @@ -90,32 +85,6 @@ public class GitBranch extends GitReference { return Pair.create(remoteName, remoteBranchName); } - /** - * Get tracked remote for the branch - * - * @param project the context project - * @param root the VCS root to investigate - * @return the remote name for tracked branch, "." meaning the current repository, or null if no branch is tracked - * @throws VcsException if there is a problem with running Git - */ - @Nullable - public String getTrackedRemoteName(Project project, VirtualFile root) throws VcsException { - return GitConfigUtil.getValue(project, root, trackedRemoteKey()); - } - - /** - * Get tracked the branch - * - * @param project the context project - * @param root the VCS root to investigate - * @return the name of tracked branch - * @throws VcsException if there is a problem with running Git - */ - @Nullable - public String getTrackedBranchName(Project project, VirtualFile root) throws VcsException { - return GitConfigUtil.getValue(project, root, trackedBranchKey()); - } - /** * Checks if the branch exists in the repository. * @return true if the branch exists, false otherwise. @@ -148,55 +117,4 @@ public class GitBranch extends GitReference { public String getHash() { return myHash; } - - /** - * @return the key for the remote of the tracked branch - */ - private String trackedBranchKey() { - return "branch." + getName() + ".merge"; - } - - /** - * @return the key for the tracked branch - */ - private String trackedRemoteKey() { - return "branch." + getName() + ".remote"; - } - - /** - * Get tracked branch for the current branch - * - * @param project the project - * @param root the vcs root - * @return the tracked branch - * @throws VcsException if there is a problem with accessing configuration file - */ - @Nullable - public GitBranch tracked(Project project, VirtualFile root) throws VcsException { - final HashMap result = new HashMap(); - GitConfigUtil.getValues(project, root, null, result); - String remote = result.get(trackedRemoteKey()); - if (remote == null) { - return null; - } - String branch = result.get(trackedBranchKey()); - if (branch == null) { - return null; - } - if (branch.startsWith(REFS_HEADS_PREFIX)) { - branch = branch.substring(REFS_HEADS_PREFIX.length()); - } - else if (branch.startsWith(REFS_REMOTES_PREFIX)) { - branch = branch.substring(REFS_REMOTES_PREFIX.length()); - } - boolean remoteFlag; - if (!".".equals(remote)) { - branch = remote + "/" + branch; - remoteFlag = true; - } - else { - remoteFlag = false; - } - return new GitBranch(branch, remoteFlag); - } } diff --git a/plugins/git4idea/src/git4idea/GitBranchesSearcher.java b/plugins/git4idea/src/git4idea/GitBranchesSearcher.java index 9ff85f025dbc..5c322a346272 100644 --- a/plugins/git4idea/src/git4idea/GitBranchesSearcher.java +++ b/plugins/git4idea/src/git4idea/GitBranchesSearcher.java @@ -40,7 +40,7 @@ public class GitBranchesSearcher { GitBranch remote = myLocal; while (true) { - remote = remote.tracked(project, root); + remote = GitBranchUtil.tracked(project, root, remote.getName()); if (remote == null) { LOG.debug("remote == null, exiting"); return; diff --git a/plugins/git4idea/src/git4idea/GitUtil.java b/plugins/git4idea/src/git4idea/GitUtil.java index bb3fee239efd..fe6855f30a3c 100644 --- a/plugins/git4idea/src/git4idea/GitUtil.java +++ b/plugins/git4idea/src/git4idea/GitUtil.java @@ -704,7 +704,7 @@ public class GitUtil { remote is configured for the current branch). */ - String remoteName = branch.getTrackedRemoteName(repository.getProject(), repository.getRoot()); + String remoteName = GitBranchUtil.getTrackedRemoteName(repository.getProject(), repository.getRoot(), branch.getName()); GitRemote remote; if (remoteName == null) { remote = findOrigin(repository.getRemotes()); diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java index e8f851228ffa..4566570614c4 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java @@ -19,10 +19,13 @@ import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; import git4idea.GitBranch; import git4idea.GitUtil; +import git4idea.config.GitConfigUtil; import git4idea.repo.GitBranchTrackInfo; +import git4idea.repo.GitConfig; import git4idea.repo.GitRemote; import git4idea.repo.GitRepository; import git4idea.ui.branch.GitBranchUiUtil; @@ -31,6 +34,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.HashMap; /** * @author Kirill Likhodedov @@ -106,7 +110,7 @@ public class GitBranchUtil { /** * Returns the current branch in the given repository, or null if either repository is not on the branch, or in case of error. - * @deprecated Use {@link git4idea.repo.GitRepository#getCurrentBranch()} + * @deprecated Use {@link GitRepository#getCurrentBranch()} */ @Deprecated @Nullable @@ -121,4 +125,63 @@ public class GitBranchUtil { return null; } + /** + * Get tracked remote for the branch + */ + @Nullable + public static String getTrackedRemoteName(Project project, VirtualFile root, String branchName) throws VcsException { + return GitConfigUtil.getValue(project, root, trackedRemoteKey(branchName)); + } + + /** + * Get tracked branch of the given branch + */ + @Nullable + public static String getTrackedBranchName(Project project, VirtualFile root, String branchName) throws VcsException { + return GitConfigUtil.getValue(project, root, trackedBranchKey(branchName)); + } + + @NotNull + private static String trackedBranchKey(String branchName) { + return "branch." + branchName + ".merge"; + } + + @NotNull + private static String trackedRemoteKey(String branchName) { + return "branch." + branchName + ".remote"; + } + + /** + * Get the tracked branch for the given branch, or null if the given branch doesn't track anything. + * @deprecated Use {@link GitConfig#getBranchTrackInfos()} + */ + @Deprecated + @Nullable + public static GitBranch tracked(Project project, VirtualFile root, String branchName) throws VcsException { + final HashMap result = new HashMap(); + GitConfigUtil.getValues(project, root, null, result); + String remote = result.get(trackedRemoteKey(branchName)); + if (remote == null) { + return null; + } + String branch = result.get(trackedBranchKey(branchName)); + if (branch == null) { + return null; + } + if (branch.startsWith(GitBranch.REFS_HEADS_PREFIX)) { + branch = branch.substring(GitBranch.REFS_HEADS_PREFIX.length()); + } + else if (branch.startsWith(GitBranch.REFS_REMOTES_PREFIX)) { + branch = branch.substring(GitBranch.REFS_REMOTES_PREFIX.length()); + } + boolean remoteFlag; + if (!".".equals(remote)) { + branch = remote + "/" + branch; + remoteFlag = true; + } + else { + remoteFlag = false; + } + return new GitBranch(branch, remoteFlag); + } } diff --git a/plugins/git4idea/src/git4idea/changes/GitCommittedChangeListProvider.java b/plugins/git4idea/src/git4idea/changes/GitCommittedChangeListProvider.java index 7fd519504377..dc685935e585 100644 --- a/plugins/git4idea/src/git4idea/changes/GitCommittedChangeListProvider.java +++ b/plugins/git4idea/src/git4idea/changes/GitCommittedChangeListProvider.java @@ -83,7 +83,7 @@ public class GitCommittedChangeListProvider implements CommittedChangesProvider< if (c == null) { return null; } - String remote = c.getTrackedRemoteName(myProject, gitRoot); + String remote = GitBranchUtil.getTrackedRemoteName(myProject, gitRoot, c.getName()); if (StringUtil.isEmpty(remote)) { return null; } diff --git a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java index cb705b0ca208..f99c456ddfcf 100644 --- a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java +++ b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java @@ -174,7 +174,7 @@ public class GitHistoryUtils { public static ItemLatestState getLastRevision(final Project project, FilePath filePath) throws VcsException { VirtualFile root = GitUtil.getGitRoot(filePath); GitBranch c = GitBranchUtil.getCurrentBranch(project, root); - GitBranch t = c == null ? null : c.tracked(project, root); + GitBranch t = c == null ? null : GitBranchUtil.tracked(project, root, c.getName()); if (t == null) { return new ItemLatestState(getCurrentRevision(project, filePath, null), true, false); } diff --git a/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java b/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java index 4b6a43b1349d..b558c70717f8 100644 --- a/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java +++ b/plugins/git4idea/src/git4idea/history/browser/LowLevelAccessImpl.java @@ -130,7 +130,7 @@ public class LowLevelAccessImpl implements LowLevelAccess { final GitBranch current = repository.getCurrentBranch(); refs.setCurrentBranch(current); if (current != null) { - GitBranch tracked = current.tracked(myProject, myRoot); + GitBranch tracked = GitBranchUtil.tracked(myProject, myRoot, current.getName()); String fullName = tracked == null ? null : tracked.getFullName(); fullName = fullName != null && fullName.startsWith(GitBranch.REFS_REMOTES_PREFIX) ? fullName.substring(GitBranch.REFS_REMOTES_PREFIX.length()) : fullName; refs.setTrackedRemoteName(fullName); diff --git a/plugins/git4idea/src/git4idea/push/GitPushDialog.java b/plugins/git4idea/src/git4idea/push/GitPushDialog.java index 4e787d474858..ab94390cbc02 100644 --- a/plugins/git4idea/src/git4idea/push/GitPushDialog.java +++ b/plugins/git4idea/src/git4idea/push/GitPushDialog.java @@ -177,7 +177,7 @@ public class GitPushDialog extends DialogWrapper { } String remoteName; try { - remoteName = currentBranch.getTrackedRemoteName(myProject, repository.getRoot()); + remoteName = GitBranchUtil.getTrackedRemoteName(myProject, repository.getRoot(), currentBranch.getName()); if (remoteName == null) { remoteName = DEFAULT_REMOTE; } @@ -225,8 +225,8 @@ public class GitPushDialog extends DialogWrapper { if (currentBranch == null) { continue; } - String remoteName = currentBranch.getTrackedRemoteName(repository.getProject(), repository.getRoot()); - String trackedBranchName = currentBranch.getTrackedBranchName(repository.getProject(), repository.getRoot()); + String remoteName = GitBranchUtil.getTrackedRemoteName(repository.getProject(), repository.getRoot(), currentBranch.getName()); + String trackedBranchName = GitBranchUtil.getTrackedBranchName(repository.getProject(), repository.getRoot(), currentBranch.getName()); GitRemote remote = GitUtil.findRemoteByName(repository, remoteName); GitBranch targetBranch = GitBranchUtil.findRemoteBranchByName(repository, remote, trackedBranchName); if (remote == null || targetBranch == null) { diff --git a/plugins/git4idea/src/git4idea/push/GitPushResult.java b/plugins/git4idea/src/git4idea/push/GitPushResult.java index e8ac07d4f6e1..c82b5ea816cb 100644 --- a/plugins/git4idea/src/git4idea/push/GitPushResult.java +++ b/plugins/git4idea/src/git4idea/push/GitPushResult.java @@ -33,6 +33,7 @@ import git4idea.GitBranch; import git4idea.GitRevisionNumber; import git4idea.GitUtil; import git4idea.GitVcs; +import git4idea.branch.GitBranchUtil; import git4idea.merge.MergeChangeCollector; import git4idea.repo.GitRepository; import org.jetbrains.annotations.NotNull; @@ -191,14 +192,14 @@ class GitPushResult { String trackedBranchName; try { - String simpleName = currentBranch.getTrackedBranchName(myProject, repository.getRoot()); + String simpleName = GitBranchUtil.getTrackedBranchName(myProject, repository.getRoot(), currentBranch.getName()); if (simpleName == null) { continue; } if (simpleName.startsWith(GitBranch.REFS_HEADS_PREFIX)) { simpleName = simpleName.substring(GitBranch.REFS_HEADS_PREFIX.length()); } - String remote = currentBranch.getTrackedRemoteName(myProject, repository.getRoot()); + String remote = GitBranchUtil.getTrackedRemoteName(myProject, repository.getRoot(), currentBranch.getName()); if (remote == null) { continue; } diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserAction.java index 0d8eb2ee377e..88d6066ce7ff 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubOpenInBrowserAction.java @@ -150,7 +150,7 @@ public class GithubOpenInBrowserAction extends DumbAwareAction { Messages.showErrorDialog(project, "Cannot find local branch", CANNOT_OPEN_IN_BROWSER); return null; } - tracked = current.tracked(project, root); + tracked = GitBranchUtil.tracked(project, root, current.getName()); if (tracked == null || !tracked.isRemote()) { Messages.showErrorDialog(project, "Cannot find tracked branch for branch: " + current.getFullName(), CANNOT_OPEN_IN_BROWSER); return null;