2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org
.spearce
.jgit
.lib
;
42 import java
.io
.ByteArrayInputStream
;
44 import java
.io
.FileInputStream
;
45 import java
.io
.FileReader
;
46 import java
.io
.FileWriter
;
47 import java
.io
.IOException
;
48 import java
.io
.PrintWriter
;
50 public class T0003_Basic
extends RepositoryTestCase
{
51 public void test001_Initalize() {
52 final File gitdir
= new File(trash
, ".git");
53 final File objects
= new File(gitdir
, "objects");
54 final File objects_pack
= new File(objects
, "pack");
55 final File objects_info
= new File(objects
, "info");
56 final File refs
= new File(gitdir
, "refs");
57 final File refs_heads
= new File(refs
, "heads");
58 final File refs_tags
= new File(refs
, "tags");
59 final File HEAD
= new File(gitdir
, "HEAD");
61 assertTrue("Exists " + trash
, trash
.isDirectory());
62 assertTrue("Exists " + objects
, objects
.isDirectory());
63 assertTrue("Exists " + objects_pack
, objects_pack
.isDirectory());
64 assertTrue("Exists " + objects_info
, objects_info
.isDirectory());
65 assertEquals(2, objects
.listFiles().length
);
66 assertTrue("Exists " + refs
, refs
.isDirectory());
67 assertTrue("Exists " + refs_heads
, refs_heads
.isDirectory());
68 assertTrue("Exists " + refs_tags
, refs_tags
.isDirectory());
69 assertTrue("Exists " + HEAD
, HEAD
.isFile());
70 assertEquals(23, HEAD
.length());
73 public void test002_WriteEmptyTree() throws IOException
{
74 // One of our test packs contains the empty tree object. If the pack is
75 // open when we create it we won't write the object file out as a loose
76 // object (as it already exists in the pack).
80 final Tree t
= new Tree(db
);
81 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
82 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t
.getId()
84 final File o
= new File(new File(new File(trash_git
, "objects"), "4b"),
85 "825dc642cb6eb9a060e54bf8d69288fbee4904");
86 assertTrue("Exists " + o
, o
.isFile());
87 assertTrue("Read-only " + o
, !o
.canWrite());
90 public void test002_WriteEmptyTree2() throws IOException
{
91 // File shouldn't exist as it is in a test pack.
93 final Tree t
= new Tree(db
);
94 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
95 assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t
.getId()
97 final File o
= new File(new File(new File(trash_git
, "objects"), "4b"),
98 "825dc642cb6eb9a060e54bf8d69288fbee4904");
99 assertFalse("Exists " + o
, o
.isFile());
102 public void test003_WriteShouldBeEmptyTree() throws IOException
{
103 final Tree t
= new Tree(db
);
104 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
105 t
.addFile("should-be-empty").setId(emptyId
);
106 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
107 assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t
.getId()
111 o
= new File(new File(new File(trash_git
, "objects"), "7b"),
112 "b943559a305bdd6bdee2cef6e5df2413c3d30a");
113 assertTrue("Exists " + o
, o
.isFile());
114 assertTrue("Read-only " + o
, !o
.canWrite());
116 o
= new File(new File(new File(trash_git
, "objects"), "e6"),
117 "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
118 assertTrue("Exists " + o
, o
.isFile());
119 assertTrue("Read-only " + o
, !o
.canWrite());
122 public void test004_CheckNewConfig() throws IOException
{
123 final RepositoryConfig c
= db
.getConfig();
125 assertEquals("0", c
.getString("core", null, "repositoryformatversion"));
126 assertEquals("0", c
.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN"));
127 assertEquals("true", c
.getString("core", null, "filemode"));
128 assertEquals("true", c
.getString("cOrE", null, "fIlEModE"));
129 assertNull(c
.getString("notavalue", null, "reallyNotAValue"));
133 public void test005_ReadSimpleConfig() throws IOException
{
134 final RepositoryConfig c
= db
.getConfig();
137 assertEquals("0", c
.getString("core", null, "repositoryformatversion"));
138 assertEquals("0", c
.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN"));
139 assertEquals("true", c
.getString("core", null, "filemode"));
140 assertEquals("true", c
.getString("cOrE", null, "fIlEModE"));
141 assertNull(c
.getString("notavalue", null, "reallyNotAValue"));
144 public void test006_ReadUglyConfig() throws IOException
{
145 final RepositoryConfig c
= db
.getConfig();
146 final File cfg
= new File(db
.getDirectory(), "config");
147 final FileWriter pw
= new FileWriter(cfg
);
148 final String configStr
= " [core];comment\n\tfilemode = yes\n"
150 + " email = A U Thor <thor@example.com> # Just an example...\n"
151 + " name = \"A Thor \\\\ \\\"\\t \"\n"
152 + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
157 assertEquals("yes", c
.getString("core", null, "filemode"));
158 assertEquals("A U Thor <thor@example.com>", c
159 .getString("user", null, "email"));
160 assertEquals("A Thor \\ \"\t ", c
.getString("user", null, "name"));
161 assertEquals("a many line\ncomment\n to test", c
.getString("user",
162 null, "defaultCheckInComment"));
164 final FileReader fr
= new FileReader(cfg
);
165 final char[] cbuf
= new char[configStr
.length()];
168 assertEquals(configStr
, new String(cbuf
));
171 public void test007_Open() throws IOException
{
172 final Repository db2
= new Repository(db
.getDirectory());
173 assertEquals(db
.getDirectory(), db2
.getDirectory());
174 assertEquals(db
.getObjectsDirectory(), db2
.getObjectsDirectory());
175 assertNotSame(db
.getConfig(), db2
.getConfig());
178 public void test008_FailOnWrongVersion() throws IOException
{
179 final File cfg
= new File(db
.getDirectory(), "config");
180 final FileWriter pw
= new FileWriter(cfg
);
181 final String badvers
= "ihopethisisneveraversion";
182 final String configStr
= "[core]\n" + "\trepositoryFormatVersion="
188 new Repository(db
.getDirectory());
189 fail("incorrectly opened a bad repository");
190 } catch (IOException ioe
) {
191 assertTrue(ioe
.getMessage().indexOf("format") > 0);
192 assertTrue(ioe
.getMessage().indexOf(badvers
) > 0);
196 public void test009_CreateCommitOldFormat() throws IOException
{
197 writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
198 db
.getConfig().load();
200 final Tree t
= new Tree(db
);
201 final FileTreeEntry f
= t
.addFile("i-am-a-file");
202 writeTrashFile(f
.getName(), "and this is the data in me\n");
203 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
204 assertEquals(ObjectId
.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
207 final Commit c
= new Commit(db
);
208 c
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
209 c
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
210 c
.setMessage("A Commit\n");
212 assertEquals(t
.getTreeId(), c
.getTreeId());
214 final ObjectId cmtid
= ObjectId
.fromString(
215 "803aec4aba175e8ab1d666873c984c0308179099");
216 assertEquals(cmtid
, c
.getCommitId());
218 // Verify the commit we just wrote is in the correct format.
219 final XInputStream xis
= new XInputStream(new FileInputStream(db
222 assertEquals(0x78, xis
.readUInt8());
223 assertEquals(0x9c, xis
.readUInt8());
224 assertTrue(0x789c % 31 == 0);
229 // Verify we can read it.
230 final Commit c2
= db
.mapCommit(cmtid
);
232 assertEquals(c
.getMessage(), c2
.getMessage());
233 assertEquals(c
.getTreeId(), c2
.getTreeId());
234 assertEquals(c
.getAuthor(), c2
.getAuthor());
235 assertEquals(c
.getCommitter(), c2
.getCommitter());
238 public void test012_SubtreeExternalSorting() throws IOException
{
239 final ObjectId emptyBlob
= new ObjectWriter(db
).writeBlob(new byte[0]);
240 final Tree t
= new Tree(db
);
241 final FileTreeEntry e0
= t
.addFile("a-");
242 final FileTreeEntry e1
= t
.addFile("a-b");
243 final FileTreeEntry e2
= t
.addFile("a/b");
244 final FileTreeEntry e3
= t
.addFile("a=");
245 final FileTreeEntry e4
= t
.addFile("a=b");
253 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
254 assertEquals(ObjectId
.fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
258 public void test020_createBlobTag() throws IOException
{
259 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
260 final Tag t
= new Tag(db
);
264 t
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
265 t
.setMessage("test020 tagged\n");
267 assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t
.getTagId().name());
269 Tag mapTag
= db
.mapTag("test020");
270 assertEquals("blob", mapTag
.getType());
271 assertEquals("test020 tagged\n", mapTag
.getMessage());
272 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag
.getAuthor());
273 assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag
.getObjId().name());
276 public void test020b_createBlobPlainTag() throws IOException
{
277 test020_createBlobTag();
279 t
.setTag("test020b");
280 t
.setObjId(ObjectId
.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
283 Tag mapTag
= db
.mapTag("test020b");
284 assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag
.getObjId().name());
286 // We do not repeat the plain tag test for other object types
289 public void test021_createTreeTag() throws IOException
{
290 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
291 final Tree almostEmptyTree
= new Tree(db
);
292 almostEmptyTree
.addEntry(new FileTreeEntry(almostEmptyTree
, emptyId
, "empty".getBytes(), false));
293 final ObjectId almostEmptyTreeId
= new ObjectWriter(db
).writeTree(almostEmptyTree
);
294 final Tag t
= new Tag(db
);
295 t
.setObjId(almostEmptyTreeId
);
298 t
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
299 t
.setMessage("test021 tagged\n");
301 assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t
.getTagId().name());
303 Tag mapTag
= db
.mapTag("test021");
304 assertEquals("tree", mapTag
.getType());
305 assertEquals("test021 tagged\n", mapTag
.getMessage());
306 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag
.getAuthor());
307 assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag
.getObjId().name());
310 public void test022_createCommitTag() throws IOException
{
311 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
312 final Tree almostEmptyTree
= new Tree(db
);
313 almostEmptyTree
.addEntry(new FileTreeEntry(almostEmptyTree
, emptyId
, "empty".getBytes(), false));
314 final ObjectId almostEmptyTreeId
= new ObjectWriter(db
).writeTree(almostEmptyTree
);
315 final Commit almostEmptyCommit
= new Commit(db
);
316 almostEmptyCommit
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -2 * 60)); // not exactly the same
317 almostEmptyCommit
.setCommitter(new PersonIdent(jauthor
, 1154236443000L, -2 * 60));
318 almostEmptyCommit
.setMessage("test022\n");
319 almostEmptyCommit
.setTreeId(almostEmptyTreeId
);
320 ObjectId almostEmptyCommitId
= new ObjectWriter(db
).writeCommit(almostEmptyCommit
);
321 final Tag t
= new Tag(db
);
322 t
.setObjId(almostEmptyCommitId
);
325 t
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
326 t
.setMessage("test022 tagged\n");
328 assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t
.getTagId().name());
330 Tag mapTag
= db
.mapTag("test022");
331 assertEquals("commit", mapTag
.getType());
332 assertEquals("test022 tagged\n", mapTag
.getMessage());
333 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag
.getAuthor());
334 assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag
.getObjId().name());
337 public void test023_createCommitNonAnullii() throws IOException
{
338 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
339 final Tree almostEmptyTree
= new Tree(db
);
340 almostEmptyTree
.addEntry(new FileTreeEntry(almostEmptyTree
, emptyId
, "empty".getBytes(), false));
341 final ObjectId almostEmptyTreeId
= new ObjectWriter(db
).writeTree(almostEmptyTree
);
342 Commit commit
= new Commit(db
);
343 commit
.setTreeId(almostEmptyTreeId
);
344 commit
.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
345 commit
.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
346 commit
.setEncoding("UTF-8");
347 commit
.setMessage("\u00dcbergeeks");
348 ObjectId cid
= new ObjectWriter(db
).writeCommit(commit
);
349 assertEquals("4680908112778718f37e686cbebcc912730b3154", cid
.name());
352 public void test024_createCommitNonAscii() throws IOException
{
353 final ObjectId emptyId
= new ObjectWriter(db
).writeBlob(new byte[0]);
354 final Tree almostEmptyTree
= new Tree(db
);
355 almostEmptyTree
.addEntry(new FileTreeEntry(almostEmptyTree
, emptyId
, "empty".getBytes(), false));
356 final ObjectId almostEmptyTreeId
= new ObjectWriter(db
).writeTree(almostEmptyTree
);
357 Commit commit
= new Commit(db
);
358 commit
.setTreeId(almostEmptyTreeId
);
359 commit
.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
360 commit
.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
361 commit
.setEncoding("ISO-8859-1");
362 commit
.setMessage("\u00dcbergeeks");
363 ObjectId cid
= new ObjectWriter(db
).writeCommit(commit
);
364 assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid
.name());
367 public void test025_packedRefs() throws IOException
{
368 test020_createBlobTag();
369 test021_createTreeTag();
370 test022_createCommitTag();
372 if (!new File(db
.getDirectory(),"refs/tags/test020").delete()) throw new Error("Cannot delete unpacked tag");
373 if (!new File(db
.getDirectory(),"refs/tags/test021").delete()) throw new Error("Cannot delete unpacked tag");
374 if (!new File(db
.getDirectory(),"refs/tags/test022").delete()) throw new Error("Cannot delete unpacked tag");
376 // We cannot resolve it now, since we have no ref
377 Tag mapTag20missing
= db
.mapTag("test020");
378 assertNull(mapTag20missing
);
380 // Construct packed refs file
381 PrintWriter w
= new PrintWriter(new FileWriter(new File(db
.getDirectory(), "packed-refs")));
382 w
.println("# packed-refs with: peeled");
383 w
.println("6759556b09fbb4fd8ae5e315134481cc25d46954 refs/tags/test020");
384 w
.println("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
385 w
.println("b0517bc8dbe2096b419d42424cd7030733f4abe5 refs/tags/test021");
386 w
.println("^417c01c8795a35b8e835113a85a5c0c1c77f67fb");
387 w
.println("0ce2ebdb36076ef0b38adbe077a07d43b43e3807 refs/tags/test022");
388 w
.println("^b5d3b45a96b340441f5abb9080411705c51cc86c");
391 Tag mapTag20
= db
.mapTag("test020");
392 assertNotNull("have tag test020", mapTag20
);
393 assertEquals("blob", mapTag20
.getType());
394 assertEquals("test020 tagged\n", mapTag20
.getMessage());
395 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag20
.getAuthor());
396 assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag20
.getObjId().name());
398 Tag mapTag21
= db
.mapTag("test021");
399 assertEquals("tree", mapTag21
.getType());
400 assertEquals("test021 tagged\n", mapTag21
.getMessage());
401 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag21
.getAuthor());
402 assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag21
.getObjId().name());
404 Tag mapTag22
= db
.mapTag("test022");
405 assertEquals("commit", mapTag22
.getType());
406 assertEquals("test022 tagged\n", mapTag22
.getMessage());
407 assertEquals(new PersonIdent(jauthor
, 1154236443000L, -4 * 60), mapTag22
.getAuthor());
408 assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag22
.getObjId().name());
411 public void test025_computeSha1NoStore() throws IOException
{
412 byte[] data
= "test025 some data, more than 16 bytes to get good coverage"
413 .getBytes("ISO-8859-1");
414 // TODO: but we do not test legacy header writing
415 final ObjectId id
= new ObjectWriter(db
).computeBlobSha1(data
.length
,
416 new ByteArrayInputStream(data
));
417 assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id
.name());
420 public void test026_CreateCommitMultipleparents() throws IOException
{
421 db
.getConfig().load();
423 final Tree t
= new Tree(db
);
424 final FileTreeEntry f
= t
.addFile("i-am-a-file");
425 writeTrashFile(f
.getName(), "and this is the data in me\n");
426 t
.accept(new WriteTree(trash
, db
), TreeEntry
.MODIFIED_ONLY
);
427 assertEquals(ObjectId
.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
430 final Commit c1
= new Commit(db
);
431 c1
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
432 c1
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
433 c1
.setMessage("A Commit\n");
435 assertEquals(t
.getTreeId(), c1
.getTreeId());
437 final ObjectId cmtid1
= ObjectId
.fromString(
438 "803aec4aba175e8ab1d666873c984c0308179099");
439 assertEquals(cmtid1
, c1
.getCommitId());
441 final Commit c2
= new Commit(db
);
442 c2
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
443 c2
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
444 c2
.setMessage("A Commit 2\n");
446 assertEquals(t
.getTreeId(), c2
.getTreeId());
447 c2
.setParentIds(new ObjectId
[] { c1
.getCommitId() } );
449 final ObjectId cmtid2
= ObjectId
.fromString(
450 "95d068687c91c5c044fb8c77c5154d5247901553");
451 assertEquals(cmtid2
, c2
.getCommitId());
453 Commit rm2
= db
.mapCommit(cmtid2
);
454 assertNotSame(c2
, rm2
); // assert the parsed objects is not from the cache
455 assertEquals(c2
.getAuthor(), rm2
.getAuthor());
456 assertEquals(c2
.getCommitId(), rm2
.getCommitId());
457 assertEquals(c2
.getMessage(), rm2
.getMessage());
458 assertEquals(c2
.getTree().getTreeId(), rm2
.getTree().getTreeId());
459 assertEquals(1, rm2
.getParentIds().length
);
460 assertEquals(c1
.getCommitId(), rm2
.getParentIds()[0]);
462 final Commit c3
= new Commit(db
);
463 c3
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
464 c3
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
465 c3
.setMessage("A Commit 3\n");
467 assertEquals(t
.getTreeId(), c3
.getTreeId());
468 c3
.setParentIds(new ObjectId
[] { c1
.getCommitId(), c2
.getCommitId() });
470 final ObjectId cmtid3
= ObjectId
.fromString(
471 "ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
472 assertEquals(cmtid3
, c3
.getCommitId());
474 Commit rm3
= db
.mapCommit(cmtid3
);
475 assertNotSame(c3
, rm3
); // assert the parsed objects is not from the cache
476 assertEquals(c3
.getAuthor(), rm3
.getAuthor());
477 assertEquals(c3
.getCommitId(), rm3
.getCommitId());
478 assertEquals(c3
.getMessage(), rm3
.getMessage());
479 assertEquals(c3
.getTree().getTreeId(), rm3
.getTree().getTreeId());
480 assertEquals(2, rm3
.getParentIds().length
);
481 assertEquals(c1
.getCommitId(), rm3
.getParentIds()[0]);
482 assertEquals(c2
.getCommitId(), rm3
.getParentIds()[1]);
484 final Commit c4
= new Commit(db
);
485 c4
.setAuthor(new PersonIdent(jauthor
, 1154236443000L, -4 * 60));
486 c4
.setCommitter(new PersonIdent(jcommitter
, 1154236443000L, -4 * 60));
487 c4
.setMessage("A Commit 4\n");
489 assertEquals(t
.getTreeId(), c3
.getTreeId());
490 c4
.setParentIds(new ObjectId
[] { c1
.getCommitId(), c2
.getCommitId(), c3
.getCommitId() });
492 final ObjectId cmtid4
= ObjectId
.fromString(
493 "d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
494 assertEquals(cmtid4
, c4
.getCommitId());
496 Commit rm4
= db
.mapCommit(cmtid4
);
497 assertNotSame(c4
, rm3
); // assert the parsed objects is not from the cache
498 assertEquals(c4
.getAuthor(), rm4
.getAuthor());
499 assertEquals(c4
.getCommitId(), rm4
.getCommitId());
500 assertEquals(c4
.getMessage(), rm4
.getMessage());
501 assertEquals(c4
.getTree().getTreeId(), rm4
.getTree().getTreeId());
502 assertEquals(3, rm4
.getParentIds().length
);
503 assertEquals(c1
.getCommitId(), rm4
.getParentIds()[0]);
504 assertEquals(c2
.getCommitId(), rm4
.getParentIds()[1]);
505 assertEquals(c3
.getCommitId(), rm4
.getParentIds()[2]);
508 public void test027_UnpackedRefHigherPriorityThanPacked() throws IOException
{
509 PrintWriter writer
= new PrintWriter(new FileWriter(new File(db
.getDirectory(), "refs/heads/a")));
510 String unpackedId
= "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
511 writer
.println(unpackedId
);
514 ObjectId resolved
= db
.resolve("refs/heads/a");
515 assertEquals(unpackedId
, resolved
.name());
518 public void test028_LockPackedRef() throws IOException
{
519 writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
520 writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
522 ObjectId resolve
= db
.resolve("HEAD");
523 assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve
.name());
525 RefUpdate lockRef
= db
.updateRef("HEAD");
526 ObjectId newId
= ObjectId
.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
527 lockRef
.setNewObjectId(newId
);
528 assertEquals(RefUpdate
.Result
.FORCED
, lockRef
.forceUpdate());
530 assertTrue(new File(db
.getDirectory(), "refs/heads/foobar").exists());
531 assertEquals(newId
, db
.resolve("refs/heads/foobar"));
533 // Again. The ref already exists
534 RefUpdate lockRef2
= db
.updateRef("HEAD");
535 ObjectId newId2
= ObjectId
.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
536 lockRef2
.setNewObjectId(newId2
);
537 assertEquals(RefUpdate
.Result
.FORCED
, lockRef2
.forceUpdate());
539 assertTrue(new File(db
.getDirectory(), "refs/heads/foobar").exists());
540 assertEquals(newId2
, db
.resolve("refs/heads/foobar"));
543 public void test029_mapObject() throws IOException
{
544 assertEquals(new byte[0].getClass(), db
.mapObject(ObjectId
.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), null).getClass());
545 assertEquals(Commit
.class, db
.mapObject(ObjectId
.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"), null).getClass());
546 assertEquals(Tree
.class, db
.mapObject(ObjectId
.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"), null).getClass());
547 assertEquals(Tag
.class, db
.mapObject(ObjectId
.fromString("17768080a2318cd89bba4c8b87834401e2095703"), null).getClass());