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