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