git4idea: Fixed options on merge and pull dialogs to better reflect git option depend...
[fedora-idea.git] / plugins / git4idea / src / git4idea / merge / GitMergeUtil.java
blobdcfecaff1667f48eab7d13cb5a712ecbf7cd7c94
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.history.Label;
19 import com.intellij.history.LocalHistory;
20 import com.intellij.ide.util.ElementsChooser;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.vcs.AbstractVcsHelper;
23 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
24 import com.intellij.openapi.vcs.TransactionRunnable;
25 import com.intellij.openapi.vcs.VcsException;
26 import com.intellij.openapi.vcs.ex.ProjectLevelVcsManagerEx;
27 import com.intellij.openapi.vcs.update.ActionInfo;
28 import com.intellij.openapi.vcs.update.FileGroup;
29 import com.intellij.openapi.vcs.update.UpdateInfoTree;
30 import com.intellij.openapi.vcs.update.UpdatedFiles;
31 import com.intellij.openapi.vfs.LocalFileSystem;
32 import com.intellij.openapi.vfs.VirtualFile;
33 import git4idea.GitRevisionNumber;
34 import git4idea.GitVcs;
35 import git4idea.actions.GitRepositoryAction;
36 import git4idea.i18n.GitBundle;
37 import org.jetbrains.annotations.NonNls;
39 import javax.swing.*;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.List;
46 /**
47 * Utilities for merge
49 public class GitMergeUtil {
50 /**
51 * The item representing default strategy
53 public static final String DEFAULT_STRATEGY = GitBundle.getString("merge.default.strategy");
55 /**
56 * A private constructor for utility class
58 private GitMergeUtil() {
62 /**
63 * Get a list of merge strategies for the specified branch count
65 * @param branchCount a number of branches to merge
66 * @return an array of strategy names
68 @NonNls
69 public static String[] getMergeStrategies(int branchCount) {
70 if (branchCount < 0) {
71 throw new IllegalArgumentException("Brach count must be non-negative: " + branchCount);
73 switch (branchCount) {
74 case 0:
75 return new String[]{DEFAULT_STRATEGY};
76 case 1:
77 return new String[]{DEFAULT_STRATEGY, "resolve", "recursive", "octopus", "ours", "subtree"};
78 default:
79 return new String[]{DEFAULT_STRATEGY, "octopus", "ours"};
83 /**
84 * Initialize no commit checkbox (for both merge and pull dialog)
86 * @param addLogInformationCheckBox a log information checkbox
87 * @param commitMessage a commit message text field or null
88 * @param noCommitCheckBox a no commit checkbox to configure
90 public static void setupNoCommitCheckbox(final JCheckBox addLogInformationCheckBox,
91 final JTextField commitMessage,
92 final JCheckBox noCommitCheckBox) {
93 noCommitCheckBox.addActionListener(new ActionListener() {
94 public void actionPerformed(final ActionEvent e) {
95 final boolean selected = noCommitCheckBox.isSelected();
96 if (commitMessage != null) {
97 commitMessage.setEnabled(!selected);
99 if (selected) {
100 addLogInformationCheckBox.setSelected(false);
102 addLogInformationCheckBox.setEnabled(!selected);
108 * Setup strategies combobox. The set of strategies changes according to amount of selected elements in branchChooser.
110 * @param branchChooser a branch chooser
111 * @param strategy a strategy selector
113 public static void setupStrategies(final ElementsChooser<String> branchChooser, final JComboBox strategy) {
114 final ElementsChooser.ElementsMarkListener<String> listener = new ElementsChooser.ElementsMarkListener<String>() {
115 private void updateStrategies(final List<String> elements) {
116 strategy.removeAllItems();
117 for (String s : getMergeStrategies(elements.size())) {
118 strategy.addItem(s);
120 strategy.setSelectedItem(DEFAULT_STRATEGY);
123 public void elementMarkChanged(final String element, final boolean isMarked) {
124 final List<String> elements = branchChooser.getMarkedElements();
125 if (elements.size() == 0) {
126 strategy.setEnabled(false);
127 updateStrategies(elements);
129 else {
130 strategy.setEnabled(true);
131 updateStrategies(elements);
135 listener.elementMarkChanged(null, true);
136 branchChooser.addElementsMarkListener(listener);
140 * Show updates caused by git operation
142 * @param project the context project
143 * @param exceptions the exception list
144 * @param root the git root
145 * @param currentRev the revision before update
146 * @param beforeLabel the local history label before update
147 * @param actionName the action name
148 * @param actionInfo the information about the action
150 public static void showUpdates(GitRepositoryAction action,
151 final Project project,
152 final List<VcsException> exceptions,
153 final VirtualFile root,
154 final GitRevisionNumber currentRev,
155 final Label beforeLabel,
156 final String actionName,
157 final ActionInfo actionInfo) {
158 final UpdatedFiles files = UpdatedFiles.create();
159 MergeChangeCollector collector = new MergeChangeCollector(project, root, currentRev, files);
160 collector.collect(exceptions);
161 if (exceptions.size() != 0) {
162 return;
164 action.delayTask(new TransactionRunnable() {
165 public void run(List<VcsException> exceptionList) {
166 ProjectLevelVcsManagerEx manager = (ProjectLevelVcsManagerEx)ProjectLevelVcsManager.getInstance(project);
167 UpdateInfoTree tree = manager.showUpdateProjectInfo(files, actionName, actionInfo);
168 tree.setBefore(beforeLabel);
169 tree.setAfter(LocalHistory.putSystemLabel(project, "After update"));
172 final Collection<String> unmergedNames = files.getGroupById(FileGroup.MERGED_WITH_CONFLICT_ID).getFiles();
173 if (!unmergedNames.isEmpty()) {
174 action.delayTask(new TransactionRunnable() {
175 public void run(List<VcsException> exceptionList) {
176 LocalFileSystem lfs = LocalFileSystem.getInstance();
177 final ArrayList<VirtualFile> unmerged = new ArrayList<VirtualFile>();
178 for (String fileName : unmergedNames) {
179 VirtualFile f = lfs.findFileByPath(fileName);
180 if (f != null) {
181 unmerged.add(f);
184 AbstractVcsHelper.getInstance(project).showMergeDialog(unmerged, GitVcs.getInstance(project).getMergeProvider());