GitBranchPopup: preselect the recent branch.

Store recent branch information for common branches and for each repository (preselect such in sub-popups and if branches diverge or not synched).
This commit is contained in:
Kirill Likhodedov
2012-02-10 14:55:39 +04:00
parent a83c894c7d
commit ec583ed072
8 changed files with 86 additions and 10 deletions
@@ -34,6 +34,7 @@ import com.intellij.util.Function;
import com.intellij.util.ui.UIUtil;
import git4idea.*;
import git4idea.commands.GitMessageWithFilesDetector;
import git4idea.config.GitVcsSettings;
import git4idea.merge.GitConflictResolver;
import git4idea.repo.GitRepository;
import git4idea.util.UntrackedFilesNotifier;
@@ -59,18 +60,22 @@ abstract class GitBranchOperation {
@NotNull protected final Project myProject;
@NotNull private final Collection<GitRepository> myRepositories;
@NotNull private final String myCurrentBranchOrRev;
@NotNull private final ProgressIndicator myIndicator;
private final GitVcsSettings mySettings;
@NotNull private final Collection<GitRepository> mySuccessfulRepositories;
@NotNull private final Collection<GitRepository> myRemainingRepositories;
protected GitBranchOperation(@NotNull Project project, @NotNull Collection<GitRepository> repositories,
@NotNull ProgressIndicator indicator) {
@NotNull String currentBranchOrRev, @NotNull ProgressIndicator indicator) {
myProject = project;
myRepositories = repositories;
myCurrentBranchOrRev = currentBranchOrRev;
myIndicator = indicator;
mySuccessfulRepositories = new ArrayList<GitRepository>();
myRemainingRepositories = new ArrayList<GitRepository>(myRepositories);
mySettings = GitVcsSettings.getInstance(myProject);
}
protected abstract void execute();
@@ -242,6 +247,20 @@ abstract class GitBranchOperation {
return pluralize("repository", getSuccessfulRepositories().size());
}
/**
* Updates the recently visited branch in the settings.
* This is to be performed after successful checkout operation.
*/
protected void updateRecentBranch() {
if (getRepositories().size() == 1) {
GitRepository repository = myRepositories.iterator().next();
mySettings.setRecentBranchOfRepository(repository.getRoot().getPath(), myCurrentBranchOrRev);
}
else {
mySettings.setRecentCommonBranch(myCurrentBranchOrRev);
}
}
private void showUnmergedFilesDialogWithRollback() {
final AtomicBoolean ok = new AtomicBoolean();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@@ -44,9 +44,8 @@ class GitCheckoutNewBranchOperation extends GitBranchOperation {
@NotNull private final String myPreviousBranch;
GitCheckoutNewBranchOperation(@NotNull Project project, @NotNull Collection<GitRepository> repositories,
@NotNull String newBranchName, @NotNull String previousBranch,
@NotNull ProgressIndicator indicator) {
super(project, repositories, indicator);
@NotNull String newBranchName, @NotNull String previousBranch, @NotNull ProgressIndicator indicator) {
super(project, repositories, previousBranch, indicator);
myNewBranchName = newBranchName;
myProject = project;
myPreviousBranch = previousBranch;
@@ -77,6 +76,7 @@ class GitCheckoutNewBranchOperation extends GitBranchOperation {
if (!fatalErrorHappened) {
notifySuccess();
updateRecentBranch();
}
}
@@ -59,7 +59,7 @@ class GitCheckoutOperation extends GitBranchOperation {
GitCheckoutOperation(@NotNull Project project, @NotNull Collection<GitRepository> repositories,
@NotNull String startPointReference, @Nullable String newBranch, @NotNull String previousBranch,
@NotNull ProgressIndicator indicator) {
super(project, repositories, indicator);
super(project, repositories, previousBranch, indicator);
myStartPointReference = startPointReference;
myNewBranch = newBranch;
myPreviousBranch = previousBranch;
@@ -104,10 +104,11 @@ class GitCheckoutOperation extends GitBranchOperation {
if (!fatalErrorHappened) {
notifySuccess();
updateRecentBranch();
}
}
private boolean smartCheckoutOrNotify(@NotNull GitRepository repository,
private boolean smartCheckoutOrNotify(@NotNull GitRepository repository,
@NotNull GitMessageWithFilesDetector localChangesOverwrittenByCheckout) {
Pair<List<GitRepository>, List<Change>> conflictingRepositoriesAndAffectedChanges =
getConflictingRepositoriesAndAffectedChanges(repository, localChangesOverwrittenByCheckout, myPreviousBranch, myStartPointReference);
@@ -53,8 +53,8 @@ class GitDeleteBranchOperation extends GitBranchOperation {
private final String myCurrentBranch;
GitDeleteBranchOperation(@NotNull Project project, @NotNull Collection<GitRepository> repositories,
@NotNull String branchName, @NotNull String currentBranch, @NotNull ProgressIndicator indicator) {
super(project, repositories, indicator);
@NotNull String branchName, @NotNull String currentBranch, @NotNull ProgressIndicator indicator) {
super(project, repositories, currentBranch, indicator);
myBranchName = branchName;
myCurrentBranch = currentBranch;
}
@@ -66,7 +66,7 @@ class GitMergeOperation extends GitBranchOperation {
@NotNull String branchToMerge, @NotNull String currentBranch, @NotNull GitRepository currentRepository,
@NotNull Map<GitRepository, String> currentRevisionsBeforeMerge,
@NotNull ProgressIndicator indicator) {
super(project, repositories, indicator);
super(project, repositories, currentBranch, indicator);
myBranchToMerge = branchToMerge;
myCurrentBranch = currentBranch;
myCurrentRepository = currentRepository;
@@ -26,7 +26,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Git VCS settings
@@ -70,6 +72,8 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsSettings.S
public boolean PUSH_AUTO_UPDATE = false;
public GitBranchSyncSetting SYNC_SETTING = GitBranchSyncSetting.NOT_DECIDED;
public String RECENT_GIT_ROOT_PATH = null;
public Map<String, String> RECENT_BRANCH_BY_REPOSITORY = new HashMap<String, String>();
public String RECENT_COMMON_BRANCH = null;
}
public GitVcsSettings(GitVcsApplicationSettings appSettings) {
@@ -168,4 +172,22 @@ public class GitVcsSettings implements PersistentStateComponent<GitVcsSettings.S
myState.RECENT_GIT_ROOT_PATH = recentGitRootPath;
}
@NotNull
public Map<String, String> getRecentBranchesByRepository() {
return myState.RECENT_BRANCH_BY_REPOSITORY;
}
public void setRecentBranchOfRepository(@NotNull String repositoryPath, @NotNull String branch) {
myState.RECENT_BRANCH_BY_REPOSITORY.put(repositoryPath, branch);
}
@Nullable
public String getRecentCommonBranch() {
return myState.RECENT_COMMON_BRANCH;
}
public void setRecentCommonBranch(@NotNull String branch) {
myState.RECENT_COMMON_BRANCH = branch;
}
}
@@ -27,6 +27,7 @@ import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.popup.PopupFactoryImpl;
import com.intellij.ui.popup.WizardPopup;
@@ -88,10 +89,33 @@ class GitBranchPopup {
String title = createPopupTitle(currentRepository);
Condition<AnAction> preselectActionCondition = new Condition<AnAction>() {
@Override
public boolean value(AnAction action) {
if (action instanceof GitBranchPopupActions.LocalBranchActions) {
GitBranchPopupActions.LocalBranchActions branchAction = (GitBranchPopupActions.LocalBranchActions)action;
String branchName = branchAction.getBranchName();
String recentBranch;
List<GitRepository> repositories = branchAction.getRepositories();
if (repositories.size() == 1) {
recentBranch = myVcsSettings.getRecentBranchesByRepository().get(repositories.iterator().next().getRoot().getPath());
}
else {
recentBranch = myVcsSettings.getRecentCommonBranch();
}
if (recentBranch != null && recentBranch.equals(branchName)) {
return true;
}
}
return false;
}
};
myPopup = new PopupFactoryImpl.ActionGroupPopup(
title, createActions(),
SimpleDataContext.getProjectContext(project),
false, false, false, true, null, -1, null, null) {
false, false, false, true, null, -1, preselectActionCondition, null) {
@Override
protected WizardPopup createPopup(WizardPopup parent, PopupStep step, Object parentValue) {
WizardPopup popup = super.createPopup(parent, step, parentValue);
@@ -180,6 +180,16 @@ class GitBranchPopupActions {
}
}
@NotNull
List<GitRepository> getRepositories() {
return myRepositories;
}
@NotNull
public String getBranchName() {
return myBranchName;
}
@NotNull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {