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 package org.eclipse.jgit.junit;
48
49 import static org.junit.Assert.assertEquals;
50
51 import java.io.File;
52 import java.io.FileInputStream;
53 import java.io.FileNotFoundException;
54 import java.io.FileOutputStream;
55 import java.io.IOException;
56 import java.io.InputStreamReader;
57 import java.io.Reader;
58 import java.nio.file.Path;
59 import java.util.Map;
60
61 import org.eclipse.jgit.api.Git;
62 import org.eclipse.jgit.api.errors.GitAPIException;
63 import org.eclipse.jgit.dircache.DirCacheBuilder;
64 import org.eclipse.jgit.dircache.DirCacheCheckout;
65 import org.eclipse.jgit.dircache.DirCacheEntry;
66 import org.eclipse.jgit.internal.storage.file.FileRepository;
67 import org.eclipse.jgit.lib.Constants;
68 import org.eclipse.jgit.lib.FileMode;
69 import org.eclipse.jgit.lib.ObjectId;
70 import org.eclipse.jgit.lib.ObjectInserter;
71 import org.eclipse.jgit.lib.RefUpdate;
72 import org.eclipse.jgit.lib.Repository;
73 import org.eclipse.jgit.revwalk.RevCommit;
74 import org.eclipse.jgit.revwalk.RevWalk;
75 import org.eclipse.jgit.treewalk.FileTreeIterator;
76 import org.eclipse.jgit.util.FS;
77 import org.eclipse.jgit.util.FileUtils;
78 import org.junit.Before;
79
80
81
82
83
84
85
86 public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
87 protected static void copyFile(final File src, final File dst)
88 throws IOException {
89 final FileInputStream fis = new FileInputStream(src);
90 try {
91 final FileOutputStream fos = new FileOutputStream(dst);
92 try {
93 final byte[] buf = new byte[4096];
94 int r;
95 while ((r = fis.read(buf)) > 0) {
96 fos.write(buf, 0, r);
97 }
98 } finally {
99 fos.close();
100 }
101 } finally {
102 fis.close();
103 }
104 }
105
106 protected File writeTrashFile(final String name, final String data)
107 throws IOException {
108 return JGitTestUtil.writeTrashFile(db, name, data);
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 protected Path writeLink(final String link, final String target)
123 throws Exception {
124 return JGitTestUtil.writeLink(db, link, target);
125 }
126
127 protected File writeTrashFile(final String subdir, final String name,
128 final String data)
129 throws IOException {
130 return JGitTestUtil.writeTrashFile(db, subdir, name, data);
131 }
132
133 protected String read(final String name) throws IOException {
134 return JGitTestUtil.read(db, name);
135 }
136
137 protected boolean check(final String name) {
138 return JGitTestUtil.check(db, name);
139 }
140
141 protected void deleteTrashFile(final String name) throws IOException {
142 JGitTestUtil.deleteTrashFile(db, name);
143 }
144
145 protected static void checkFile(File f, final String checkData)
146 throws IOException {
147 Reader r = new InputStreamReader(new FileInputStream(f), "UTF-8");
148 try {
149 char[] data = new char[checkData.length()];
150 if (checkData.length() != r.read(data))
151 throw new IOException("Internal error reading file data from "+f);
152 assertEquals(checkData, new String(data));
153 } finally {
154 r.close();
155 }
156 }
157
158
159 protected FileRepository db;
160
161
162 protected File trash;
163
164 @Override
165 @Before
166 public void setUp() throws Exception {
167 super.setUp();
168 db = createWorkRepository();
169 trash = db.getWorkTree();
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 public String indexState(int includedOptions)
208 throws IllegalStateException, IOException {
209 return indexState(db, includedOptions);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 protected void resetIndex(FileTreeIterator treeItr)
229 throws FileNotFoundException, IOException {
230 try (ObjectInserter inserter = db.newObjectInserter()) {
231 DirCacheBuilder builder = db.lockDirCache().builder();
232 DirCacheEntry dce;
233
234 while (!treeItr.eof()) {
235 long len = treeItr.getEntryLength();
236
237 dce = new DirCacheEntry(treeItr.getEntryPathString());
238 dce.setFileMode(treeItr.getEntryFileMode());
239 dce.setLastModified(treeItr.getEntryLastModified());
240 dce.setLength((int) len);
241 FileInputStream in = new FileInputStream(
242 treeItr.getEntryFile());
243 dce.setObjectId(inserter.insert(Constants.OBJ_BLOB, len, in));
244 in.close();
245 builder.add(dce);
246 treeItr.next(1);
247 }
248 builder.commit();
249 inserter.flush();
250 }
251 }
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public static String lookup(Object l, String nameTemplate,
275 Map<Object, String> lookupTable) {
276 String name = lookupTable.get(l);
277 if (name == null) {
278 name = nameTemplate.replaceAll("%n",
279 Integer.toString(lookupTable.size()));
280 lookupTable.put(l, name);
281 }
282 return name;
283 }
284
285
286
287
288
289
290
291
292
293 public static String slashify(String str) {
294 str = str.replace('\\', '/');
295 return str;
296 }
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 public static long fsTick(File lastFile) throws InterruptedException,
315 IOException {
316 long sleepTime = 64;
317 FS fs = FS.DETECTED;
318 if (lastFile != null && !fs.exists(lastFile))
319 throw new FileNotFoundException(lastFile.getPath());
320 File tmp = File.createTempFile("FileTreeIteratorWithTimeControl", null);
321 try {
322 long startTime = (lastFile == null) ? fs.lastModified(tmp) : fs
323 .lastModified(lastFile);
324 long actTime = fs.lastModified(tmp);
325 while (actTime <= startTime) {
326 Thread.sleep(sleepTime);
327 sleepTime *= 2;
328 FileOutputStream fos = new FileOutputStream(tmp);
329 fos.close();
330 actTime = fs.lastModified(tmp);
331 }
332 return actTime;
333 } finally {
334 FileUtils.delete(tmp);
335 }
336 }
337
338 protected void createBranch(ObjectId objectId, String branchName)
339 throws IOException {
340 RefUpdate updateRef = db.updateRef(branchName);
341 updateRef.setNewObjectId(objectId);
342 updateRef.update();
343 }
344
345 protected void checkoutBranch(String branchName)
346 throws IllegalStateException, IOException {
347 try (RevWalk walk = new RevWalk(db)) {
348 RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
349 RevCommit branch = walk.parseCommit(db.resolve(branchName));
350 DirCacheCheckout dco = new DirCacheCheckout(db,
351 head.getTree().getId(), db.lockDirCache(),
352 branch.getTree().getId());
353 dco.setFailOnConflict(true);
354 dco.checkout();
355 }
356
357 RefUpdate refUpdate = db.updateRef(Constants.HEAD);
358 refUpdate.setRefLogMessage("checkout: moving to " + branchName, false);
359 refUpdate.link(branchName);
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 protected File writeTrashFiles(boolean ensureDistinctTimestamps,
379 String... contents)
380 throws IOException, InterruptedException {
381 File f = null;
382 for (int i = 0; i < contents.length; i++)
383 if (contents[i] != null) {
384 if (ensureDistinctTimestamps && (f != null))
385 fsTick(f);
386 f = writeTrashFile(Integer.toString(i), contents[i]);
387 }
388 return f;
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403 protected RevCommit commitFile(String filename, String contents, String branch) {
404 try (Git git = new Git(db)) {
405 Repository repo = git.getRepository();
406 String originalBranch = repo.getFullBranch();
407 boolean empty = repo.resolve(Constants.HEAD) == null;
408 if (!empty) {
409 if (repo.findRef(branch) == null)
410 git.branchCreate().setName(branch).call();
411 git.checkout().setName(branch).call();
412 }
413
414 writeTrashFile(filename, contents);
415 git.add().addFilepattern(filename).call();
416 RevCommit commit = git.commit()
417 .setMessage(branch + ": " + filename).call();
418
419 if (originalBranch != null)
420 git.checkout().setName(originalBranch).call();
421 else if (empty)
422 git.branchCreate().setName(branch).setStartPoint(commit).call();
423
424 return commit;
425 } catch (IOException e) {
426 throw new RuntimeException(e);
427 } catch (GitAPIException e) {
428 throw new RuntimeException(e);
429 }
430 }
431
432 protected DirCacheEntry createEntry(final String path, final FileMode mode) {
433 return createEntry(path, mode, DirCacheEntry.STAGE_0, path);
434 }
435
436 protected DirCacheEntry createEntry(final String path, final FileMode mode,
437 final String content) {
438 return createEntry(path, mode, DirCacheEntry.STAGE_0, content);
439 }
440
441 protected DirCacheEntry createEntry(final String path, final FileMode mode,
442 final int stage, final String content) {
443 final DirCacheEntry entry = new DirCacheEntry(path, stage);
444 entry.setFileMode(mode);
445 try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
446 entry.setObjectId(formatter.idFor(
447 Constants.OBJ_BLOB, Constants.encode(content)));
448 }
449 return entry;
450 }
451
452 public static void assertEqualsFile(File expected, File actual)
453 throws IOException {
454 assertEquals(expected.getCanonicalFile(), actual.getCanonicalFile());
455 }
456 }