git4idea: IDEADEV-36916: added file referesh before showing merge dialog
[fedora-idea.git] / plugins / git4idea / src / git4idea / merge / GitMergeUtil.java
blobce279817d5529b69e73b3dc2353f8d01dc137e9a
1 /*
2 * Copyright 2000-2008 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package git4idea.merge;
18 import com.intellij.ide.util.ElementsChooser;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vcs.AbstractVcsHelper;
21 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
22 import com.intellij.openapi.vcs.VcsException;
23 import com.intellij.openapi.vcs.ex.ProjectLevelVcsManagerEx;
24 import com.intellij.openapi.vcs.update.ActionInfo;
25 import com.intellij.openapi.vcs.update.FileGroup;
26 import com.intellij.openapi.vcs.update.UpdatedFiles;
27 import com.intellij.openapi.vfs.LocalFileSystem;
28 import com.intellij.openapi.vfs.VirtualFile;
29 import git4idea.GitRevisionNumber;
30 import git4idea.GitVcs;
31 import git4idea.i18n.GitBundle;
32 import org.jetbrains.annotations.NonNls;
34 import javax.swing.*;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
41 /**
42 * Utilities for merge
44 public class GitMergeUtil {
45 /**
46 * The item representing default strategy
48 public static final String DEFAULT_STRATEGY = GitBundle.getString("merge.default.strategy");
50 /**
51 * A private constructor for utility class
53 private GitMergeUtil() {
57 /**
58 * Get a list of merge strategies for the specified branch count
60 * @param branchCount a number of branches to merge
61 * @return an array of strategy names
63 @NonNls
64 public static String[] getMergeStrategies(int branchCount) {
65 if (branchCount < 0) {
66 throw new IllegalArgumentException("Brach count must be non-negative: " + branchCount);
68 switch (branchCount) {
69 case 0:
70 return new String[]{DEFAULT_STRATEGY};
71 case 1:
72 return new String[]{DEFAULT_STRATEGY, "resolve", "recursive", "octopus", "ours", "subtree"};
73 default:
74 return new String[]{DEFAULT_STRATEGY, "octopus", "ours"};
78 /**
79 * Initialize no commit checkbox (for both merge and pull dialog)
81 * @param addLogInformationCheckBox a log information checkbox
82 * @param commitMessage a commit message text field or null
83 * @param noCommitCheckBox a no commit checkbox to configure
85 public static void setupNoCommitCheckbox(final JCheckBox addLogInformationCheckBox,
86 final JTextField commitMessage,
87 final JCheckBox noCommitCheckBox) {
88 noCommitCheckBox.addActionListener(new ActionListener() {
89 public void actionPerformed(final ActionEvent e) {
90 final boolean selected = noCommitCheckBox.isSelected();
91 if (commitMessage != null) {
92 commitMessage.setEnabled(!selected);
94 if (selected) {
95 addLogInformationCheckBox.setSelected(false);
97 addLogInformationCheckBox.setEnabled(!selected);
99 });
103 * Setup strategies combobox. The set of strategies changes according to amount of selected elements in branchChooser.
105 * @param branchChooser a branch chooser
106 * @param noCommitCheckBox no commit checkbox
107 * @param strategy a strategy selector
109 public static void setupStrategies(final ElementsChooser<String> branchChooser,
110 final JCheckBox noCommitCheckBox,
111 final JComboBox strategy) {
112 final ElementsChooser.ElementsMarkListener<String> listener = new ElementsChooser.ElementsMarkListener<String>() {
113 private void updateStrategies(final List<String> elements) {
114 strategy.removeAllItems();
115 for (String s : getMergeStrategies(elements.size())) {
116 strategy.addItem(s);
118 strategy.setSelectedItem(DEFAULT_STRATEGY);
121 public void elementMarkChanged(final String element, final boolean isMarked) {
122 final List<String> elements = branchChooser.getMarkedElements();
123 if (elements.size() == 0) {
124 strategy.setEnabled(false);
125 updateStrategies(elements);
127 else if (elements.size() == 1) {
128 strategy.setEnabled(true);
129 updateStrategies(elements);
130 noCommitCheckBox.setEnabled(true);
131 noCommitCheckBox.setSelected(false);
133 else {
134 strategy.setEnabled(true);
135 updateStrategies(elements);
136 noCommitCheckBox.setEnabled(false);
137 noCommitCheckBox.setSelected(false);
141 listener.elementMarkChanged(null, true);
142 branchChooser.addElementsMarkListener(listener);
146 * Show updates caused by git operation
148 * @param project the context project
149 * @param exceptions the exception list
150 * @param root the git root
151 * @param currentRev the revision before update
152 * @param actionName the action name
153 * @param actionInfo the information about the action
155 public static void showUpdates(final Project project,
156 final List<VcsException> exceptions,
157 final VirtualFile root,
158 final GitRevisionNumber currentRev,
159 final String actionName,
160 final ActionInfo actionInfo) {
161 final UpdatedFiles files = UpdatedFiles.create();
162 MergeChangeCollector collector = new MergeChangeCollector(project, root, currentRev, files);
163 collector.collect(exceptions);
164 if (exceptions.size() != 0) {
165 return;
167 ProjectLevelVcsManagerEx manager = (ProjectLevelVcsManagerEx)ProjectLevelVcsManager.getInstance(project);
168 manager.showUpdateProjectInfo(files, actionName, actionInfo);
169 Collection<String> unmergedNames = files.getGroupById(FileGroup.MERGED_WITH_CONFLICT_ID).getFiles();
170 if (!unmergedNames.isEmpty()) {
171 LocalFileSystem lfs = LocalFileSystem.getInstance();
172 ArrayList<VirtualFile> unmerged = new ArrayList<VirtualFile>();
173 for (String fileName : unmergedNames) {
174 VirtualFile f = lfs.findFileByPath(fileName);
175 if (f != null) {
176 f.refresh(false, false);
177 unmerged.add(f);
180 AbstractVcsHelper.getInstance(project).showMergeDialog(unmerged, GitVcs.getInstance(project).getMergeProvider());