1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.diffmergetool;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.util.Map;
20
21 import org.eclipse.jgit.diff.DiffEntry;
22
23
24
25
26
27 public class FileElement {
28
29
30
31
32
33 public enum Type {
34
35
36
37 LOCAL,
38
39
40
41 REMOTE,
42
43
44
45 MERGED,
46
47
48
49 BASE,
50
51
52
53 BACKUP
54 }
55
56 private final String path;
57
58 private final Type type;
59
60 private final File workDir;
61
62 private InputStream stream;
63
64 private File tempFile;
65
66
67
68
69
70
71
72
73
74 public FileElement(String path, Type type) {
75 this(path, type, null);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89 public FileElement(String path, Type type, File workDir) {
90 this(path, type, workDir, null);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public FileElement(String path, Type type, File workDir,
106 InputStream stream) {
107 this.path = path;
108 this.type = type;
109 this.workDir = workDir;
110 this.stream = stream;
111 }
112
113
114
115
116 public String getPath() {
117 return path;
118 }
119
120
121
122
123 public Type getType() {
124 return type;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public File getFile() throws IOException {
143
144
145 if ((tempFile != null) && (stream == null)) {
146 return tempFile;
147 }
148 File file = new File(workDir, path);
149
150
151 if ((stream != null) || isNullPath()) {
152 if (tempFile == null) {
153 tempFile = getTempFile(file, type.name(), null);
154 }
155 if (stream != null) {
156 copyFromStream(tempFile, stream);
157 }
158
159 stream = null;
160 return tempFile;
161 }
162 return file;
163 }
164
165
166
167
168
169
170 public boolean isNullPath() {
171 return path.equals(DiffEntry.DEV_NULL);
172 }
173
174
175
176
177
178
179
180
181
182
183 public File createTempFile(File directory) throws IOException {
184 if (tempFile == null) {
185 tempFile = getTempFile(new File(path), type.name(), directory);
186 }
187 return tempFile;
188 }
189
190
191
192
193 public void cleanTemporaries() {
194 if (tempFile != null && tempFile.exists()) {
195 tempFile.delete();
196 }
197 tempFile = null;
198 }
199
200
201
202
203
204
205
206
207
208 public String replaceVariable(String input) throws IOException {
209 return input.replace("$" + type.name(), getFile().getPath());
210 }
211
212
213
214
215
216
217
218
219 public void addToEnv(Map<String, String> env) throws IOException {
220 env.put(type.name(), getFile().getPath());
221 }
222
223 private static File getTempFile(final File file, final String midName,
224 final File workingDir) throws IOException {
225 String[] fileNameAndExtension = splitBaseFileNameAndExtension(file);
226
227
228 return File.createTempFile(
229 fileNameAndExtension[0] + "_" + midName + "_",
230 fileNameAndExtension[1], workingDir);
231 }
232
233 private static void copyFromStream(final File file,
234 final InputStream stream)
235 throws IOException, FileNotFoundException {
236 try (OutputStream outStream = new FileOutputStream(file)) {
237 int read = 0;
238 byte[] bytes = new byte[8 * 1024];
239 while ((read = stream.read(bytes)) != -1) {
240 outStream.write(bytes, 0, read);
241 }
242 } finally {
243
244 stream.close();
245 }
246 }
247
248 private static String[] splitBaseFileNameAndExtension(File file) {
249 String[] result = new String[2];
250 result[0] = file.getName();
251 result[1] = "";
252 int idx = result[0].lastIndexOf(".");
253
254
255 if (idx > 0) {
256 result[1] = result[0].substring(idx, result[0].length());
257 result[0] = result[0].substring(0, idx);
258 }
259 return result;
260 }
261
262 }