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.internal.storage.file;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertFalse;
51 import static org.junit.Assert.assertNotNull;
52 import static org.junit.Assert.assertNotSame;
53 import static org.junit.Assert.assertTrue;
54 import static org.junit.Assert.fail;
55
56 import java.io.File;
57 import java.io.FileInputStream;
58 import java.io.FileReader;
59 import java.io.IOException;
60 import java.io.UnsupportedEncodingException;
61
62 import org.eclipse.jgit.errors.ConfigInvalidException;
63 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
64 import org.eclipse.jgit.errors.MissingObjectException;
65 import org.eclipse.jgit.internal.JGitText;
66 import org.eclipse.jgit.lib.AnyObjectId;
67 import org.eclipse.jgit.lib.CommitBuilder;
68 import org.eclipse.jgit.lib.Constants;
69 import org.eclipse.jgit.lib.FileMode;
70 import org.eclipse.jgit.lib.FileTreeEntry;
71 import org.eclipse.jgit.lib.ObjectDatabase;
72 import org.eclipse.jgit.lib.ObjectId;
73 import org.eclipse.jgit.lib.ObjectInserter;
74 import org.eclipse.jgit.lib.PersonIdent;
75 import org.eclipse.jgit.lib.RefUpdate;
76 import org.eclipse.jgit.lib.Repository;
77 import org.eclipse.jgit.lib.TagBuilder;
78 import org.eclipse.jgit.lib.Tree;
79 import org.eclipse.jgit.lib.TreeFormatter;
80 import org.eclipse.jgit.revwalk.RevCommit;
81 import org.eclipse.jgit.revwalk.RevTag;
82 import org.eclipse.jgit.revwalk.RevWalk;
83 import org.eclipse.jgit.storage.file.FileBasedConfig;
84 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
85 import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
86 import org.eclipse.jgit.util.FileUtils;
87 import org.junit.Test;
88
89 @SuppressWarnings("deprecation")
90 public class T0003_BasicTest extends SampleDataRepositoryTestCase {
91
92 @Test
93 public void test001_Initalize() {
94 final File gitdir = new File(trash, Constants.DOT_GIT);
95 final File hooks = new File(gitdir, "hooks");
96 final File objects = new File(gitdir, "objects");
97 final File objects_pack = new File(objects, "pack");
98 final File objects_info = new File(objects, "info");
99 final File refs = new File(gitdir, "refs");
100 final File refs_heads = new File(refs, "heads");
101 final File refs_tags = new File(refs, "tags");
102 final File HEAD = new File(gitdir, "HEAD");
103
104 assertTrue("Exists " + trash, trash.isDirectory());
105 assertTrue("Exists " + hooks, hooks.isDirectory());
106 assertTrue("Exists " + objects, objects.isDirectory());
107 assertTrue("Exists " + objects_pack, objects_pack.isDirectory());
108 assertTrue("Exists " + objects_info, objects_info.isDirectory());
109 assertEquals(2L, objects.listFiles().length);
110 assertTrue("Exists " + refs, refs.isDirectory());
111 assertTrue("Exists " + refs_heads, refs_heads.isDirectory());
112 assertTrue("Exists " + refs_tags, refs_tags.isDirectory());
113 assertTrue("Exists " + HEAD, HEAD.isFile());
114 assertEquals(23, HEAD.length());
115 }
116
117 @Test
118 public void test000_openRepoBadArgs() throws IOException {
119 try {
120 new FileRepositoryBuilder().build();
121 fail("Must pass either GIT_DIR or GIT_WORK_TREE");
122 } catch (IllegalArgumentException e) {
123 assertEquals(JGitText.get().eitherGitDirOrWorkTreeRequired, e
124 .getMessage());
125 }
126 }
127
128
129
130
131
132
133
134 @Test
135 public void test000_openrepo_default_gitDirSet() throws IOException {
136 File repo1Parent = new File(trash.getParentFile(), "r1");
137 Repository repo1initial = new FileRepository(new File(repo1Parent,
138 Constants.DOT_GIT));
139 repo1initial.create();
140 repo1initial.close();
141
142 File theDir = new File(repo1Parent, Constants.DOT_GIT);
143 FileRepository r = (FileRepository) new FileRepositoryBuilder()
144 .setGitDir(theDir).build();
145 assertEqualsPath(theDir, r.getDirectory());
146 assertEqualsPath(repo1Parent, r.getWorkTree());
147 assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
148 assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
149 .getDirectory());
150 }
151
152
153
154
155
156
157
158 @Test
159 public void test000_openrepo_default_gitDirAndWorkTreeSet()
160 throws IOException {
161 File repo1Parent = new File(trash.getParentFile(), "r1");
162 Repository repo1initial = new FileRepository(new File(repo1Parent,
163 Constants.DOT_GIT));
164 repo1initial.create();
165 repo1initial.close();
166
167 File theDir = new File(repo1Parent, Constants.DOT_GIT);
168 FileRepository r = (FileRepository) new FileRepositoryBuilder()
169 .setGitDir(theDir).setWorkTree(repo1Parent.getParentFile())
170 .build();
171 assertEqualsPath(theDir, r.getDirectory());
172 assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree());
173 assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
174 assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
175 .getDirectory());
176 }
177
178
179
180
181
182
183
184 @Test
185 public void test000_openrepo_default_workDirSet() throws IOException {
186 File repo1Parent = new File(trash.getParentFile(), "r1");
187 Repository repo1initial = new FileRepository(new File(repo1Parent,
188 Constants.DOT_GIT));
189 repo1initial.create();
190 repo1initial.close();
191
192 File theDir = new File(repo1Parent, Constants.DOT_GIT);
193 FileRepository r = (FileRepository) new FileRepositoryBuilder()
194 .setWorkTree(repo1Parent).build();
195 assertEqualsPath(theDir, r.getDirectory());
196 assertEqualsPath(repo1Parent, r.getWorkTree());
197 assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
198 assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
199 .getDirectory());
200 }
201
202
203
204
205
206
207 @Test
208 public void test000_openrepo_default_absolute_workdirconfig()
209 throws IOException {
210 File repo1Parent = new File(trash.getParentFile(), "r1");
211 File workdir = new File(trash.getParentFile(), "rw");
212 FileUtils.mkdir(workdir);
213 FileRepository repo1initial = new FileRepository(new File(repo1Parent,
214 Constants.DOT_GIT));
215 repo1initial.create();
216 final FileBasedConfig cfg = repo1initial.getConfig();
217 cfg.setString("core", null, "worktree", workdir.getAbsolutePath());
218 cfg.save();
219 repo1initial.close();
220
221 File theDir = new File(repo1Parent, Constants.DOT_GIT);
222 FileRepository r = (FileRepository) new FileRepositoryBuilder()
223 .setGitDir(theDir).build();
224 assertEqualsPath(theDir, r.getDirectory());
225 assertEqualsPath(workdir, r.getWorkTree());
226 assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
227 assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
228 .getDirectory());
229 }
230
231
232
233
234
235
236 @Test
237 public void test000_openrepo_default_relative_workdirconfig()
238 throws IOException {
239 File repo1Parent = new File(trash.getParentFile(), "r1");
240 File workdir = new File(trash.getParentFile(), "rw");
241 FileUtils.mkdir(workdir);
242 FileRepository repo1initial = new FileRepository(new File(repo1Parent,
243 Constants.DOT_GIT));
244 repo1initial.create();
245 final FileBasedConfig cfg = repo1initial.getConfig();
246 cfg.setString("core", null, "worktree", "../../rw");
247 cfg.save();
248 repo1initial.close();
249
250 File theDir = new File(repo1Parent, Constants.DOT_GIT);
251 FileRepository r = (FileRepository) new FileRepositoryBuilder()
252 .setGitDir(theDir).build();
253 assertEqualsPath(theDir, r.getDirectory());
254 assertEqualsPath(workdir, r.getWorkTree());
255 assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
256 assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
257 .getDirectory());
258 }
259
260
261
262
263
264
265
266 @Test
267 public void test000_openrepo_alternate_index_file_and_objdirs()
268 throws IOException {
269 File repo1Parent = new File(trash.getParentFile(), "r1");
270 File indexFile = new File(trash, "idx");
271 File objDir = new File(trash, "../obj");
272 File altObjDir = db.getObjectDatabase().getDirectory();
273 Repository repo1initial = new FileRepository(new File(repo1Parent,
274 Constants.DOT_GIT));
275 repo1initial.create();
276 repo1initial.close();
277
278 File theDir = new File(repo1Parent, Constants.DOT_GIT);
279 FileRepository r = (FileRepository) new FileRepositoryBuilder()
280 .setGitDir(theDir).setObjectDirectory(objDir)
281 .addAlternateObjectDirectory(altObjDir)
282 .setIndexFile(indexFile)
283 .build();
284 assertEqualsPath(theDir, r.getDirectory());
285 assertEqualsPath(theDir.getParentFile(), r.getWorkTree());
286 assertEqualsPath(indexFile, r.getIndexFile());
287 assertEqualsPath(objDir, r.getObjectDatabase().getDirectory());
288 assertNotNull(r.open(ObjectId
289 .fromString("6db9c2ebf75590eef973081736730a9ea169a0c4")));
290
291
292 r.close();
293 }
294
295 protected void assertEqualsPath(File expected, File actual)
296 throws IOException {
297 assertEquals(expected.getCanonicalPath(), actual.getCanonicalPath());
298 }
299
300 @Test
301 public void test002_WriteEmptyTree() throws IOException {
302
303
304
305
306 final Repository newdb = createBareRepository();
307 try (final ObjectInserter oi = newdb.newObjectInserter()) {
308 final ObjectId treeId = oi.insert(new TreeFormatter());
309 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904",
310 treeId.name());
311 }
312
313 final File o = new File(new File(new File(newdb.getDirectory(),
314 "objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904");
315 assertTrue("Exists " + o, o.isFile());
316 assertTrue("Read-only " + o, !o.canWrite());
317 }
318
319 @Test
320 public void test002_WriteEmptyTree2() throws IOException {
321
322
323 final ObjectId treeId = insertTree(new TreeFormatter());
324 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", treeId.name());
325 final File o = new File(new File(
326 new File(db.getDirectory(), "objects"), "4b"),
327 "825dc642cb6eb9a060e54bf8d69288fbee4904");
328 assertFalse("Exists " + o, o.isFile());
329 }
330
331 @Test
332 public void test006_ReadUglyConfig() throws IOException,
333 ConfigInvalidException {
334 final File cfg = new File(db.getDirectory(), Constants.CONFIG);
335 final FileBasedConfig c = new FileBasedConfig(cfg, db.getFS());
336 final String configStr = " [core];comment\n\tfilemode = yes\n"
337 + "[user]\n"
338 + " email = A U Thor <thor@example.com> # Just an example...\n"
339 + " name = \"A Thor \\\\ \\\"\\t \"\n"
340 + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
341 + " to test\n";
342 write(cfg, configStr);
343 c.load();
344 assertEquals("yes", c.getString("core", null, "filemode"));
345 assertEquals("A U Thor <thor@example.com>", c.getString("user", null,
346 "email"));
347 assertEquals("A Thor \\ \"\t ", c.getString("user", null, "name"));
348 assertEquals("a many line\ncomment\n to test", c.getString("user",
349 null, "defaultCheckInComment"));
350 c.save();
351 final FileReader fr = new FileReader(cfg);
352 final char[] cbuf = new char[configStr.length()];
353 fr.read(cbuf);
354 fr.close();
355 assertEquals(configStr, new String(cbuf));
356 }
357
358 @Test
359 public void test007_Open() throws IOException {
360 final FileRepository db2 = new FileRepository(db.getDirectory());
361 assertEquals(db.getDirectory(), db2.getDirectory());
362 assertEquals(db.getObjectDatabase().getDirectory(), db2
363 .getObjectDatabase().getDirectory());
364 assertNotSame(db.getConfig(), db2.getConfig());
365 }
366
367 @Test
368 public void test008_FailOnWrongVersion() throws IOException {
369 final File cfg = new File(db.getDirectory(), Constants.CONFIG);
370 final String badvers = "ihopethisisneveraversion";
371 final String configStr = "[core]\n" + "\trepositoryFormatVersion="
372 + badvers + "\n";
373 write(cfg, configStr);
374
375 try {
376 new FileRepository(db.getDirectory());
377 fail("incorrectly opened a bad repository");
378 } catch (IllegalArgumentException ioe) {
379 assertNotNull(ioe.getMessage());
380 }
381 }
382
383 @Test
384 public void test009_CreateCommitOldFormat() throws IOException {
385 final ObjectId treeId = insertTree(new TreeFormatter());
386 final CommitBuilder c = new CommitBuilder();
387 c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
388 c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
389 c.setMessage("A Commit\n");
390 c.setTreeId(treeId);
391 assertEquals(treeId, c.getTreeId());
392
393 ObjectId actid = insertCommit(c);
394
395 final ObjectId cmtid = ObjectId
396 .fromString("9208b2459ea6609a5af68627cc031796d0d9329b");
397 assertEquals(cmtid, actid);
398
399
400 ObjectDatabase odb = db.getObjectDatabase();
401 assertTrue("is ObjectDirectory", odb instanceof ObjectDirectory);
402 final XInputStream xis = new XInputStream(new FileInputStream(
403 ((ObjectDirectory) odb).fileFor(cmtid)));
404 try {
405 assertEquals(0x78, xis.readUInt8());
406 assertEquals(0x9c, xis.readUInt8());
407 assertEquals(0, 0x789c % 31);
408 } finally {
409 xis.close();
410 }
411
412
413 RevCommit c2 = parseCommit(actid);
414 assertNotNull(c2);
415 assertEquals(c.getMessage(), c2.getFullMessage());
416 assertEquals(c.getTreeId(), c2.getTree());
417 assertEquals(c.getAuthor(), c2.getAuthorIdent());
418 assertEquals(c.getCommitter(), c2.getCommitterIdent());
419 }
420
421 @Test
422 public void test012_SubtreeExternalSorting() throws IOException {
423 final ObjectId emptyBlob = insertEmptyBlob();
424 final Tree t = new Tree(db);
425 final FileTreeEntry e0 = t.addFile("a-");
426 final FileTreeEntry e1 = t.addFile("a-b");
427 final FileTreeEntry e2 = t.addFile("a/b");
428 final FileTreeEntry e3 = t.addFile("a=");
429 final FileTreeEntry e4 = t.addFile("a=b");
430
431 e0.setId(emptyBlob);
432 e1.setId(emptyBlob);
433 e2.setId(emptyBlob);
434 e3.setId(emptyBlob);
435 e4.setId(emptyBlob);
436
437 final Tree a = (Tree) t.findTreeMember("a");
438 a.setId(insertTree(a));
439 assertEquals(ObjectId
440 .fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
441 insertTree(t));
442 }
443
444 @Test
445 public void test020_createBlobTag() throws IOException {
446 final ObjectId emptyId = insertEmptyBlob();
447 final TagBuilder t = new TagBuilder();
448 t.setObjectId(emptyId, Constants.OBJ_BLOB);
449 t.setTag("test020");
450 t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
451 t.setMessage("test020 tagged\n");
452 ObjectId actid = insertTag(t);
453 assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", actid.name());
454
455 RevTag mapTag = parseTag(actid);
456 assertEquals(Constants.OBJ_BLOB, mapTag.getObject().getType());
457 assertEquals("test020 tagged\n", mapTag.getFullMessage());
458 assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
459 .getTaggerIdent());
460 assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag
461 .getObject().getId().name());
462 }
463
464 @Test
465 public void test021_createTreeTag() throws IOException {
466 final ObjectId emptyId = insertEmptyBlob();
467 final Tree almostEmptyTree = new Tree(db);
468 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
469 "empty".getBytes(), false));
470 final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
471 final TagBuilder t = new TagBuilder();
472 t.setObjectId(almostEmptyTreeId, Constants.OBJ_TREE);
473 t.setTag("test021");
474 t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
475 t.setMessage("test021 tagged\n");
476 ObjectId actid = insertTag(t);
477 assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", actid.name());
478
479 RevTag mapTag = parseTag(actid);
480 assertEquals(Constants.OBJ_TREE, mapTag.getObject().getType());
481 assertEquals("test021 tagged\n", mapTag.getFullMessage());
482 assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
483 .getTaggerIdent());
484 assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag
485 .getObject().getId().name());
486 }
487
488 @Test
489 public void test022_createCommitTag() throws IOException {
490 final ObjectId emptyId = insertEmptyBlob();
491 final Tree almostEmptyTree = new Tree(db);
492 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
493 "empty".getBytes(), false));
494 final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
495 final CommitBuilder almostEmptyCommit = new CommitBuilder();
496 almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L,
497 -2 * 60));
498 almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L,
499 -2 * 60));
500 almostEmptyCommit.setMessage("test022\n");
501 almostEmptyCommit.setTreeId(almostEmptyTreeId);
502 ObjectId almostEmptyCommitId = insertCommit(almostEmptyCommit);
503 final TagBuilder t = new TagBuilder();
504 t.setObjectId(almostEmptyCommitId, Constants.OBJ_COMMIT);
505 t.setTag("test022");
506 t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
507 t.setMessage("test022 tagged\n");
508 ObjectId actid = insertTag(t);
509 assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", actid.name());
510
511 RevTag mapTag = parseTag(actid);
512 assertEquals(Constants.OBJ_COMMIT, mapTag.getObject().getType());
513 assertEquals("test022 tagged\n", mapTag.getFullMessage());
514 assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
515 .getTaggerIdent());
516 assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag
517 .getObject().getId().name());
518 }
519
520 @Test
521 public void test023_createCommitNonAnullii() throws IOException {
522 final ObjectId emptyId = insertEmptyBlob();
523 final Tree almostEmptyTree = new Tree(db);
524 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
525 "empty".getBytes(), false));
526 final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
527 CommitBuilder commit = new CommitBuilder();
528 commit.setTreeId(almostEmptyTreeId);
529 commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
530 4294967295000L, 60));
531 commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
532 4294967295000L, 60));
533 commit.setEncoding("UTF-8");
534 commit.setMessage("\u00dcbergeeks");
535 ObjectId cid = insertCommit(commit);
536 assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
537
538 RevCommit loadedCommit = parseCommit(cid);
539 assertEquals(commit.getMessage(), loadedCommit.getFullMessage());
540 }
541
542 @Test
543 public void test024_createCommitNonAscii() throws IOException {
544 final ObjectId emptyId = insertEmptyBlob();
545 final Tree almostEmptyTree = new Tree(db);
546 almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
547 "empty".getBytes(), false));
548 final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
549 CommitBuilder commit = new CommitBuilder();
550 commit.setTreeId(almostEmptyTreeId);
551 commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
552 4294967295000L, 60));
553 commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
554 4294967295000L, 60));
555 commit.setEncoding("ISO-8859-1");
556 commit.setMessage("\u00dcbergeeks");
557 ObjectId cid = insertCommit(commit);
558 assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
559 }
560
561 @Test
562 public void test025_computeSha1NoStore() throws IOException {
563 byte[] data = "test025 some data, more than 16 bytes to get good coverage"
564 .getBytes("ISO-8859-1");
565 final ObjectId id = new ObjectInserter.Formatter().idFor(
566 Constants.OBJ_BLOB, data);
567 assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
568 }
569
570 @Test
571 public void test026_CreateCommitMultipleparents() throws IOException {
572 final ObjectId treeId;
573 try (final ObjectInserter oi = db.newObjectInserter()) {
574 final ObjectId blobId = oi.insert(Constants.OBJ_BLOB,
575 "and this is the data in me\n".getBytes(Constants.CHARSET
576 .name()));
577 TreeFormatter fmt = new TreeFormatter();
578 fmt.append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
579 treeId = oi.insert(fmt);
580 oi.flush();
581 }
582 assertEquals(ObjectId
583 .fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"), treeId);
584
585 final CommitBuilder c1 = new CommitBuilder();
586 c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
587 c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
588 c1.setMessage("A Commit\n");
589 c1.setTreeId(treeId);
590 assertEquals(treeId, c1.getTreeId());
591 ObjectId actid1 = insertCommit(c1);
592 final ObjectId cmtid1 = ObjectId
593 .fromString("803aec4aba175e8ab1d666873c984c0308179099");
594 assertEquals(cmtid1, actid1);
595
596 final CommitBuilder c2 = new CommitBuilder();
597 c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
598 c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
599 c2.setMessage("A Commit 2\n");
600 c2.setTreeId(treeId);
601 assertEquals(treeId, c2.getTreeId());
602 c2.setParentIds(actid1);
603 ObjectId actid2 = insertCommit(c2);
604 final ObjectId cmtid2 = ObjectId
605 .fromString("95d068687c91c5c044fb8c77c5154d5247901553");
606 assertEquals(cmtid2, actid2);
607
608 RevCommit rm2 = parseCommit(cmtid2);
609 assertNotSame(c2, rm2);
610
611 assertEquals(c2.getAuthor(), rm2.getAuthorIdent());
612 assertEquals(actid2, rm2.getId());
613 assertEquals(c2.getMessage(), rm2.getFullMessage());
614 assertEquals(c2.getTreeId(), rm2.getTree().getId());
615 assertEquals(1, rm2.getParentCount());
616 assertEquals(actid1, rm2.getParent(0));
617
618 final CommitBuilder c3 = new CommitBuilder();
619 c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
620 c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
621 c3.setMessage("A Commit 3\n");
622 c3.setTreeId(treeId);
623 assertEquals(treeId, c3.getTreeId());
624 c3.setParentIds(actid1, actid2);
625 ObjectId actid3 = insertCommit(c3);
626 final ObjectId cmtid3 = ObjectId
627 .fromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
628 assertEquals(cmtid3, actid3);
629
630 RevCommit rm3 = parseCommit(cmtid3);
631 assertNotSame(c3, rm3);
632
633 assertEquals(c3.getAuthor(), rm3.getAuthorIdent());
634 assertEquals(actid3, rm3.getId());
635 assertEquals(c3.getMessage(), rm3.getFullMessage());
636 assertEquals(c3.getTreeId(), rm3.getTree().getId());
637 assertEquals(2, rm3.getParentCount());
638 assertEquals(actid1, rm3.getParent(0));
639 assertEquals(actid2, rm3.getParent(1));
640
641 final CommitBuilder c4 = new CommitBuilder();
642 c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
643 c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
644 c4.setMessage("A Commit 4\n");
645 c4.setTreeId(treeId);
646 assertEquals(treeId, c3.getTreeId());
647 c4.setParentIds(actid1, actid2, actid3);
648 ObjectId actid4 = insertCommit(c4);
649 final ObjectId cmtid4 = ObjectId
650 .fromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
651 assertEquals(cmtid4, actid4);
652
653 RevCommit rm4 = parseCommit(cmtid4);
654 assertNotSame(c4, rm3);
655
656 assertEquals(c4.getAuthor(), rm4.getAuthorIdent());
657 assertEquals(actid4, rm4.getId());
658 assertEquals(c4.getMessage(), rm4.getFullMessage());
659 assertEquals(c4.getTreeId(), rm4.getTree().getId());
660 assertEquals(3, rm4.getParentCount());
661 assertEquals(actid1, rm4.getParent(0));
662 assertEquals(actid2, rm4.getParent(1));
663 assertEquals(actid3, rm4.getParent(2));
664 }
665
666 @Test
667 public void test027_UnpackedRefHigherPriorityThanPacked()
668 throws IOException {
669 String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
670 write(new File(db.getDirectory(), "refs/heads/a"), unpackedId + "\n");
671
672 ObjectId resolved = db.resolve("refs/heads/a");
673 assertEquals(unpackedId, resolved.name());
674 }
675
676 @Test
677 public void test028_LockPackedRef() throws IOException {
678 writeTrashFile(".git/packed-refs",
679 "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
680 writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
681 BUG_WorkAroundRacyGitIssues("packed-refs");
682 BUG_WorkAroundRacyGitIssues("HEAD");
683
684 ObjectId resolve = db.resolve("HEAD");
685 assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
686
687 RefUpdate lockRef = db.updateRef("HEAD");
688 ObjectId newId = ObjectId
689 .fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
690 lockRef.setNewObjectId(newId);
691 assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
692
693 assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
694 assertEquals(newId, db.resolve("refs/heads/foobar"));
695
696
697 RefUpdate lockRef2 = db.updateRef("HEAD");
698 ObjectId newId2 = ObjectId
699 .fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
700 lockRef2.setNewObjectId(newId2);
701 assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
702
703 assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
704 assertEquals(newId2, db.resolve("refs/heads/foobar"));
705 }
706
707 @Test
708 public void test30_stripWorkDir() {
709 File relCwd = new File(".");
710 File absCwd = relCwd.getAbsoluteFile();
711 File absBase = new File(new File(absCwd, "repo"), "workdir");
712 File relBase = new File(new File(relCwd, "repo"), "workdir");
713 assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
714
715 File relBaseFile = new File(new File(relBase, "other"), "module.c");
716 File absBaseFile = new File(new File(absBase, "other"), "module.c");
717 assertEquals("other/module.c", Repository.stripWorkDir(relBase,
718 relBaseFile));
719 assertEquals("other/module.c", Repository.stripWorkDir(relBase,
720 absBaseFile));
721 assertEquals("other/module.c", Repository.stripWorkDir(absBase,
722 relBaseFile));
723 assertEquals("other/module.c", Repository.stripWorkDir(absBase,
724 absBaseFile));
725
726 File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
727 File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
728 assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
729 assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
730
731 assertEquals("", Repository.stripWorkDir(db.getWorkTree(), db
732 .getWorkTree()));
733
734 File file = new File(new File(db.getWorkTree(), "subdir"), "File.java");
735 assertEquals("subdir/File.java", Repository.stripWorkDir(db
736 .getWorkTree(), file));
737
738 }
739
740 private ObjectId insertEmptyBlob() throws IOException {
741 final ObjectId emptyId;
742 try (ObjectInserter oi = db.newObjectInserter()) {
743 emptyId = oi.insert(Constants.OBJ_BLOB, new byte[] {});
744 oi.flush();
745 }
746 return emptyId;
747 }
748
749 private ObjectId insertTree(Tree tree) throws IOException {
750 try (ObjectInserter oi = db.newObjectInserter()) {
751 ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
752 oi.flush();
753 return id;
754 }
755 }
756
757 private ObjectId insertTree(TreeFormatter tree) throws IOException {
758 try (ObjectInserter oi = db.newObjectInserter()) {
759 ObjectId id = oi.insert(tree);
760 oi.flush();
761 return id;
762 }
763 }
764
765 private ObjectId insertCommit(final CommitBuilder builder)
766 throws IOException, UnsupportedEncodingException {
767 try (ObjectInserter oi = db.newObjectInserter()) {
768 ObjectId id = oi.insert(builder);
769 oi.flush();
770 return id;
771 }
772 }
773
774 private RevCommit parseCommit(AnyObjectId id)
775 throws MissingObjectException, IncorrectObjectTypeException,
776 IOException {
777 try (RevWalk rw = new RevWalk(db)) {
778 return rw.parseCommit(id);
779 }
780 }
781
782 private ObjectId insertTag(final TagBuilder tag) throws IOException,
783 UnsupportedEncodingException {
784 try (ObjectInserter oi = db.newObjectInserter()) {
785 ObjectId id = oi.insert(tag);
786 oi.flush();
787 return id;
788 }
789 }
790
791 private RevTag parseTag(AnyObjectId id) throws MissingObjectException,
792 IncorrectObjectTypeException, IOException {
793 try (RevWalk rw = new RevWalk(db)) {
794 return rw.parseTag(id);
795 }
796 }
797
798
799
800
801
802
803
804
805
806
807
808
809 private void BUG_WorkAroundRacyGitIssues(String name) {
810 File path = new File(db.getDirectory(), name);
811 long old = path.lastModified();
812 long set = 1250379778668L;
813 path.setLastModified(set);
814 assertTrue("time changed", old != path.lastModified());
815 }
816 }