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 com
.intellij
.ui
.DocumentAdapter
;
24 import git4idea
.GitRemote
;
25 import git4idea
.GitVcs
;
26 import git4idea
.commands
.GitHandler
;
27 import git4idea
.commands
.GitHandlerUtil
;
28 import git4idea
.commands
.GitLineHandler
;
29 import git4idea
.commands
.GitSimpleHandler
;
30 import git4idea
.i18n
.GitBundle
;
31 import git4idea
.ui
.GitUIUtil
;
32 import org
.jetbrains
.annotations
.Nullable
;
35 import javax
.swing
.event
.DocumentEvent
;
36 import java
.awt
.event
.ActionEvent
;
37 import java
.awt
.event
.ActionListener
;
38 import java
.util
.Collections
;
39 import java
.util
.List
;
44 public class GitPullDialog
extends DialogWrapper
{
48 private JPanel myPanel
;
50 * The selected git root
52 private JComboBox myGitRoot
;
54 * Current branch label
56 private JLabel myCurrentBranch
;
60 private JComboBox myStrategy
;
64 private JCheckBox myNoCommitCheckBox
;
66 * Squash commit option
68 private JCheckBox mySquashCommitCheckBox
;
70 * No fast forward option
72 private JCheckBox myNoFastForwardCheckBox
;
74 * Add log info to commit option
76 private JCheckBox myAddLogInformationCheckBox
;
78 * Selected remote option
80 private JComboBox myRemote
;
84 private JButton myGetBranchesButton
;
88 private ElementsChooser
<String
> myBranchChooser
;
92 private final Project myProject
;
97 * @param project a project to select
98 * @param roots a git repository roots for the project
99 * @param defaultRoot a guessed default root
101 public GitPullDialog(Project project
, List
<VirtualFile
> roots
, VirtualFile defaultRoot
) {
102 super(project
, true);
103 setTitle(GitBundle
.getString("pull.title"));
105 GitUIUtil
.setupRootChooser(myProject
, roots
, defaultRoot
, myGitRoot
, myCurrentBranch
);
106 myGitRoot
.addActionListener(new ActionListener() {
107 public void actionPerformed(final ActionEvent e
) {
111 setOKButtonText(GitBundle
.getString("pull.button"));
115 final ElementsChooser
.ElementsMarkListener
<String
> listener
= new ElementsChooser
.ElementsMarkListener
<String
>() {
116 public void elementMarkChanged(final String element
, final boolean isMarked
) {
120 myBranchChooser
.addElementsMarkListener(listener
);
121 listener
.elementMarkChanged(null, true);
122 GitMergeUtil
.setupNoCommitCheckbox(myAddLogInformationCheckBox
, null, myNoCommitCheckBox
);
123 GitMergeUtil
.setupStrategies(myBranchChooser
, myNoCommitCheckBox
, myStrategy
);
128 * Setup branch updating
130 private void setupBranches() {
131 ((JTextField
)myRemote
.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentAdapter() {
132 protected void textChanged(final DocumentEvent e
) {
140 * Validate dialog and enable buttons
142 private void validateDialog() {
143 if (getRemote().trim().length() == 0) {
144 setOKActionEnabled(false);
147 setOKActionEnabled(myBranchChooser
.getMarkedElements().size() != 0);
151 * Setup get branches button
153 private void setupGetBranches() {
154 final JTextField textField
= (JTextField
)myRemote
.getEditor().getEditorComponent();
155 final DocumentAdapter listener
= new DocumentAdapter() {
156 protected void textChanged(final DocumentEvent e
) {
158 myGetBranchesButton
.setEnabled(textField
.getText().trim().length() != 0);
161 textField
.getDocument().addDocumentListener(listener
);
162 listener
.changedUpdate(null);
163 myGetBranchesButton
.addActionListener(new ActionListener() {
164 public void actionPerformed(final ActionEvent e
) {
165 GitSimpleHandler h
= new GitSimpleHandler(myProject
, gitRoot(), GitHandler
.LS_REMOTE
);
166 h
.addParameters("--heads", myRemote
.getSelectedItem().toString());
167 String output
= GitHandlerUtil
.doSynchronously(h
, GitBundle
.getString("pull.getting.remote.branches"), h
.printableCommandLine());
168 if (output
== null) {
171 myBranchChooser
.removeAllElements();
172 for (String line
: output
.split("\n")) {
173 if (line
.length() == 0) {
176 int pos
= line
.lastIndexOf('/');
178 pos
= line
.lastIndexOf('\t');
180 myBranchChooser
.addElement(line
.substring(pos
+ 1), false);
187 * @return a pull handler configured according to dialog options
189 public GitLineHandler
pullHandler() {
190 GitLineHandler h
= new GitLineHandler(myProject
, gitRoot(), GitHandler
.PULL
);
191 // ignore merge failure for the pull
192 h
.ignoreErrorCode(1);
193 h
.addParameters("--no-stat");
194 if (myNoCommitCheckBox
.isSelected()) {
195 h
.addParameters("--no-commit");
198 if (myAddLogInformationCheckBox
.isSelected()) {
199 h
.addParameters("--log");
202 if (mySquashCommitCheckBox
.isSelected()) {
203 h
.addParameters("--squash");
205 if (myNoFastForwardCheckBox
.isSelected()) {
206 h
.addParameters("--no-ff");
208 String strategy
= (String
)myStrategy
.getSelectedItem();
209 if (!GitMergeUtil
.DEFAULT_STRATEGY
.equals(strategy
)) {
210 h
.addParameters("--strategy", strategy
);
212 h
.addParameters("-v");
213 h
.addParameters(getRemote());
214 final List
<String
> markedBranches
= myBranchChooser
.getMarkedElements();
215 h
.addParameters(markedBranches
.toArray(new String
[markedBranches
.size()]));
222 private void updateBranches() {
224 String item
= getRemote();
225 myBranchChooser
.removeAllElements();
227 final int count
= myRemote
.getItemCount();
228 for (int i
= 0; i
< count
; i
++) {
229 GitRemote candidate
= (GitRemote
)myRemote
.getItemAt(i
);
230 if (candidate
.name().equals(item
)) {
238 GitRemote
.Info ri
= r
.localInfo(myProject
, gitRoot());
239 String toSelect
= ri
.getRemoteForLocal(currentBranch());
240 for (String trackedBranch
: ri
.trackedBranches()) {
241 myBranchChooser
.addElement(trackedBranch
, trackedBranch
.equals(toSelect
));
244 catch (VcsException e
) {
245 GitVcs
.getInstance(myProject
).showErrors(Collections
.singletonList(e
), GitBundle
.getString("pull.retriving.remotes"));
253 * @return current local branch for the git or null
256 private String
currentBranch() {
257 String text
= myCurrentBranch
.getText();
258 return text
.equals(GitUIUtil
.NO_CURRENT_BRANCH
) ?
null : text
;
262 * Update remotes for the git root
264 private void updateRemotes() {
265 GitUIUtil
.setupRemotes(myProject
, gitRoot(), currentBranch(), myRemote
);
269 * @return a currently selected git root
271 public VirtualFile
gitRoot() {
272 return (VirtualFile
)myGitRoot
.getSelectedItem();
277 * Create branch chooser
279 private void createUIComponents() {
280 myBranchChooser
= new ElementsChooser
<String
>(true);
286 protected JComponent
createCenterPanel() {
294 protected String
getDimensionServiceKey() {
295 return getClass().getName();
302 protected String
getHelpId() {
303 return "reference.VersionControl.Git.Pull";
309 public String
getRemote() {
310 return ((JTextField
)myRemote
.getEditor().getEditorComponent()).getText();