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
.ui
.DialogWrapper
;
21 import com
.intellij
.openapi
.vcs
.VcsException
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import git4idea
.GitVcs
;
24 import git4idea
.commands
.GitHandler
;
25 import git4idea
.commands
.GitLineHandler
;
26 import git4idea
.commands
.GitSimpleHandler
;
27 import git4idea
.i18n
.GitBundle
;
28 import git4idea
.ui
.GitUIUtil
;
32 import java
.awt
.event
.ActionEvent
;
33 import java
.awt
.event
.ActionListener
;
34 import java
.util
.Collections
;
35 import java
.util
.List
;
36 import java
.util
.StringTokenizer
;
39 * A dialog for merge action. It represents most options available for git merge.
41 public class GitMergeDialog
extends DialogWrapper
{
43 * The git root available for git merge action
45 private JComboBox myGitRoot
;
47 * The check box indicating that no commit will be created
49 private JCheckBox myNoCommitCheckBox
;
51 * The checkbox that suppresses fast forward resolution even if it is available
53 private JCheckBox myNoFastForwardCheckBox
;
55 * The checkbox that allows squashing all changes from branch into a single commit
57 private JCheckBox mySquashCommitCheckBox
;
59 * The label containing a name of the current branch
61 private JLabel myCurrentBranchText
;
63 * The panel containing a chooser of branches to merge
65 private JPanel myBranchToMergeContainer
;
67 * Chooser of branches to merge
69 private ElementsChooser
<String
> myBranchChooser
;
73 private JTextField myCommitMessage
;
75 * The strategy for merge
77 private JComboBox myStrategy
;
81 private JPanel myPanel
;
83 * The log information checkbox
85 private JCheckBox myAddLogInformationCheckBox
;
89 private final Project myProject
;
95 * @param project a project to select
96 * @param roots a git repository roots for the project
97 * @param defaultRoot a guessed default root
99 public GitMergeDialog(Project project
, List
<VirtualFile
> roots
, VirtualFile defaultRoot
) {
100 super(project
, true);
101 setTitle(GitBundle
.getString("merge.branch.title"));
104 GitMergeUtil
.setupNoCommitCheckbox(myAddLogInformationCheckBox
, myCommitMessage
, myNoCommitCheckBox
);
105 setOKActionEnabled(false);
106 setOKButtonText(GitBundle
.getString("merge.branch.button"));
107 GitUIUtil
.setupRootChooser(myProject
, roots
, defaultRoot
, myGitRoot
, myCurrentBranchText
);
108 myGitRoot
.addActionListener(new ActionListener() {
109 public void actionPerformed(final ActionEvent e
) {
118 * Initialize {@link #myBranchChooser} component
120 private void initBranchChooser() {
121 myBranchChooser
= new ElementsChooser
<String
>(true);
122 myBranchChooser
.setToolTipText(GitBundle
.getString("merge.branches.tooltip"));
123 GridBagConstraints c
= new GridBagConstraints();
124 c
.insets
= new Insets(0, 0, 0, 0);
129 c
.fill
= GridBagConstraints
.BOTH
;
130 myBranchToMergeContainer
.add(myBranchChooser
, c
);
131 GitMergeUtil
.setupStrategies(myBranchChooser
, myNoCommitCheckBox
, myStrategy
);
132 final ElementsChooser
.ElementsMarkListener
<String
> listener
= new ElementsChooser
.ElementsMarkListener
<String
>() {
133 public void elementMarkChanged(final String element
, final boolean isMarked
) {
134 setOKActionEnabled(myBranchChooser
.getMarkedElements().size() != 0);
137 listener
.elementMarkChanged(null, true);
138 myBranchChooser
.addElementsMarkListener(listener
);
143 * Setup branches for git root, this method should be called when root is changed.
145 private void updateBranches() {
147 VirtualFile root
= getSelectedRoot();
148 GitSimpleHandler handler
= new GitSimpleHandler(myProject
, root
, GitHandler
.BRANCH
);
149 handler
.setNoSSH(true);
150 handler
.setSilent(true);
151 handler
.addParameters("-a", "--no-merged");
152 String output
= handler
.run();
153 myBranchChooser
.clear();
154 for (StringTokenizer lines
= new StringTokenizer(output
, "\n", false); lines
.hasMoreTokens();) {
155 String branch
= lines
.nextToken().substring(2);
156 myBranchChooser
.addElement(branch
, false);
159 catch (VcsException e
) {
160 GitVcs
.getInstance(myProject
).showErrors(Collections
.singletonList(e
), GitBundle
.getString("merge.retriving.branches"));
165 * @return get line handler configured according to the selected options
167 public GitLineHandler
handler() {
169 throw new IllegalStateException("The handler could be retrieved only if dialog was completed successfully.");
171 VirtualFile root
= (VirtualFile
)myGitRoot
.getSelectedItem();
172 GitLineHandler h
= new GitLineHandler(myProject
, root
, GitHandler
.MERGE
);
173 // ignore merge failure
174 h
.ignoreErrorCode(1);
176 if (myNoCommitCheckBox
.isSelected()) {
177 h
.addParameters("--no-commit");
180 if (myAddLogInformationCheckBox
.isSelected()) {
181 h
.addParameters("--log");
183 final String msg
= myCommitMessage
.getText().trim();
184 if (msg
.length() != 0) {
185 h
.addParameters("-m", msg
);
188 if (mySquashCommitCheckBox
.isSelected()) {
189 h
.addParameters("--squash");
191 if (myNoFastForwardCheckBox
.isSelected()) {
192 h
.addParameters("--no-ff");
194 String strategy
= (String
)myStrategy
.getSelectedItem();
195 if (!GitMergeUtil
.DEFAULT_STRATEGY
.equals(strategy
)) {
196 h
.addParameters("--strategy", strategy
);
198 for (String branch
: myBranchChooser
.getMarkedElements()) {
199 h
.addParameters(branch
);
208 protected JComponent
createCenterPanel() {
216 protected String
getDimensionServiceKey() {
217 return getClass().getName();
224 protected String
getHelpId() {
225 return "reference.VersionControl.Git.MergeBranches";
229 * @return selected root
231 public VirtualFile
getSelectedRoot() {
232 return (VirtualFile
)myGitRoot
.getSelectedItem();