summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java')
-rw-r--r--plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java136
1 files changed, 29 insertions, 107 deletions
diff --git a/plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java b/plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java
index 87edd30ae077..a6488d4ce3d0 100644
--- a/plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java
+++ b/plugins/git4idea/src/git4idea/history/GitDiffFromHistoryHandler.java
@@ -22,25 +22,22 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
-import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ContentRevision;
-import com.intellij.openapi.vcs.changes.ui.ChangesBrowser;
-import com.intellij.openapi.vcs.history.CurrentRevision;
+import com.intellij.openapi.vcs.history.BaseDiffFromHistoryHandler;
import com.intellij.openapi.vcs.history.DiffFromHistoryHandler;
import com.intellij.openapi.vcs.history.VcsFileRevision;
-import com.intellij.openapi.vcs.history.VcsHistoryUtil;
import com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Consumer;
+import com.intellij.util.containers.ContainerUtil;
import git4idea.GitFileRevision;
import git4idea.GitRevisionNumber;
import git4idea.GitUtil;
@@ -67,71 +64,59 @@ import java.util.List;
*
* @author Kirill Likhodedov
*/
-public class GitDiffFromHistoryHandler implements DiffFromHistoryHandler {
+public class GitDiffFromHistoryHandler extends BaseDiffFromHistoryHandler<GitFileRevision> {
private static final Logger LOG = Logger.getInstance(GitDiffFromHistoryHandler.class);
- @NotNull private final Project myProject;
@NotNull private final Git myGit;
@NotNull private final GitRepositoryManager myRepositoryManager;
public GitDiffFromHistoryHandler(@NotNull Project project) {
- myProject = project;
+ super(project);
myGit = ServiceManager.getService(project, Git.class);
myRepositoryManager = GitUtil.getRepositoryManager(project);
}
@Override
- public void showDiffForOne(@NotNull AnActionEvent e, @NotNull FilePath filePath,
- @NotNull VcsFileRevision previousRevision, @NotNull VcsFileRevision revision) {
+ public void showDiffForOne(@NotNull AnActionEvent e,
+ @NotNull FilePath filePath,
+ @NotNull VcsFileRevision previousRevision,
+ @NotNull VcsFileRevision revision) {
GitFileRevision rev = (GitFileRevision)revision;
Collection<String> parents = rev.getParents();
if (parents.size() < 2) {
- doShowDiff(filePath, previousRevision, revision, false);
+ super.showDiffForOne(e, filePath, previousRevision, revision);
}
else { // merge
showDiffForMergeCommit(e, filePath, rev, parents);
}
}
+ @NotNull
@Override
- public void showDiffForTwo(@NotNull FilePath filePath, @NotNull VcsFileRevision revision1, @NotNull VcsFileRevision revision2) {
- doShowDiff(filePath, revision1, revision2, true);
- }
+ protected List<Change> getChangesBetweenRevisions(@NotNull FilePath path, @NotNull GitFileRevision rev1, @Nullable GitFileRevision rev2)
+ throws VcsException {
+ GitRepository repository = getRepository(path);
+ String hash1 = rev1.getHash();
+ String hash2 = rev2 != null ? rev2.getHash() : null;
- private void doShowDiff(@NotNull FilePath filePath, @NotNull VcsFileRevision revision1, @NotNull VcsFileRevision revision2,
- boolean autoSort) {
- if (!filePath.isDirectory()) {
- VcsHistoryUtil.showDifferencesInBackground(myProject, filePath, revision1, revision2, autoSort);
- }
- else if (revision2 instanceof CurrentRevision) {
- GitFileRevision left = (GitFileRevision)revision1;
- showDiffForDirectory(filePath, left.getHash(), null);
- }
- else if (revision1.equals(VcsFileRevision.NULL)) {
- GitFileRevision right = (GitFileRevision)revision2;
- showDiffForDirectory(filePath, null, right.getHash());
- }
- else {
- GitFileRevision left = (GitFileRevision)revision1;
- GitFileRevision right = (GitFileRevision)revision2;
- if (autoSort) {
- Couple<VcsFileRevision> pair = VcsHistoryUtil.sortRevisions(revision1, revision2);
- left = (GitFileRevision)pair.first;
- right = (GitFileRevision)pair.second;
- }
- showDiffForDirectory(filePath, left.getHash(), right.getHash());
- }
+ return ContainerUtil
+ .newArrayList(GitChangeUtils.getDiff(repository.getProject(), repository.getRoot(), hash1, hash2, Collections.singletonList(path)));
}
- private void showDiffForDirectory(@NotNull final FilePath path, @Nullable final String hash1, @Nullable final String hash2) {
+ @NotNull
+ @Override
+ protected List<Change> getAffectedChanges(@NotNull FilePath path, @NotNull GitFileRevision rev) throws VcsException {
GitRepository repository = getRepository(path);
- calculateDiffInBackground(repository, path, hash1, hash2, new Consumer<List<Change>>() {
- @Override
- public void consume(List<Change> changes) {
- showDirDiffDialog(path, hash1, hash2, changes);
- }
- });
+
+ return ContainerUtil.newArrayList(
+ GitChangeUtils.getRevisionChanges(repository.getProject(), repository.getRoot(), rev.getHash(), false, true, true).getChanges());
+ }
+
+ @NotNull
+ @Override
+ protected String getPresentableName(@NotNull GitFileRevision revision) {
+ return GitUtil.getShortHash(revision.getHash());
}
@NotNull
@@ -141,63 +126,6 @@ public class GitDiffFromHistoryHandler implements DiffFromHistoryHandler {
return repository;
}
- // hash1 == null => hash2 is the initial commit
- // hash2 == null => comparing hash1 with local
- private void calculateDiffInBackground(@NotNull final GitRepository repository, @NotNull final FilePath path,
- @Nullable final String hash1, @Nullable final String hash2,
- final Consumer<List<Change>> successHandler) {
- new Task.Backgroundable(myProject, "Comparing revisions...") {
- private List<Change> myChanges;
- @Override
- public void run(@NotNull ProgressIndicator indicator) {
- try {
- if (hash1 != null) {
- // diff
- myChanges = new ArrayList<Change>(GitChangeUtils.getDiff(repository.getProject(), repository.getRoot(), hash1, hash2,
- Collections.singletonList(path)));
- }
- else {
- // show the initial commit
- myChanges = new ArrayList<Change>(GitChangeUtils.getRevisionChanges(repository.getProject(), repository.getRoot(), hash2, false,
- true, true).getChanges());
- }
- }
- catch (VcsException e) {
- showError(e, "Error during requesting diff for directory");
- }
- }
-
- @Override
- public void onSuccess() {
- successHandler.consume(myChanges);
- }
- }.queue();
- }
-
- private void showDirDiffDialog(@NotNull FilePath path, @Nullable String hash1, @Nullable String hash2, @NotNull List<Change> diff) {
- DialogBuilder dialogBuilder = new DialogBuilder(myProject);
- String title;
- if (hash2 != null) {
- if (hash1 != null) {
- title = String.format("Difference between %s and %s in %s", GitUtil.getShortHash(hash1), GitUtil.getShortHash(hash2), path.getName());
- }
- else {
- title = String.format("Initial commit %s in %s", GitUtil.getShortHash(hash2), path.getName());
- }
- }
- else {
- LOG.assertTrue(hash1 != null, "hash1 and hash2 can't both be null. Path: " + path);
- title = String.format("Difference between %s and local version in %s", GitUtil.getShortHash(hash1), path.getName());
- }
- dialogBuilder.setTitle(title);
- dialogBuilder.setActionDescriptors(new DialogBuilder.ActionDescriptor[] { new DialogBuilder.CloseDialogAction()});
- final ChangesBrowser changesBrowser = new ChangesBrowser(myProject, null, diff, null, false, true,
- null, ChangesBrowser.MyUseCase.COMMITTED_CHANGES, null);
- changesBrowser.setChangesToDisplay(diff);
- dialogBuilder.setCenterPanel(changesBrowser);
- dialogBuilder.showNotModal();
- }
-
private void showDiffForMergeCommit(@NotNull final AnActionEvent event, @NotNull final FilePath filePath,
@NotNull final GitFileRevision rev, @NotNull final Collection<String> parents) {
@@ -299,12 +227,6 @@ public class GitDiffFromHistoryHandler implements DiffFromHistoryHandler {
return makeRevisionFromHash(currentRevisionPath, parentHash);
}
-
- private void showError(VcsException e, String logMessage) {
- LOG.info(logMessage, e);
- VcsBalloonProblemNotifier.showOverVersionControlView(this.myProject, e.getMessage(), MessageType.ERROR);
- }
-
private void showPopup(@NotNull AnActionEvent event, @NotNull GitFileRevision rev, @NotNull FilePath filePath,
@NotNull Collection<GitFileRevision> parents) {
ActionGroup parentActions = createActionGroup(rev, filePath, parents);