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