diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java b/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java index 456f6922f096..99a5a01bf2e7 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchOperation.java @@ -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 myRepositories; + @NotNull private final String myCurrentBranchOrRev; @NotNull private final ProgressIndicator myIndicator; + private final GitVcsSettings mySettings; @NotNull private final Collection mySuccessfulRepositories; @NotNull private final Collection myRemainingRepositories; protected GitBranchOperation(@NotNull Project project, @NotNull Collection repositories, - @NotNull ProgressIndicator indicator) { + @NotNull String currentBranchOrRev, @NotNull ProgressIndicator indicator) { myProject = project; myRepositories = repositories; + myCurrentBranchOrRev = currentBranchOrRev; myIndicator = indicator; mySuccessfulRepositories = new ArrayList(); myRemainingRepositories = new ArrayList(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() { diff --git a/plugins/git4idea/src/git4idea/branch/GitCheckoutNewBranchOperation.java b/plugins/git4idea/src/git4idea/branch/GitCheckoutNewBranchOperation.java index 27f135bac9a3..553277e329f4 100644 --- a/plugins/git4idea/src/git4idea/branch/GitCheckoutNewBranchOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitCheckoutNewBranchOperation.java @@ -44,9 +44,8 @@ class GitCheckoutNewBranchOperation extends GitBranchOperation { @NotNull private final String myPreviousBranch; GitCheckoutNewBranchOperation(@NotNull Project project, @NotNull Collection 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(); } } diff --git a/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java b/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java index 6585ce74fd73..e3530b19e040 100644 --- a/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java @@ -59,7 +59,7 @@ class GitCheckoutOperation extends GitBranchOperation { GitCheckoutOperation(@NotNull Project project, @NotNull Collection 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> conflictingRepositoriesAndAffectedChanges = getConflictingRepositoriesAndAffectedChanges(repository, localChangesOverwrittenByCheckout, myPreviousBranch, myStartPointReference); diff --git a/plugins/git4idea/src/git4idea/branch/GitDeleteBranchOperation.java b/plugins/git4idea/src/git4idea/branch/GitDeleteBranchOperation.java index 0c4c89fa3f69..b21d46ffc41b 100644 --- a/plugins/git4idea/src/git4idea/branch/GitDeleteBranchOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitDeleteBranchOperation.java @@ -53,8 +53,8 @@ class GitDeleteBranchOperation extends GitBranchOperation { private final String myCurrentBranch; GitDeleteBranchOperation(@NotNull Project project, @NotNull Collection 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; } diff --git a/plugins/git4idea/src/git4idea/branch/GitMergeOperation.java b/plugins/git4idea/src/git4idea/branch/GitMergeOperation.java index dde3d9a2abac..5009f1ca5400 100644 --- a/plugins/git4idea/src/git4idea/branch/GitMergeOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitMergeOperation.java @@ -66,7 +66,7 @@ class GitMergeOperation extends GitBranchOperation { @NotNull String branchToMerge, @NotNull String currentBranch, @NotNull GitRepository currentRepository, @NotNull Map currentRevisionsBeforeMerge, @NotNull ProgressIndicator indicator) { - super(project, repositories, indicator); + super(project, repositories, currentBranch, indicator); myBranchToMerge = branchToMerge; myCurrentBranch = currentBranch; myCurrentRepository = currentRepository; diff --git a/plugins/git4idea/src/git4idea/config/GitVcsSettings.java b/plugins/git4idea/src/git4idea/config/GitVcsSettings.java index d5d7ef1f20e8..61ccb83bc04a 100644 --- a/plugins/git4idea/src/git4idea/config/GitVcsSettings.java +++ b/plugins/git4idea/src/git4idea/config/GitVcsSettings.java @@ -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 RECENT_BRANCH_BY_REPOSITORY = new HashMap(); + public String RECENT_COMMON_BRANCH = null; } public GitVcsSettings(GitVcsApplicationSettings appSettings) { @@ -168,4 +172,22 @@ public class GitVcsSettings implements PersistentStateComponent 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; + } + } diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java index 715e459f40ac..395488cd219d 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java @@ -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 preselectActionCondition = new Condition() { + @Override + public boolean value(AnAction action) { + if (action instanceof GitBranchPopupActions.LocalBranchActions) { + GitBranchPopupActions.LocalBranchActions branchAction = (GitBranchPopupActions.LocalBranchActions)action; + String branchName = branchAction.getBranchName(); + + String recentBranch; + List 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); diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java index ba4d118967ea..fa6e23495187 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java @@ -180,6 +180,16 @@ class GitBranchPopupActions { } } + @NotNull + List getRepositories() { + return myRepositories; + } + + @NotNull + public String getBranchName() { + return myBranchName; + } + @NotNull @Override public AnAction[] getChildren(@Nullable AnActionEvent e) {