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