From c54bcdacfc833f04a5ee5b362904c22ecb31aa10 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 29 May 2012 12:02:34 +0400 Subject: [PATCH] [git] Let dynamically add changes from a stash by updating the live file. Define "local" parameter in GitChangeUtils.parseChangeList() and use CurrentContentRevision if local. Use it in the GitUnstashDialog. --- plugins/git4idea/src/git4idea/GitUtil.java | 4 +-- .../GitShowAllSubmittedFilesAction.java | 7 +++-- .../src/git4idea/changes/GitChangeUtils.java | 29 +++++++++++++------ .../src/git4idea/rebase/GitRebaseEditor.java | 2 +- .../git4idea/ui/GitReferenceValidator.java | 2 +- .../src/git4idea/ui/GitUnstashDialog.java | 2 +- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/plugins/git4idea/src/git4idea/GitUtil.java b/plugins/git4idea/src/git4idea/GitUtil.java index 4c4879a483ab..2dcc24f0c017 100644 --- a/plugins/git4idea/src/git4idea/GitUtil.java +++ b/plugins/git4idea/src/git4idea/GitUtil.java @@ -508,7 +508,7 @@ public class GitUtil { if ((!firstStep) && lineIsAStart) { final StringScanner innerScanner = new StringScanner(sb.toString()); sb.setLength(0); - consumer.consume(GitChangeUtils.parseChangeList(project, root, innerScanner, skipDiffsForMerge, h)); + consumer.consume(GitChangeUtils.parseChangeList(project, root, innerScanner, skipDiffsForMerge, h, false)); } sb.append(lineIsAStart ? line.substring(2) : line).append('\n'); firstStep = false; @@ -516,7 +516,7 @@ public class GitUtil { if (sb.length() > 0) { final StringScanner innerScanner = new StringScanner(sb.toString()); sb.setLength(0); - consumer.consume(GitChangeUtils.parseChangeList(project, root, innerScanner, skipDiffsForMerge, h)); + consumer.consume(GitChangeUtils.parseChangeList(project, root, innerScanner, skipDiffsForMerge, h, false)); } if (s.hasMoreData()) { throw new IllegalStateException("More input is avaialble: " + s.line()); diff --git a/plugins/git4idea/src/git4idea/actions/GitShowAllSubmittedFilesAction.java b/plugins/git4idea/src/git4idea/actions/GitShowAllSubmittedFilesAction.java index a97f569e70b8..b0f760edd2c8 100644 --- a/plugins/git4idea/src/git4idea/actions/GitShowAllSubmittedFilesAction.java +++ b/plugins/git4idea/src/git4idea/actions/GitShowAllSubmittedFilesAction.java @@ -86,7 +86,7 @@ public class GitShowAllSubmittedFilesAction extends AnAction implements DumbAwar * @param file file affected by the revision */ public static void showSubmittedFiles(final Project project, final VcsFileRevision revision, final VirtualFile file) { - showSubmittedFiles(project, revision.getRevisionNumber().asString(), file); + showSubmittedFiles(project, revision.getRevisionNumber().asString(), file, false); } /** @@ -95,14 +95,15 @@ public class GitShowAllSubmittedFilesAction extends AnAction implements DumbAwar * @param project a project * @param revision a revision number * @param file file affected by the revision + * @param local */ - public static void showSubmittedFiles(final Project project, final String revision, final VirtualFile file) { + public static void showSubmittedFiles(final Project project, final String revision, final VirtualFile file, final boolean local) { new Task.Backgroundable(project, GitBundle.message("changes.retrieving", revision)) { public void run(@NotNull ProgressIndicator indicator) { indicator.setIndeterminate(true); try { VirtualFile vcsRoot = GitUtil.getGitRoot(file); - final CommittedChangeList changeList = GitChangeUtils.getRevisionChanges(project, vcsRoot, revision, true); + final CommittedChangeList changeList = GitChangeUtils.getRevisionChanges(project, vcsRoot, revision, true, local); if (changeList != null) { UIUtil.invokeLaterIfNeeded(new Runnable() { public void run() { diff --git a/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java b/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java index 4e5f4af077cc..c0080c347893 100644 --- a/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java +++ b/plugins/git4idea/src/git4idea/changes/GitChangeUtils.java @@ -122,7 +122,7 @@ public class GitChangeUtils { public static void parseChanges(Project project, VirtualFile vcsRoot, @Nullable GitRevisionNumber thisRevision, - GitRevisionNumber parentRevision, + @Nullable GitRevisionNumber parentRevision, StringScanner s, Collection changes, final Set ignoreNames) throws VcsException { @@ -230,14 +230,20 @@ public class GitChangeUtils { * of the merge, so changes are reported as difference with the first revision * listed on the the merge that has at least some changes. * + * * @param project the project file * @param root the git root * @param revisionName the name of revision (might be tag) * @param skipDiffsForMerge + * @param local * @return change list for the respective revision * @throws VcsException in case of problem with running git */ - public static GitCommittedChangeList getRevisionChanges(Project project, VirtualFile root, String revisionName, boolean skipDiffsForMerge) throws VcsException { + public static GitCommittedChangeList getRevisionChanges(Project project, + VirtualFile root, + String revisionName, + boolean skipDiffsForMerge, + boolean local) throws VcsException { GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.SHOW); h.setNoSSH(true); h.setSilent(true); @@ -245,7 +251,7 @@ public class GitChangeUtils { revisionName, "--"); String output = h.run(); StringScanner s = new StringScanner(output); - return parseChangeList(project, root, s, skipDiffsForMerge, h); + return parseChangeList(project, root, s, skipDiffsForMerge, h, local); } @Nullable @@ -328,15 +334,24 @@ public class GitChangeUtils { /** * Parse changelist * + * * @param project the project * @param root the git root * @param s the scanner for log or show command output * @param skipDiffsForMerge * @param handler the handler that produced the output to parse. - for debugging purposes. + * @param local pass {@code true} to indicate that this revision should be an editable + * {@link com.intellij.openapi.vcs.changes.CurrentContentRevision}. + * Pass {@code false} for * @return the parsed changelist * @throws VcsException if there is a problem with running git */ - public static GitCommittedChangeList parseChangeList(Project project, VirtualFile root, StringScanner s, boolean skipDiffsForMerge, GitHandler handler) throws VcsException { + public static GitCommittedChangeList parseChangeList(Project project, + VirtualFile root, + StringScanner s, + boolean skipDiffsForMerge, + GitHandler handler, + boolean local) throws VcsException { ArrayList changes = new ArrayList(); // parse commit information final Date commitDate = GitUtil.parseTimestampWithNFEReport(s.line(), handler, s.getAllText()); @@ -367,7 +382,7 @@ public class GitChangeUtils { final GitRevisionNumber parentRevision = parents.length > 0 ? loadRevision(project, root, parents[0]) : null; // This is the first or normal commit with the single parent. // Just parse changes in this commit as returned by the show command. - parseChanges(project, root, thisRevision, parentRevision, s, changes, null); + parseChanges(project, root, thisRevision, local ? null : parentRevision, s, changes, null); } else { // This is the merge commit. It has multiple parent commits. @@ -376,10 +391,6 @@ public class GitChangeUtils { for (String parent : parents) { final GitRevisionNumber parentRevision = loadRevision(project, root, parent); - if (parentRevision == null) { - // the repository was cloned with --depth parameter - continue; - } GitSimpleHandler diffHandler = new GitSimpleHandler(project, root, GitCommand.DIFF); diffHandler.setNoSSH(true); diffHandler.setSilent(true); diff --git a/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java b/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java index e1332d5ba92b..a0225ad98d98 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java +++ b/plugins/git4idea/src/git4idea/rebase/GitRebaseEditor.java @@ -133,7 +133,7 @@ public class GitRebaseEditor extends DialogWrapper { return; } GitRebaseEntry entry = myTableModel.myEntries.get(row); - GitShowAllSubmittedFilesAction.showSubmittedFiles(project, entry.getCommit(), gitRoot); + GitShowAllSubmittedFilesAction.showSubmittedFiles(project, entry.getCommit(), gitRoot, false); } }); diff --git a/plugins/git4idea/src/git4idea/ui/GitReferenceValidator.java b/plugins/git4idea/src/git4idea/ui/GitReferenceValidator.java index eb652deb8702..125992f17c6e 100644 --- a/plugins/git4idea/src/git4idea/ui/GitReferenceValidator.java +++ b/plugins/git4idea/src/git4idea/ui/GitReferenceValidator.java @@ -94,7 +94,7 @@ public class GitReferenceValidator { myLastResult = false; try { GitRevisionNumber revision = GitRevisionNumber.resolve(myProject, gitRoot(), revisionExpression); - GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, revision.asString(), gitRoot()); + GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, revision.asString(), gitRoot(), false); myLastResult = true; } catch (VcsException ex) { diff --git a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java index 61ce16bfe79a..3804907db2b2 100644 --- a/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java +++ b/plugins/git4idea/src/git4idea/ui/GitUnstashDialog.java @@ -223,7 +223,7 @@ public class GitUnstashDialog extends DialogWrapper { GitUIUtil.showOperationError(myProject, ex, "resolving revision"); return; } - GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, resolvedStash, root); + GitShowAllSubmittedFilesAction.showSubmittedFiles(myProject, resolvedStash, root, true); } }); init();