From 5a3817a5cea96d54be7eb0a4d305900a1fdf559f Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Wed, 17 Jan 2018 16:10:43 +0300 Subject: [PATCH] git: IDEA-145325 Show Rebase actions at the top of Branch popup * show action if there is an ongoing rebase process; * extract per repository rebase action; --- .../actions/GitAbstractRebaseAction.java | 22 ++++---- .../git4idea/ui/branch/GitBranchPopup.java | 10 +++- .../ui/branch/GitBranchPopupActions.java | 51 +++++++++++++++++++ 3 files changed, 71 insertions(+), 12 deletions(-) diff --git a/plugins/git4idea/src/git4idea/actions/GitAbstractRebaseAction.java b/plugins/git4idea/src/git4idea/actions/GitAbstractRebaseAction.java index 952724556e93..9e1e234fe256 100644 --- a/plugins/git4idea/src/git4idea/actions/GitAbstractRebaseAction.java +++ b/plugins/git4idea/src/git4idea/actions/GitAbstractRebaseAction.java @@ -37,7 +37,7 @@ import static com.intellij.util.ObjectUtils.assertNotNull; import static com.intellij.util.containers.ContainerUtil.newArrayList; import static git4idea.GitUtil.*; -abstract class GitAbstractRebaseAction extends DumbAwareAction { +public abstract class GitAbstractRebaseAction extends DumbAwareAction { @Override public void update(@NotNull AnActionEvent e) { @@ -54,10 +54,8 @@ abstract class GitAbstractRebaseAction extends DumbAwareAction { @Override public final void actionPerformed(AnActionEvent e) { final Project project = e.getRequiredData(CommonDataKeys.PROJECT); - ProgressManager progressManager = ProgressManager.getInstance(); - String progressTitle = getProgressTitle(); if (getRepositoryManager(project).hasOngoingRebase()) { - progressManager.run(new Task.Backgroundable(project, progressTitle) { + ProgressManager.getInstance().run(new Task.Backgroundable(project, getProgressTitle()) { @Override public void run(@NotNull ProgressIndicator indicator) { performActionForProject(project, indicator); @@ -67,16 +65,20 @@ abstract class GitAbstractRebaseAction extends DumbAwareAction { else { final GitRepository repositoryToOperate = chooseRepository(project, GitRebaseUtils.getRebasingRepositories(project)); if (repositoryToOperate != null) { - progressManager.run(new Task.Backgroundable(project, progressTitle) { - @Override - public void run(@NotNull ProgressIndicator indicator) { - performActionForRepository(project, repositoryToOperate, indicator); - } - }); + performInBackground(repositoryToOperate); } } } + public void performInBackground(@NotNull GitRepository repositoryToOperate) { + ProgressManager.getInstance().run(new Task.Backgroundable(repositoryToOperate.getProject(), getProgressTitle()) { + @Override + public void run(@NotNull ProgressIndicator indicator) { + performActionForRepository(repositoryToOperate.getProject(), repositoryToOperate, indicator); + } + }); + } + @NotNull protected abstract String getProgressTitle(); diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java index 466051b5ab60..30275897861e 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopup.java @@ -27,8 +27,8 @@ import com.intellij.openapi.actionSystem.EmptyAction; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Condition; import com.intellij.util.containers.ContainerUtil; -import git4idea.GitUtil; import git4idea.config.GitVcsSettings; +import git4idea.rebase.GitRebaseSpec; import git4idea.repo.GitRepository; import git4idea.repo.GitRepositoryManager; import org.jetbrains.annotations.NotNull; @@ -44,6 +44,7 @@ import static com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR; import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfTopShownBranches; import static com.intellij.util.ObjectUtils.tryCast; import static com.intellij.util.containers.ContainerUtil.map; +import static git4idea.GitUtil.getRepositoryManager; import static git4idea.branch.GitBranchUtil.getDisplayableBranchText; import static java.util.stream.Collectors.toList; @@ -84,7 +85,7 @@ class GitBranchPopup extends DvcsBranchPopup { } return false; }; - return new GitBranchPopup(currentRepository, GitUtil.getRepositoryManager(project), vcsSettings, preselectActionCondition); + return new GitBranchPopup(currentRepository, getRepositoryManager(project), vcsSettings, preselectActionCondition); } @Nullable @@ -106,6 +107,11 @@ class GitBranchPopup extends DvcsBranchPopup { protected void fillWithCommonRepositoryActions(@NotNull DefaultActionGroup popupGroup, @NotNull AbstractRepositoryManager repositoryManager) { List allRepositories = repositoryManager.getRepositories(); + GitRebaseSpec rebaseSpec = getRepositoryManager(myProject).getOngoingRebaseSpec(); + // add rebase actions only if sync rebase action is in progress for all repos + if (rebaseSpec != null && rebaseSpec.getAllRepositories().size() == allRepositories.size()) { + popupGroup.addAll(GitBranchPopupActions.getRebaseActions()); + } popupGroup.add(new GitBranchPopupActions.GitNewBranchAction(myProject, allRepositories)); popupGroup.add(new GitBranchPopupActions.CheckoutRevisionActions(myProject, allRepositories)); diff --git a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java index d121b583a66d..e4dd5fcbbdd7 100644 --- a/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java +++ b/plugins/git4idea/src/git4idea/ui/branch/GitBranchPopupActions.java @@ -28,27 +28,33 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.containers.ContainerUtil; import git4idea.GitBranch; import git4idea.GitLocalBranch; +import git4idea.actions.GitAbstractRebaseAction; import git4idea.branch.GitBranchUtil; import git4idea.branch.GitBrancher; import git4idea.branch.GitBranchesCollection; import git4idea.branch.GitNewBranchOptions; +import git4idea.rebase.GitRebaseSpec; import git4idea.repo.GitRepository; +import git4idea.repo.GitRepositoryManager; import git4idea.validators.GitNewBranchNameValidator; import icons.DvcsImplIcons; import one.util.streamex.StreamEx; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collection; import java.util.Collections; import java.util.List; import static com.intellij.dvcs.ui.BranchActionGroupPopup.wrapWithMoreActionIfNeeded; import static com.intellij.dvcs.ui.BranchActionUtil.FAVORITE_BRANCH_COMPARATOR; import static com.intellij.dvcs.ui.BranchActionUtil.getNumOfTopShownBranches; +import static com.intellij.util.ObjectUtils.notNull; import static git4idea.GitStatisticsCollectorKt.reportUsage; import static git4idea.GitUtil.HEAD; import static git4idea.branch.GitBranchType.LOCAL; import static git4idea.branch.GitBranchType.REMOTE; +import static java.util.Arrays.asList; class GitBranchPopupActions { @@ -68,6 +74,12 @@ class GitBranchPopupActions { DefaultActionGroup popupGroup = new DefaultActionGroup(null, false); List repositoryList = Collections.singletonList(myRepository); + if (myRepository.isRebaseInProgress()) { + GitRebaseSpec rebaseSpec = GitRepositoryManager.getInstance(myProject).getOngoingRebaseSpec(); + popupGroup.addAll( + rebaseSpec != null && isSpecForRepo(rebaseSpec, myRepository) ? getRebaseActions() : createPerRepoRebaseActions(myRepository)); + } + popupGroup.addAction(new GitNewBranchAction(myProject, repositoryList)); popupGroup.addAction(new CheckoutRevisionActions(myProject, repositoryList)); @@ -107,6 +119,45 @@ class GitBranchPopupActions { return popupGroup; } + private static boolean isSpecForRepo(@NotNull GitRebaseSpec spec, @NotNull GitRepository repository) { + Collection repositoriesFromSpec = spec.getAllRepositories(); + return repositoriesFromSpec.size() == 1 && repository.equals(ContainerUtil.getFirstItem(repositoriesFromSpec)); + } + + @NotNull + private static List createPerRepoRebaseActions(@NotNull GitRepository repository) { + return asList(createRepositoryRebaseAction("Git.Rebase.Abort", repository), + createRepositoryRebaseAction("Git.Rebase.Continue", repository), + createRepositoryRebaseAction("Git.Rebase.Skip", repository)); + } + + @NotNull + static List getRebaseActions() { + ActionManager actionManager = ActionManager.getInstance(); + return asList(actionManager.getAction("Git.Rebase.Abort"), + actionManager.getAction("Git.Rebase.Continue"), + actionManager.getAction("Git.Rebase.Skip")); + } + + @NotNull + private static AnAction createRepositoryRebaseAction(@NotNull String rebaseActionId, @NotNull GitRepository repository) { + GitAbstractRebaseAction rebaseAction = notNull((GitAbstractRebaseAction)ActionManager.getInstance().getAction(rebaseActionId)); + DumbAwareAction repositoryAction = new DumbAwareAction() { + + @Override + public void update(AnActionEvent e) { + e.getPresentation().setEnabledAndVisible(repository.isRebaseInProgress()); + } + + @Override + public void actionPerformed(AnActionEvent e) { + rebaseAction.performInBackground(repository); + } + }; + repositoryAction.getTemplatePresentation().copyFrom(rebaseAction.getTemplatePresentation()); + return repositoryAction; + } + public static class GitNewBranchAction extends NewBranchAction { public GitNewBranchAction(@NotNull Project project, @NotNull List repositories) {