/
GitDefineRemoteDialog.java
143 lines (126 loc) · 5.3 KB
/
GitDefineRemoteDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package git4idea.remote;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.GridBag;
import com.intellij.util.ui.UIUtil;
import git4idea.commands.Git;
import git4idea.commands.GitCommandResult;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import git4idea.validators.GitRefNameValidator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import static com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile;
import static com.intellij.util.containers.ContainerUtil.exists;
import static git4idea.GitUtil.mention;
public class GitDefineRemoteDialog extends DialogWrapper {
private static final Logger LOG = Logger.getInstance(GitDefineRemoteDialog.class);
@NotNull private final GitRepository myRepository;
@NotNull private final Git myGit;
@NotNull private final String myInitialRemoteName;
@NotNull private final JTextField myRemoteName;
@NotNull private final JTextField myRemoteUrl;
public GitDefineRemoteDialog(@NotNull GitRepository repository, @NotNull Git git) {
this(repository, git, GitRemote.ORIGIN, "");
}
public GitDefineRemoteDialog(@NotNull GitRepository repository,
@NotNull Git git,
@NotNull String initialRemoteName,
@NotNull String initialRemoteUrl) {
super(repository.getProject());
myRepository = repository;
myGit = git;
myRemoteName = new JTextField(initialRemoteName, 20);
myInitialRemoteName = initialRemoteName;
myRemoteUrl = new JTextField(initialRemoteUrl, 20);
setTitle("Define Remote" + mention(myRepository));
init();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel defineRemoteComponent = new JPanel(new GridBagLayout());
GridBag gb = new GridBag().
setDefaultAnchor(GridBagConstraints.LINE_START).
setDefaultInsets(UIUtil.DEFAULT_VGAP, UIUtil.DEFAULT_HGAP, 0, 0);
defineRemoteComponent.add(new JBLabel("Name:"), gb.nextLine().next().anchor(GridBagConstraints.EAST));
defineRemoteComponent.add(myRemoteName, gb.next());
defineRemoteComponent.add(new JBLabel("URL: "),
gb.nextLine().next().anchor(GridBagConstraints.EAST).insets(0, UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP, 0));
defineRemoteComponent.add(myRemoteUrl, gb.next());
return defineRemoteComponent;
}
@NotNull
public String getRemoteName() {
return StringUtil.notNullize(myRemoteName.getText()).trim();
}
@NotNull
public String getRemoteUrl() {
return StringUtil.notNullize(myRemoteUrl.getText()).trim();
}
@Nullable
@Override
public JComponent getPreferredFocusedComponent() {
return myRemoteName.getText().isEmpty() ? myRemoteName : myRemoteUrl;
}
@Override
protected void doOKAction() {
String url = getRemoteUrl();
String error = validateRemoteUnderModal(url);
if (error != null) {
LOG.warn(String.format("Invalid remote. Name: [%s], URL: [%s], error: %s", getRemoteName(), url, error));
Messages.showErrorDialog(myRepository.getProject(), error, "Invalid Remote");
}
else {
super.doOKAction();
}
}
@Nullable
@Override
protected ValidationInfo doValidate() {
String name = getRemoteName();
if (name.isEmpty()) {
return new ValidationInfo("Remote name can't be empty", myRemoteName);
}
if (getRemoteUrl().isEmpty()) {
return new ValidationInfo("Remote URL can't be empty", myRemoteUrl);
}
if (!GitRefNameValidator.getInstance().checkInput(name)) {
return new ValidationInfo("Remote name contains illegal characters", myRemoteName);
}
if (!name.equals(myInitialRemoteName) && exists(myRepository.getRemotes(), remote -> remote.getName().equals(name))) {
return new ValidationInfo("Remote name '" + name + "' is already in use", myRemoteName);
}
return null;
}
@Nullable
private String validateRemoteUnderModal(@NotNull final String url) throws ProcessCanceledException {
return ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
GitCommandResult result = myGit.lsRemote(myRepository.getProject(), virtualToIoFile(myRepository.getRoot()), url);
return !result.success() ? "Remote URL test failed: " + result.getErrorOutputAsHtmlString() : null;
}, "Checking URL...", true, myRepository.getProject());
}
}