diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaser.java b/plugins/git4idea/src/git4idea/rebase/GitRebaser.java index d6c36020ed1a..365721520750 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaser.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaser.java @@ -7,11 +7,9 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vcs.ProjectLevelVcsManager; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.VcsNotifier; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.VirtualFile; import git4idea.GitUtil; import git4idea.GitVcs; @@ -21,15 +19,9 @@ import git4idea.update.GitUpdateResult; import git4idea.util.GitUIUtil; import git4idea.util.GitUntrackedFilesHelper; import git4idea.util.LocalChangesWouldBeOverwrittenHelper; -import git4idea.util.StringScanner; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -154,36 +146,6 @@ public class GitRebaser { return rebasingRoots; } - /** - * Reorders commits so that the given commits go before others, just after the given parentCommit. - * For example, if A->B->C->D are unpushed commits and B and D are supplied to this method, then after rebase the commits will - * look like that: B->D->A->C. - * NB: If there are merges in the unpushed commits being reordered, a conflict would happen. The calling code should probably - * prohibit reordering merge commits. - */ - public boolean reoderCommitsIfNeeded(@NotNull final VirtualFile root, @NotNull String parentCommit, @NotNull List olderCommits) { - List allCommits = new ArrayList<>(); //TODO - if (olderCommits.isEmpty() || olderCommits.size() == allCommits.size()) { - LOG.info("Nothing to reorder. olderCommits: " + olderCommits + " allCommits: " + allCommits); - return true; - } - - final GitLineHandler h = new GitLineHandler(myProject, root, GitCommand.REBASE); - h.setStdoutSuppressed(false); - h.addParameters("-i", "-m", "-v"); - h.addParameters(parentCommit); - - final GitRebaseProblemDetector rebaseConflictDetector = new GitRebaseProblemDetector(); - h.addLineListener(rebaseConflictDetector); - - final PushRebaseEditor pushRebaseEditor = new PushRebaseEditor(root, olderCommits, false); - try (GitHandlerRebaseEditorManager ignored = GitHandlerRebaseEditorManager.prepareEditor(h, pushRebaseEditor)) { - final GitTask rebaseTask = new GitTask(myProject, h, "Reordering commits"); - rebaseTask.setProgressIndicator(myProgressIndicator); - return executeRebaseTaskInBackground(root, h, rebaseConflictDetector, rebaseTask); - } - } - private boolean executeRebaseTaskInBackground(VirtualFile root, GitLineHandler h, GitRebaseProblemDetector rebaseConflictDetector, GitTask rebaseTask) { final AtomicBoolean result = new AtomicBoolean(); final AtomicBoolean failure = new AtomicBoolean(); @@ -338,71 +300,4 @@ public class GitRebaser { return myRebaser.continueRebase(myRoot); } } - - /** - * The rebase editor that just overrides the list of commits - */ - class PushRebaseEditor extends GitInteractiveRebaseEditorHandler { - private final Logger LOG = Logger.getInstance(PushRebaseEditor.class); - private final List myCommits; // The reordered commits - private final boolean myHasMerges; // true means that the root has merges - - /** - * The constructor from fields that is expected to be - * accessed only from {@link GitRebaseEditorService}. - * - * @param rebaseEditorService - * @param root the git repository root - * @param commits the reordered commits - * @param hasMerges if true, the vcs root has merges - */ - PushRebaseEditor(final VirtualFile root, - List commits, - boolean hasMerges) { - super(myProject, root); - myCommits = commits; - myHasMerges = hasMerges; - } - - @Override - public int editCommits(@NotNull String path) { - if (!myRebaseEditorShown) { - myRebaseEditorShown = true; - if (myHasMerges) { - return 0; - } - try { - TreeMap pickLines = new TreeMap<>(); - StringScanner s = new StringScanner(new String(FileUtil.loadFileText(new File(path), CharsetToolkit.UTF8))); - while (s.hasMoreData()) { - if (!s.tryConsume("pick ")) { - s.line(); - continue; - } - String commit = s.spaceToken(); - pickLines.put(commit, "pick " + commit + " " + s.line()); - } - try (PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8))) { - for (String commit : myCommits) { - String key = pickLines.headMap(commit + "\u0000").lastKey(); - if (key == null || !commit.startsWith(key)) { - continue; // commit from merged branch - } - w.print(pickLines.get(key) + "\n"); - } - } - return 0; - } - catch (Exception ex) { - LOG.error("Editor failed: ", ex); - return 1; - } - } - else { - return super.editCommits(path); - } - } - } - - } diff --git a/plugins/git4idea/tests/git4idea/rebase/GitRebaserReorderCommitsTest.java b/plugins/git4idea/tests/git4idea/rebase/GitRebaserReorderCommitsTest.java deleted file mode 100644 index 6101036774e0..000000000000 --- a/plugins/git4idea/tests/git4idea/rebase/GitRebaserReorderCommitsTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2000-2011 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package git4idea.rebase; - -import com.intellij.openapi.progress.EmptyProgressIndicator; -import com.intellij.openapi.vfs.VirtualFile; -import git4idea.commands.Git; -import git4idea.test.GitSingleRepoTest; -import git4idea.test.GitTestUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.ide.BuiltInServerManager; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static com.intellij.openapi.vcs.Executor.touch; -import static git4idea.test.GitExecutor.*; - -/** - * NB: we don't test merge commits here, since {@link GitRebaser#reoderCommitsIfNeeded(VirtualFile, String, List)} - * doesn't handle it for now. - */ -public class GitRebaserReorderCommitsTest extends GitSingleRepoTest { - - private GitRebaser myRebaser; - private String myFirstCommit; - - @Override protected void setUp() throws Exception { - super.setUp(); - GitTestUtil.createRepository(myProject, projectPath, false); - myRebaser = new GitRebaser(myProject, Git.getInstance(), new EmptyProgressIndicator()); - myFirstCommit = makeCommit(); - BuiltInServerManager.getInstance().waitForStart(); - } - - @Override - protected boolean makeInitialCommit() { - return false; - } - - public void testReorderingNothingShouldDoNothing() { - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Collections.emptyList()); - assertCommits(myFirstCommit); - } - - public void testReorderingOneShouldDoNothing() { - String hash = makeCommit(); - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Collections.singletonList(hash)); - assertCommits(myFirstCommit, hash); - } - - public void testReorderingAllShouldDoNothing() { - String hash1 = makeCommit(); - String hash2 = makeCommit(); - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Arrays.asList(hash1, hash2)); - assertCommits(myFirstCommit, hash1, hash2); - } - - public void disabled_testReorderingOldestShouldDoNothing() { - String[] hashes = makeCommits(3); - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Arrays.asList(hashes[0], hashes[1])); - assertCommits(myFirstCommit, hashes[0], hashes[1], hashes[2]); - } - - public void disabled_testReorderingOneCommit() { - String[] hashes = makeCommits(3); - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Collections.singletonList(hashes[2])); - assertCommits(myFirstCommit, hashes[2], hashes[0], hashes[1]); - } - - public void disabled_testReorderingTwoCommits() { - String[] hashes = makeCommits(3); - myRebaser.reoderCommitsIfNeeded(projectRoot, myFirstCommit, Arrays.asList(hashes[2], hashes[1])); - assertCommits(myFirstCommit, hashes[2], hashes[1], hashes[0]); - } - - private String[] makeCommits(int number) { - String[] hashes = new String[number]; - for (int i = 0; i < hashes.length; i++) { - hashes[i] = makeCommit(); - } - return hashes; - } - - @NotNull - private String makeCommit() { - touch(Math.random() + ".txt", "initial" + Math.random()); - addCommit(repo, "some commit"); - return last(repo); - } - - private void assertCommits(String... commits) { - final String[] hashes = git("rev-list --reverse HEAD").split("\n"); - assertEquals(commits.length, hashes.length); - for (int i = 0; i < commits.length; i++) { - assertEquals("Commit #" + i + " doesn't match", commits[i], hashes[i]); - } - } - -}