1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.diffmergetool;
11
12 import java.util.TreeMap;
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.LinkedHashSet;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
19
20 import org.eclipse.jgit.attributes.Attributes;
21 import org.eclipse.jgit.errors.RevisionSyntaxException;
22 import org.eclipse.jgit.lib.Constants;
23 import org.eclipse.jgit.lib.Repository;
24 import org.eclipse.jgit.treewalk.FileTreeIterator;
25 import org.eclipse.jgit.treewalk.TreeWalk;
26 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
27 import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
28 import org.eclipse.jgit.util.FS;
29
30
31
32
33 public class ExternalToolUtils {
34
35
36
37
38 public static final String KEY_MERGE_TOOL = "mergetool";
39
40
41
42
43 public static final String KEY_DIFF_TOOL = "difftool";
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static String prepareCommand(String command, FileElement localFile,
62 FileElement remoteFile, FileElement mergedFile,
63 FileElement baseFile) throws IOException {
64 if (localFile != null) {
65 command = localFile.replaceVariable(command);
66 }
67 if (remoteFile != null) {
68 command = remoteFile.replaceVariable(command);
69 }
70 if (mergedFile != null) {
71 command = mergedFile.replaceVariable(command);
72 }
73 if (baseFile != null) {
74 command = baseFile.replaceVariable(command);
75 }
76 return command;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public static Map<String, String> prepareEnvironment(File gitDir,
96 FileElement localFile, FileElement remoteFile,
97 FileElement mergedFile, FileElement baseFile) throws IOException {
98 Map<String, String> env = new TreeMap<>();
99 if (gitDir != null) {
100 env.put(Constants.GIT_DIR_KEY, gitDir.getAbsolutePath());
101 }
102 if (localFile != null) {
103 localFile.addToEnv(env);
104 }
105 if (remoteFile != null) {
106 remoteFile.addToEnv(env);
107 }
108 if (mergedFile != null) {
109 mergedFile.addToEnv(env);
110 }
111 if (baseFile != null) {
112 baseFile.addToEnv(env);
113 }
114 return env;
115 }
116
117
118
119
120
121
122 @SuppressWarnings("nls")
123 public static String quotePath(String path) {
124
125 if (path.contains(" ")) {
126
127 if (!path.startsWith("\"")) {
128 path = "\"" + path;
129 }
130
131 if (!path.endsWith("\"")) {
132 path = path + "\"";
133 }
134 }
135 return path;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 public static boolean isToolAvailable(FS fs, File gitDir, File directory,
150 String path) {
151 boolean available = true;
152 try {
153 CommandExecutor cmdExec = new CommandExecutor(fs, false);
154 available = cmdExec.checkExecutable(path, directory,
155 prepareEnvironment(gitDir, null, null, null, null));
156 } catch (Exception e) {
157 available = false;
158 }
159 return available;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 public static Set<String> createSortedToolSet(String defaultName,
174 Set<String> userDefinedNames, Set<String> preDefinedNames) {
175 Set<String> names = new LinkedHashSet<>();
176 if (defaultName != null) {
177
178 Set<String> namesPredef = new LinkedHashSet<>();
179 Set<String> namesUser = new LinkedHashSet<>();
180 namesUser.addAll(userDefinedNames);
181 namesUser.remove(defaultName);
182 namesPredef.addAll(preDefinedNames);
183 namesPredef.remove(defaultName);
184
185 names.add(defaultName);
186 names.addAll(namesUser);
187 names.addAll(namesPredef);
188 } else {
189 names.addAll(userDefinedNames);
190 names.addAll(preDefinedNames);
191 }
192 return names;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public static Optional<String> getExternalToolFromAttributes(
214 final Repository repository, final String path,
215 final String toolKey) throws ToolException {
216 try {
217 WorkingTreeIterator treeIterator = new FileTreeIterator(repository);
218 try (TreeWalk walk = new TreeWalk(repository)) {
219 walk.addTree(treeIterator);
220 walk.setFilter(new NotIgnoredFilter(0));
221 while (walk.next()) {
222 String treePath = walk.getPathString();
223 if (treePath.equals(path)) {
224 Attributes attrs = walk.getAttributes();
225 if (attrs.containsKey(toolKey)) {
226 return Optional.of(attrs.getValue(toolKey));
227 }
228 }
229 if (walk.isSubtree()) {
230 walk.enterSubtree();
231 }
232 }
233
234 return Optional.empty();
235 }
236
237 } catch (RevisionSyntaxException | IOException e) {
238 throw new ToolException(e);
239 }
240 }
241
242 }