From 269a7a6ce7e7ab5e0367764999dda6b8f227da3c Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Thu, 7 Mar 2024 22:07:04 +0100 Subject: [PATCH] [git] allow to call "Rebase..." action when the HEAD is detached IDEA-195601 GitOrigin-RevId: b53f5adfc6a1bc7375a0e09d68313e6c98673ccb --- plugins/git4idea/resources/messages/GitBundle.properties | 2 -- plugins/git4idea/src/git4idea/actions/GitRebase.java | 2 +- .../git4idea/src/git4idea/rebase/GitRebaseProcess.java | 8 +++++--- plugins/git4idea/src/git4idea/rebase/GitRebaseUtils.java | 6 +----- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/plugins/git4idea/resources/messages/GitBundle.properties b/plugins/git4idea/resources/messages/GitBundle.properties index 7e6a99408b22..936a136a0207 100644 --- a/plugins/git4idea/resources/messages/GitBundle.properties +++ b/plugins/git4idea/resources/messages/GitBundle.properties @@ -299,8 +299,6 @@ rebase.notification.failed.stash.text=Could not stash local uncommitted changes: rebase.notification.no.rebase.in.progress.abort.title=Cannot abort rebase rebase.notification.no.rebase.in.progress.continue.title=Cannot continue rebase rebase.notification.no.rebase.in.progress.message=No rebase in progress -rebase.notification.not.allowed.detached.message.first=You are in the detached HEAD state in {0}. -rebase.notification.not.allowed.detached.message.second=Rebase is not possible. rebase.notification.not.allowed.empty.repository.message=Repository {0} is empty. rebase.notification.not.allowed.grafting.message.first=There is an unfinished cherry-pick process in {0}. rebase.notification.not.allowed.grafting.message.second=You should finish it before starting a rebase. diff --git a/plugins/git4idea/src/git4idea/actions/GitRebase.java b/plugins/git4idea/src/git4idea/actions/GitRebase.java index ac0024f4fa22..29d866086e77 100644 --- a/plugins/git4idea/src/git4idea/actions/GitRebase.java +++ b/plugins/git4idea/src/git4idea/actions/GitRebase.java @@ -39,7 +39,7 @@ public class GitRebase extends DumbAwareAction { if (project == null || !hasGitRepositories(project) || !getRebasingRepositories(project).isEmpty()) { presentation.setEnabledAndVisible(false); } - else if (getRepositoriesInStates(project, Repository.State.NORMAL).isEmpty()) { + else if (getRepositoriesInStates(project, Repository.State.NORMAL, Repository.State.DETACHED).isEmpty()) { presentation.setEnabled(false); } else { diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseProcess.java b/plugins/git4idea/src/git4idea/rebase/GitRebaseProcess.java index feb28cf5ac9c..a13b45a7963f 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseProcess.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseProcess.java @@ -54,6 +54,7 @@ import java.util.regex.Pattern; import static com.intellij.dvcs.DvcsUtil.getShortRepositoryName; import static com.intellij.openapi.ui.Messages.getWarningIcon; import static com.intellij.openapi.vcs.VcsNotifier.IMPORTANT_ERROR_NOTIFICATION; +import static com.intellij.util.ObjectUtils.chooseNotNull; import static com.intellij.util.ObjectUtils.notNull; import static com.intellij.util.containers.ContainerUtil.*; import static git4idea.GitActionIdsHolder.Id.*; @@ -492,11 +493,12 @@ public class GitRebaseProcess { String upstream = myRebaseSpec.getParams().getUpstream(); for (GitRepository repository : myRebaseSpec.getAllRepositories()) { - if (repository.getCurrentBranchName() == null) { - LOG.error("No current branch in " + repository); + String currentBranchName = chooseNotNull(repository.getCurrentBranchName(), repository.getCurrentRevision()); + if (currentBranchName == null) { + LOG.error("No current branch or revision in " + repository); return true; } - String rebasingBranch = notNull(myRebaseSpec.getParams().getBranch(), repository.getCurrentBranchName()); + String rebasingBranch = notNull(myRebaseSpec.getParams().getBranch(), currentBranchName); if (isRebasingPublishedCommit(repository, upstream, rebasingBranch)) { return askIfShouldRebasePublishedCommit(); } diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseUtils.java b/plugins/git4idea/src/git4idea/rebase/GitRebaseUtils.java index 7a4ec9deda17..a38a6dde8437 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseUtils.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseUtils.java @@ -132,7 +132,7 @@ public final class GitRebaseUtils { Repository.State state = repository.getState(); String repositoryName = getShortRepositoryName(repository); String message = switch (state) { - case NORMAL -> { + case NORMAL, DETACHED -> { if (repository.isFresh()) { yield GitBundle.message("rebase.notification.not.allowed.empty.repository.message", repositoryName); } @@ -154,10 +154,6 @@ public final class GitRebaseUtils { .append(GitBundle.message("rebase.notification.not.allowed.reverting.message.first", repositoryName)).br() .append(GitBundle.message("rebase.notification.not.allowed.reverting.message.second")) .toString(); - case DETACHED -> new HtmlBuilder() - .append(GitBundle.message("rebase.notification.not.allowed.detached.message.first", repositoryName)).br() - .append(GitBundle.message("rebase.notification.not.allowed.detached.message.second")) - .toString(); }; if (message != null) { VcsNotifier.getInstance(project).notifyError(