From b719e2241d6599b472f9d680f28ade46a641c704 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sun, 16 Jun 2019 11:23:24 +0100 Subject: [PATCH] git: replace '--preserve-merges' with '--rebase -merges' for Git >= 2.22: IDEA-216247 Not only the parameter became deprecated, but also a custom core.commentChar is not supported with '--preserve-merges' anymore. GitOrigin-RevId: 24c336e752935cbbc69cb91f50d02dfc943c0669 --- .../src/git4idea/branch/GitBranchWorker.java | 7 ++++-- .../src/git4idea/branch/GitRebaseParams.kt | 24 +++++++++++++------ .../git4idea/config/GitVersionSpecialty.java | 9 +++++-- .../rebase/GitInteractiveRebaseAction.kt | 2 +- .../rebase/GitRebaseCheckinHandlerFactory.kt | 2 +- .../src/git4idea/rebase/GitRebaseDialog.java | 3 ++- .../src/git4idea/rebase/GitRewordOperation.kt | 3 ++- .../merge/GitMergeProviderTestCase.kt | 2 +- .../git4idea/rebase/GitMultiRepoRebaseTest.kt | 2 +- .../rebase/GitSingleRepoRebaseTest.kt | 6 ++--- 10 files changed, 40 insertions(+), 20 deletions(-) diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchWorker.java b/plugins/git4idea/src/git4idea/branch/GitBranchWorker.java index 9210ab119047..ecbb4d93bf70 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchWorker.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchWorker.java @@ -22,6 +22,7 @@ import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.changes.Change; import com.intellij.util.containers.ContainerUtil; import git4idea.GitLocalBranch; +import git4idea.GitVcs; import git4idea.changes.GitChangeUtils; import git4idea.commands.Git; import git4idea.rebase.GitRebaseUtils; @@ -44,11 +45,13 @@ public final class GitBranchWorker { @NotNull private final Project myProject; @NotNull private final Git myGit; @NotNull private final GitBranchUiHandler myUiHandler; + @NotNull private final GitVcs myVcs; public GitBranchWorker(@NotNull Project project, @NotNull Git git, @NotNull GitBranchUiHandler uiHandler) { myProject = project; myGit = git; myUiHandler = uiHandler; + myVcs = GitVcs.getInstance(myProject); } public void checkoutNewBranch(@NotNull final String name, @NotNull List repositories) { @@ -117,12 +120,12 @@ public final class GitBranchWorker { public void rebase(@NotNull List repositories, @NotNull String branchName) { updateInfo(repositories); - GitRebaseUtils.rebase(myProject, repositories, new GitRebaseParams(branchName), myUiHandler.getProgressIndicator()); + GitRebaseUtils.rebase(myProject, repositories, new GitRebaseParams(myVcs.getVersion(), branchName), myUiHandler.getProgressIndicator()); } public void rebaseOnCurrent(@NotNull List repositories, @NotNull String branchName) { updateInfo(repositories); - GitRebaseUtils.rebase(myProject, repositories, new GitRebaseParams(branchName, null, "HEAD", false, false), + GitRebaseUtils.rebase(myProject, repositories, new GitRebaseParams(myVcs.getVersion(), branchName, null, "HEAD", false, false), myUiHandler.getProgressIndicator()); } diff --git a/plugins/git4idea/src/git4idea/branch/GitRebaseParams.kt b/plugins/git4idea/src/git4idea/branch/GitRebaseParams.kt index 8eccc89a4176..06bfa0d29801 100644 --- a/plugins/git4idea/src/git4idea/branch/GitRebaseParams.kt +++ b/plugins/git4idea/src/git4idea/branch/GitRebaseParams.kt @@ -1,9 +1,12 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package git4idea.branch +import git4idea.config.GitVersion +import git4idea.config.GitVersionSpecialty import git4idea.rebase.GitRebaseEditorHandler -class GitRebaseParams private constructor(branch: String?, +class GitRebaseParams private constructor(private val version: GitVersion, + branch: String?, newBase: String?, val upstream: String, val interactive: Boolean, @@ -11,11 +14,12 @@ class GitRebaseParams private constructor(branch: String?, private val autoSquash: AutoSquashOption, val editorHandler: GitRebaseEditorHandler? = null) { companion object { - fun editCommits(base: String, + fun editCommits(version: GitVersion, + base: String, editorHandler: GitRebaseEditorHandler?, preserveMerges: Boolean, autoSquash: AutoSquashOption = AutoSquashOption.DEFAULT): GitRebaseParams = - GitRebaseParams(null, null, base, true, preserveMerges, autoSquash, editorHandler) + GitRebaseParams(version, null, null, base, true, preserveMerges, autoSquash, editorHandler) } enum class AutoSquashOption { @@ -27,20 +31,26 @@ class GitRebaseParams private constructor(branch: String?, val branch: String? = branch?.takeIf { it.isNotBlank() } val newBase: String? = newBase?.takeIf { it.isNotBlank() } - constructor(upstream: String) : this(null, null, upstream, false, false) + constructor(version: GitVersion, upstream: String) : this(version, null, null, upstream, false, false) - constructor(branch: String?, + constructor(version: GitVersion, + branch: String?, newBase: String?, upstream: String, interactive: Boolean, - preserveMerges: Boolean) : this(branch, newBase, upstream, interactive, preserveMerges, AutoSquashOption.DEFAULT) + preserveMerges: Boolean) : this(version, branch, newBase, upstream, interactive, preserveMerges, AutoSquashOption.DEFAULT) fun asCommandLineArguments(): List = mutableListOf().apply { if (interactive) { add("--interactive") } if (preserveMerges) { - add("--preserve-merges") + if (GitVersionSpecialty.REBASE_MERGES_REPLACES_PRESERVE_MERGES.existsIn(version)) { + add("--rebase-merges") + } + else { + add("--preserve-merges") + } } when (autoSquash) { AutoSquashOption.DEFAULT -> { diff --git a/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java b/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java index 3560a22bc8bc..077bbc4bf083 100644 --- a/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java +++ b/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java @@ -22,8 +22,6 @@ import org.jetbrains.annotations.NotNull; * } * } *

- * - * @author Kirill Likhodedov */ public enum GitVersionSpecialty { @@ -243,6 +241,13 @@ public enum GitVersionSpecialty { public boolean existsIn(@NotNull GitVersion version) { return version.isLaterOrEqual(new GitVersion(2, 2, 0, 0)); } + }, + + REBASE_MERGES_REPLACES_PRESERVE_MERGES { + @Override + public boolean existsIn(@NotNull GitVersion version) { + return version.isLaterOrEqual(new GitVersion(2, 22, 0, 0)); + } }; public abstract boolean existsIn(@NotNull GitVersion version); diff --git a/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseAction.kt b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseAction.kt index af0a38287fdf..6a81cc8529e2 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseAction.kt +++ b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseAction.kt @@ -33,7 +33,7 @@ class GitInteractiveRebaseAction : GitCommitEditingAction() { object : Task.Backgroundable(project, "Rebasing") { override fun run(indicator: ProgressIndicator) { - val params = GitRebaseParams.editCommits(commit.parents.first().asString(), null, false) + val params = GitRebaseParams.editCommits(repository.vcs.version, commit.parents.first().asString(), null, false) GitRebaseUtils.rebase(project, listOf(repository), params, indicator); } }.queue() diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseCheckinHandlerFactory.kt b/plugins/git4idea/src/git4idea/rebase/GitRebaseCheckinHandlerFactory.kt index 45a37c672a2d..4dba55157318 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseCheckinHandlerFactory.kt +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseCheckinHandlerFactory.kt @@ -26,7 +26,7 @@ class GitRebaseCheckinHandlerFactory : VcsCheckinHandlerFactory(GitVcs.getKey()) if (!active) return object : Task.Backgroundable(project, "Rebasing") { override fun run(indicator: ProgressIndicator) { - val params = GitRebaseParams.editCommits(rebaseFrom, null, false) + val params = GitRebaseParams.editCommits(repository.vcs.version, rebaseFrom, null, false) GitRebaseUtils.rebase(project, listOf(repository), params, indicator) } }.queue() diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseDialog.java b/plugins/git4idea/src/git4idea/rebase/GitRebaseDialog.java index 7710befd2ec7..2fa537601d52 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseDialog.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseDialog.java @@ -404,7 +404,8 @@ public class GitRebaseDialog extends DialogWrapper { newBase = onto; } - return new GitRebaseParams(branch, newBase, upstream, myInteractiveCheckBox.isSelected(), myPreserveMergesCheckBox.isSelected()); + return new GitRebaseParams(GitVcs.getInstance(myProject).getVersion(), + branch, newBase, upstream, myInteractiveCheckBox.isSelected(), myPreserveMergesCheckBox.isSelected()); } /** diff --git a/plugins/git4idea/src/git4idea/rebase/GitRewordOperation.kt b/plugins/git4idea/src/git4idea/rebase/GitRewordOperation.kt index c2595afd06c9..6416d4dcfb82 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRewordOperation.kt +++ b/plugins/git4idea/src/git4idea/rebase/GitRewordOperation.kt @@ -76,7 +76,8 @@ class GitRewordOperation(private val repository: GitRepository, entriesEditor = { list -> injectRewordAction(list) }, plainTextEditor = { editorText -> supplyNewMessage(editorText) }) - val params = GitRebaseParams.editCommits(commit.parents.first().asString(), rebaseEditor, true, + val params = GitRebaseParams.editCommits(repository.vcs.version, + commit.parents.first().asString(), rebaseEditor, true, GitRebaseParams.AutoSquashOption.DISABLE) val indicator = ProgressManager.getInstance().progressIndicator ?: EmptyProgressIndicator() val spec = GitRebaseSpec.forNewRebase(project, params, listOf(repository), indicator) diff --git a/plugins/git4idea/tests/git4idea/merge/GitMergeProviderTestCase.kt b/plugins/git4idea/tests/git4idea/merge/GitMergeProviderTestCase.kt index 5df2c4ed583c..83b22e67ee72 100644 --- a/plugins/git4idea/tests/git4idea/merge/GitMergeProviderTestCase.kt +++ b/plugins/git4idea/tests/git4idea/merge/GitMergeProviderTestCase.kt @@ -98,7 +98,7 @@ abstract class GitMergeProviderTestCase : GitPlatformTest() { else s.replace("pick", "reword") }.joinToString(LineSeparator.getSystemLineSeparator().separatorString) }, null)) - val rebaseParams = GitRebaseParams(null, null, "branch-$onto", true, false) + val rebaseParams = GitRebaseParams(vcs.version, null, null, "branch-$onto", true, false) git.rebase(repository, rebaseParams) } diff --git a/plugins/git4idea/tests/git4idea/rebase/GitMultiRepoRebaseTest.kt b/plugins/git4idea/tests/git4idea/rebase/GitMultiRepoRebaseTest.kt index 4e56c5a75421..48906008a5e0 100644 --- a/plugins/git4idea/tests/git4idea/rebase/GitMultiRepoRebaseTest.kt +++ b/plugins/git4idea/tests/git4idea/rebase/GitMultiRepoRebaseTest.kt @@ -263,7 +263,7 @@ class GitMultiRepoRebaseTest : GitRebaseBaseTest() { } private fun rebase(onto: String) { - GitTestingRebaseProcess(project, GitRebaseParams(onto), allRepositories).rebase() + GitTestingRebaseProcess(project, GitRebaseParams(vcs.version, onto), allRepositories).rebase() } private fun abortOngoingRebase() { diff --git a/plugins/git4idea/tests/git4idea/rebase/GitSingleRepoRebaseTest.kt b/plugins/git4idea/tests/git4idea/rebase/GitSingleRepoRebaseTest.kt index af1f198d557b..1484e9669b9c 100644 --- a/plugins/git4idea/tests/git4idea/rebase/GitSingleRepoRebaseTest.kt +++ b/plugins/git4idea/tests/git4idea/rebase/GitSingleRepoRebaseTest.kt @@ -443,7 +443,7 @@ class GitSingleRepoRebaseTest : GitRebaseBaseTest() { list }, plainTextEditor = { it }) - GitTestingRebaseProcess(project, GitRebaseParams.editCommits("HEAD^", rebaseEditor, false), repo).rebase() + GitTestingRebaseProcess(project, GitRebaseParams.editCommits(vcs.version, "HEAD^", rebaseEditor, false), repo).rebase() assertNotNull("Didn't get any rebase entries", receivedEntries) assertEquals("Rebase entries parsed incorrectly", listOf(GitRebaseEntry.Action.PICK), receivedEntries!!.map { it.action }) @@ -514,7 +514,7 @@ class GitSingleRepoRebaseTest : GitRebaseBaseTest() { } private fun rebaseInteractively(revision: String = "master") { - GitTestingRebaseProcess(project, GitRebaseParams(null, null, revision, true, false), repo).rebase() + GitTestingRebaseProcess(project, GitRebaseParams(vcs.version, null, null, revision, true, false), repo).rebase() } fun `test checkout with rebase`() { @@ -539,7 +539,7 @@ class GitSingleRepoRebaseTest : GitRebaseBaseTest() { } private fun simpleParams(newBase: String): GitRebaseParams { - return GitRebaseParams(newBase) + return GitRebaseParams(vcs.version, newBase) } internal fun file(path: String) = repo.file(path)