From c59e2edeff640bcf976d00d17f62495beaeb9985 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 14 Apr 2015 16:09:17 +0300 Subject: [PATCH] [git] extract reading interactive rebase data file to a separate class Preparation to fix IDEA-138787 --- .../GitInteractiveRebaseEditorHandler.java | 9 +- .../rebase/GitInteractiveRebaseFile.java | 96 +++++++++++++ .../src/git4idea/rebase/GitRebaseEditor.java | 129 ++---------------- 3 files changed, 116 insertions(+), 118 deletions(-) create mode 100644 plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseFile.java diff --git a/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseEditorHandler.java b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseEditorHandler.java index b07cd4e23f0f..d2e12f0b786f 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseEditorHandler.java +++ b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseEditorHandler.java @@ -24,6 +24,7 @@ import git4idea.commands.GitHandler; import org.jetbrains.annotations.NotNull; import java.io.Closeable; +import java.util.List; /** * The handler for rebase editor request. The handler shows {@link git4idea.rebase.GitRebaseEditor} @@ -115,15 +116,17 @@ public class GitInteractiveRebaseEditorHandler implements Closeable, GitRebaseEd } } else { + GitInteractiveRebaseFile rebaseFile = new GitInteractiveRebaseFile(myProject, myRoot, path); + List entries = rebaseFile.load(); setRebaseEditorShown(); - GitRebaseEditor editor = new GitRebaseEditor(myProject, myRoot, path); + GitRebaseEditor editor = new GitRebaseEditor(myProject, myRoot, entries); if (editor.showAndGet()) { - editor.save(); + rebaseFile.save(editor.getEntries()); isSuccess.set(true); return; } else { - editor.cancel(); + rebaseFile.cancel(); isSuccess.set(true); } } diff --git a/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseFile.java b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseFile.java new file mode 100644 index 000000000000..e23920fc311f --- /dev/null +++ b/plugins/git4idea/src/git4idea/rebase/GitInteractiveRebaseFile.java @@ -0,0 +1,96 @@ +/* + * Copyright 2000-2015 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.project.Project; +import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.containers.ContainerUtil; +import git4idea.config.GitConfigUtil; +import git4idea.util.StringScanner; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +import java.io.*; +import java.util.List; + +class GitInteractiveRebaseFile { + @NonNls private static final String CYGDRIVE_PREFIX = "/cygdrive/"; + + @NotNull private final Project myProject; + @NotNull private final VirtualFile myRoot; + @NotNull private final String myFile; + + GitInteractiveRebaseFile(@NotNull Project project, @NotNull VirtualFile root, @NotNull String rebaseFilePath) { + myProject = project; + myRoot = root; + myFile = adjustFilePath(rebaseFilePath); + } + + @NotNull + public List load() throws IOException { + String encoding = GitConfigUtil.getLogEncoding(myProject, myRoot); + List entries = ContainerUtil.newArrayList(); + final StringScanner s = new StringScanner(FileUtil.loadFile(new File(myFile), encoding)); + while (s.hasMoreData()) { + if (s.isEol() || s.startsWith('#') || s.startsWith("noop")) { + s.nextLine(); + continue; + } + String action = s.spaceToken(); + String hash = s.spaceToken(); + String comment = s.line(); + + entries.add(new GitRebaseEntry(action, hash, comment)); + } + return entries; + } + + public void cancel() throws IOException { + PrintWriter out = new PrintWriter(new FileWriter(myFile)); + try { + out.println("# rebase is cancelled"); + } + finally { + out.close(); + } + } + + public void save(@NotNull List entries) throws IOException { + String encoding = GitConfigUtil.getLogEncoding(myProject, myRoot); + PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(myFile), encoding)); + try { + for (GitRebaseEntry e : entries) { + if (e.getAction() != GitRebaseEntry.Action.skip) { + out.println(e.getAction().toString() + " " + e.getCommit() + " " + e.getSubject()); + } + } + } + finally { + out.close(); + } + } + + @NotNull + private static String adjustFilePath(@NotNull String file) { + if (SystemInfo.isWindows && file.startsWith(CYGDRIVE_PREFIX)) { + final int prefixSize = CYGDRIVE_PREFIX.length(); + return file.substring(prefixSize, prefixSize + 1) + ":" + file.substring(prefixSize + 1); + } + return file; + } +} diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java b/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java index f14ed780c58d..51edcf439623 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java @@ -17,8 +17,6 @@ package git4idea.rebase; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.Cell; @@ -33,10 +31,8 @@ import com.intellij.util.ui.ComboBoxTableCellRenderer; import com.intellij.util.ui.JBUI; import com.intellij.util.ui.UIUtil; import git4idea.GitUtil; -import git4idea.config.GitConfigUtil; import git4idea.i18n.GitBundle; -import git4idea.util.StringScanner; -import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -48,8 +44,7 @@ import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.*; -import java.util.ArrayList; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -83,45 +78,21 @@ public class GitRebaseEditor extends DialogWrapper { * Table model */ private final MyTableModel myTableModel; - /** - * The file name - */ - private final String myFile; - /** - * The project - */ - private final Project myProject; - /** - * The git root - */ - private final VirtualFile myGitRoot; - /** - * The cygwin drive prefix - */ - @NonNls private static final String CYGDRIVE_PREFIX = "/cygdrive/"; /** * The constructor * * @param project the project * @param gitRoot the git root - * @param file the file to edit + * @param entries the file to edit * @throws IOException if file could not be loaded */ - protected GitRebaseEditor(final Project project, final VirtualFile gitRoot, String file) throws IOException { + protected GitRebaseEditor(final Project project, final VirtualFile gitRoot, List entries) throws IOException { super(project, true); - myProject = project; - myGitRoot = gitRoot; setTitle(GitBundle.getString("rebase.editor.title")); setOKButtonText(GitBundle.getString("rebase.editor.button")); - if (SystemInfo.isWindows && file.startsWith(CYGDRIVE_PREFIX)) { - final int prefixSize = CYGDRIVE_PREFIX.length(); - file = file.substring(prefixSize, prefixSize + 1) + ":" + file.substring(prefixSize + 1); - } - myFile = file; - myTableModel = new MyTableModel(); - myTableModel.load(file); + myTableModel = new MyTableModel(entries); myCommitsTable.setModel(myTableModel); myCommitsTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); myCommitsTable.setIntercellSpacing(JBUI.emptySize()); @@ -225,15 +196,6 @@ public class GitRebaseEditor extends DialogWrapper { setOKActionEnabled(true); } - /** - * Save entries back to the file - * - * @throws IOException if there is IO problem with saving - */ - public void save() throws IOException { - myTableModel.save(myFile); - } - /** * {@inheritDoc} */ @@ -257,13 +219,9 @@ public class GitRebaseEditor extends DialogWrapper { return "reference.VersionControl.Git.RebaseCommits"; } - /** - * Cancel rebase - * - * @throws IOException if file cannot be reset to empty one - */ - public void cancel() throws IOException { - myTableModel.cancel(myFile); + @NotNull + public List getEntries() { + return myTableModel.myEntries; } @@ -284,12 +242,13 @@ public class GitRebaseEditor extends DialogWrapper { */ private static final int SUBJECT = 2; - /** - * The entries - */ - final List myEntries = new ArrayList(); + @NotNull private final List myEntries; private int[] myLastEditableSelectedRows = new int[]{}; + MyTableModel(@NotNull List entries) { + myEntries = entries; + } + /** * {@inheritDoc} */ @@ -367,7 +326,7 @@ public class GitRebaseEditor extends DialogWrapper { } private void setSelection(ContiguousIntIntervalTracker intervalBuilder) { - myCommitsTable.getSelectionModel().setSelectionInterval( intervalBuilder.getMin() , intervalBuilder.getMax() ); + myCommitsTable.getSelectionModel().setSelectionInterval(intervalBuilder.getMin(), intervalBuilder.getMax()); } private void setRowAction(Object aValue, int rowIndex, int columnIndex) { @@ -385,66 +344,6 @@ public class GitRebaseEditor extends DialogWrapper { return columnIndex == ACTION; } - /** - * Load data from the file - * - * @param file the file to load - * @throws IOException if file could not be loaded - */ - public void load(final String file) throws IOException { - String encoding = GitConfigUtil.getLogEncoding(myProject, myGitRoot); - final StringScanner s = new StringScanner(FileUtil.loadFile(new File(file), encoding)); - while (s.hasMoreData()) { - if (s.isEol() || s.startsWith('#') || s.startsWith("noop")) { - s.nextLine(); - continue; - } - String action = s.spaceToken(); - String hash = s.spaceToken(); - String comment = s.line(); - myEntries.add(new GitRebaseEntry(action, hash, comment)); - } - } - - /** - * Save text to the file - * - * @param file the file to save to - * @throws IOException if there is IO problem - */ - public void save(final String file) throws IOException { - String encoding = GitConfigUtil.getLogEncoding(myProject, myGitRoot); - PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); - try { - for (GitRebaseEntry e : myEntries) { - if (e.getAction() != GitRebaseEntry.Action.skip) { - out.println(e.getAction().toString() + " " + e.getCommit() + " " + e.getSubject()); - } - } - } - finally { - out.close(); - } - } - - /** - * Save text to the file - * - * @param file the file to save to - * @throws IOException if there is IO problem - */ - public void cancel(final String file) throws IOException { - PrintWriter out = new PrintWriter(new FileWriter(file)); - try { - //noinspection HardCodedStringLiteral - out.println("# rebase is cancelled"); - } - finally { - out.close(); - } - } - - public void moveRows(int[] rows, MoveDirection direction) { myCommitsTable.removeEditor();