IDEA-56303 Git Merge Tool renamed to Resolve Conflicts.
[idea/community.git] / plugins / git4idea / src / git4idea / actions / GitResolveConflictsAction.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package git4idea.actions;
17
18 import com.intellij.openapi.actionSystem.AnActionEvent;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vcs.AbstractVcsHelper;
21 import com.intellij.openapi.vcs.FileStatus;
22 import com.intellij.openapi.vcs.changes.Change;
23 import com.intellij.openapi.vcs.changes.ChangeListManager;
24 import com.intellij.openapi.vcs.changes.ContentRevision;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import git4idea.GitVcs;
27 import org.jetbrains.annotations.NotNull;
28
29 import java.util.*;
30
31 /**
32  * Git merge tool for resolving conflicts. Use IDEA built-in 3-way merge tool.
33  */
34 public class GitResolveConflictsAction extends GitAction {
35
36   @Override
37   public void actionPerformed(@NotNull AnActionEvent event) {
38     final Project project = event.getProject();
39
40     final Set<VirtualFile> conflictedFiles = new TreeSet<VirtualFile>(new Comparator<VirtualFile>() {
41       @Override
42       public int compare(@NotNull VirtualFile f1, @NotNull VirtualFile f2) {
43         return f1.getPresentableUrl().compareTo(f2.getPresentableUrl());
44       }
45     });
46     for (Change change : ChangeListManager.getInstance(project).getAllChanges()) {
47       final ContentRevision before = change.getBeforeRevision();
48       final ContentRevision after = change.getAfterRevision();
49       if (before != null) {
50         final VirtualFile file = before.getFile().getVirtualFile();
51         if (file != null) {
52           conflictedFiles.add(file);
53         }
54       }
55       if (after != null) {
56         final VirtualFile file = after.getFile().getVirtualFile();
57         if (file != null) {
58           conflictedFiles.add(file);
59         }
60       }
61     }
62
63     AbstractVcsHelper.getInstance(project).showMergeDialog(new ArrayList<VirtualFile>(conflictedFiles), GitVcs.getInstance(project).getMergeProvider());
64   }
65
66   @Override
67   protected boolean isEnabled(@NotNull AnActionEvent event) {
68     final Collection<Change> changes = ChangeListManager.getInstance(event.getProject()).getAllChanges();
69     if (changes.size() > 1000) {
70       return true;
71     }
72     for (Change change : changes) {
73       if (change.getFileStatus() == FileStatus.MERGED_WITH_CONFLICTS) {
74         return true;
75       }
76     }
77     return false;
78   }
79
80 }