GitBranch: move methods to get the tracking branch to GitBranchUtil, deprecate them.

This commit is contained in:
Kirill Likhodedov
2012-10-18 17:53:57 +04:00
parent e171fdf684
commit 602edc29aa
10 changed files with 76 additions and 94 deletions
@@ -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<String, String> result = new HashMap<String, String>();
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);
}
}
@@ -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;
+1 -1
View File
@@ -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());
@@ -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<String, String> result = new HashMap<String, String>();
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);
}
}
@@ -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;
}
@@ -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);
}
@@ -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);
@@ -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) {
@@ -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;
}
@@ -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;