View Javadoc
1   /*
2    * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3    * Copyright (C) 2007-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
5    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.ObjectDatabase;
71  import org.eclipse.jgit.lib.ObjectId;
72  import org.eclipse.jgit.lib.ObjectInserter;
73  import org.eclipse.jgit.lib.PersonIdent;
74  import org.eclipse.jgit.lib.RefUpdate;
75  import org.eclipse.jgit.lib.Repository;
76  import org.eclipse.jgit.lib.TagBuilder;
77  import org.eclipse.jgit.lib.TreeFormatter;
78  import org.eclipse.jgit.revwalk.RevCommit;
79  import org.eclipse.jgit.revwalk.RevTag;
80  import org.eclipse.jgit.revwalk.RevWalk;
81  import org.eclipse.jgit.storage.file.FileBasedConfig;
82  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
83  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
84  import org.eclipse.jgit.util.FileUtils;
85  import org.junit.Rule;
86  import org.junit.Test;
87  import org.junit.rules.ExpectedException;
88  
89  public class T0003_BasicTest extends SampleDataRepositoryTestCase {
90  	@Rule
91  	public ExpectedException expectedException = ExpectedException.none();
92  
93  	@Test
94  	public void test001_Initalize() {
95  		final File gitdir = new File(trash, Constants.DOT_GIT);
96  		final File hooks = new File(gitdir, "hooks");
97  		final File objects = new File(gitdir, "objects");
98  		final File objects_pack = new File(objects, "pack");
99  		final File objects_info = new File(objects, "info");
100 		final File refs = new File(gitdir, "refs");
101 		final File refs_heads = new File(refs, "heads");
102 		final File refs_tags = new File(refs, "tags");
103 		final File HEAD = new File(gitdir, "HEAD");
104 
105 		assertTrue("Exists " + trash, trash.isDirectory());
106 		assertTrue("Exists " + hooks, hooks.isDirectory());
107 		assertTrue("Exists " + objects, objects.isDirectory());
108 		assertTrue("Exists " + objects_pack, objects_pack.isDirectory());
109 		assertTrue("Exists " + objects_info, objects_info.isDirectory());
110 		assertEquals(2L, objects.listFiles().length);
111 		assertTrue("Exists " + refs, refs.isDirectory());
112 		assertTrue("Exists " + refs_heads, refs_heads.isDirectory());
113 		assertTrue("Exists " + refs_tags, refs_tags.isDirectory());
114 		assertTrue("Exists " + HEAD, HEAD.isFile());
115 		assertEquals(23, HEAD.length());
116 	}
117 
118 	@Test
119 	public void test000_openRepoBadArgs() throws IOException {
120 		try {
121 			new FileRepositoryBuilder().build();
122 			fail("Must pass either GIT_DIR or GIT_WORK_TREE");
123 		} catch (IllegalArgumentException e) {
124 			assertEquals(JGitText.get().eitherGitDirOrWorkTreeRequired, e
125 					.getMessage());
126 		}
127 	}
128 
129 	/**
130 	 * Check the default rules for looking up directories and files within a
131 	 * repo when the gitDir is given.
132 	 *
133 	 * @throws IOException
134 	 */
135 	@Test
136 	public void test000_openrepo_default_gitDirSet() throws IOException {
137 		File repo1Parent = new File(trash.getParentFile(), "r1");
138 		Repository repo1initial = new FileRepository(new File(repo1Parent,
139 				Constants.DOT_GIT));
140 		repo1initial.create();
141 		repo1initial.close();
142 
143 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
144 		FileRepository r = (FileRepository) new FileRepositoryBuilder()
145 				.setGitDir(theDir).build();
146 		assertEqualsPath(theDir, r.getDirectory());
147 		assertEqualsPath(repo1Parent, r.getWorkTree());
148 		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
149 		assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
150 				.getDirectory());
151 	}
152 
153 	/**
154 	 * Check that we can pass both a git directory and a work tree repo when the
155 	 * gitDir is given.
156 	 *
157 	 * @throws IOException
158 	 */
159 	@Test
160 	public void test000_openrepo_default_gitDirAndWorkTreeSet()
161 			throws IOException {
162 		File repo1Parent = new File(trash.getParentFile(), "r1");
163 		Repository repo1initial = new FileRepository(new File(repo1Parent,
164 				Constants.DOT_GIT));
165 		repo1initial.create();
166 		repo1initial.close();
167 
168 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
169 		FileRepository r = (FileRepository) new FileRepositoryBuilder()
170 				.setGitDir(theDir).setWorkTree(repo1Parent.getParentFile())
171 				.build();
172 		assertEqualsPath(theDir, r.getDirectory());
173 		assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree());
174 		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
175 		assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
176 				.getDirectory());
177 	}
178 
179 	/**
180 	 * Check the default rules for looking up directories and files within a
181 	 * repo when the workTree is given.
182 	 *
183 	 * @throws IOException
184 	 */
185 	@Test
186 	public void test000_openrepo_default_workDirSet() throws IOException {
187 		File repo1Parent = new File(trash.getParentFile(), "r1");
188 		Repository repo1initial = new FileRepository(new File(repo1Parent,
189 				Constants.DOT_GIT));
190 		repo1initial.create();
191 		repo1initial.close();
192 
193 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
194 		FileRepository r = (FileRepository) new FileRepositoryBuilder()
195 				.setWorkTree(repo1Parent).build();
196 		assertEqualsPath(theDir, r.getDirectory());
197 		assertEqualsPath(repo1Parent, r.getWorkTree());
198 		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
199 		assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
200 				.getDirectory());
201 	}
202 
203 	/**
204 	 * Check that worktree config has an effect, given absolute path.
205 	 *
206 	 * @throws IOException
207 	 */
208 	@Test
209 	public void test000_openrepo_default_absolute_workdirconfig()
210 			throws IOException {
211 		File repo1Parent = new File(trash.getParentFile(), "r1");
212 		File workdir = new File(trash.getParentFile(), "rw");
213 		FileUtils.mkdir(workdir);
214 		FileRepository repo1initial = new FileRepository(new File(repo1Parent,
215 				Constants.DOT_GIT));
216 		repo1initial.create();
217 		final FileBasedConfig cfg = repo1initial.getConfig();
218 		cfg.setString("core", null, "worktree", workdir.getAbsolutePath());
219 		cfg.save();
220 		repo1initial.close();
221 
222 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
223 		FileRepository r = (FileRepository) new FileRepositoryBuilder()
224 				.setGitDir(theDir).build();
225 		assertEqualsPath(theDir, r.getDirectory());
226 		assertEqualsPath(workdir, r.getWorkTree());
227 		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
228 		assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
229 				.getDirectory());
230 	}
231 
232 	/**
233 	 * Check that worktree config has an effect, given a relative path.
234 	 *
235 	 * @throws IOException
236 	 */
237 	@Test
238 	public void test000_openrepo_default_relative_workdirconfig()
239 			throws IOException {
240 		File repo1Parent = new File(trash.getParentFile(), "r1");
241 		File workdir = new File(trash.getParentFile(), "rw");
242 		FileUtils.mkdir(workdir);
243 		FileRepository repo1initial = new FileRepository(new File(repo1Parent,
244 				Constants.DOT_GIT));
245 		repo1initial.create();
246 		final FileBasedConfig cfg = repo1initial.getConfig();
247 		cfg.setString("core", null, "worktree", "../../rw");
248 		cfg.save();
249 		repo1initial.close();
250 
251 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
252 		FileRepository r = (FileRepository) new FileRepositoryBuilder()
253 				.setGitDir(theDir).build();
254 		assertEqualsPath(theDir, r.getDirectory());
255 		assertEqualsPath(workdir, r.getWorkTree());
256 		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
257 		assertEqualsPath(new File(theDir, "objects"), r.getObjectDatabase()
258 				.getDirectory());
259 	}
260 
261 	/**
262 	 * Check that the given index file is honored and the alternate object
263 	 * directories too
264 	 *
265 	 * @throws IOException
266 	 */
267 	@Test
268 	public void test000_openrepo_alternate_index_file_and_objdirs()
269 			throws IOException {
270 		File repo1Parent = new File(trash.getParentFile(), "r1");
271 		File indexFile = new File(trash, "idx");
272 		File objDir = new File(trash, "../obj");
273 		File altObjDir = db.getObjectDatabase().getDirectory();
274 		Repository repo1initial = new FileRepository(new File(repo1Parent,
275 				Constants.DOT_GIT));
276 		repo1initial.create();
277 		repo1initial.close();
278 
279 		File theDir = new File(repo1Parent, Constants.DOT_GIT);
280 		FileRepository r = (FileRepository) new FileRepositoryBuilder() //
281 				.setGitDir(theDir).setObjectDirectory(objDir) //
282 				.addAlternateObjectDirectory(altObjDir) //
283 				.setIndexFile(indexFile) //
284 				.build();
285 		assertEqualsPath(theDir, r.getDirectory());
286 		assertEqualsPath(theDir.getParentFile(), r.getWorkTree());
287 		assertEqualsPath(indexFile, r.getIndexFile());
288 		assertEqualsPath(objDir, r.getObjectDatabase().getDirectory());
289 		assertNotNull(r.open(ObjectId
290 				.fromString("6db9c2ebf75590eef973081736730a9ea169a0c4")));
291 		// Must close or the default repo pack files created by this test gets
292 		// locked via the alternate object directories on Windows.
293 		r.close();
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 		// One of our test packs contains the empty tree object. If the pack is
304 		// open when we create it we won't write the object file out as a loose
305 		// object (as it already exists in the pack).
306 		//
307 		final Repository newdb = createBareRepository();
308 		try (final 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 		// File shouldn't exist as it is in a test pack.
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 		// We won't create a tree entry with an empty filename
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 		final FileReader fr = new FileReader(cfg);
364 		final char[] cbuf = new char[configStr.length()];
365 		fr.read(cbuf);
366 		fr.close();
367 		assertEquals(configStr, new String(cbuf));
368 	}
369 
370 	@Test
371 	public void test007_Open() throws IOException {
372 		try (final FileRepository db2 = new FileRepository(db.getDirectory())) {
373 			assertEquals(db.getDirectory(), db2.getDirectory());
374 			assertEquals(db.getObjectDatabase().getDirectory(), db2
375 					.getObjectDatabase().getDirectory());
376 			assertNotSame(db.getConfig(), db2.getConfig());
377 		}
378 	}
379 
380 	@Test
381 	public void test008_FailOnWrongVersion() throws IOException {
382 		final File cfg = new File(db.getDirectory(), Constants.CONFIG);
383 		final String badvers = "ihopethisisneveraversion";
384 		final String configStr = "[core]\n" + "\trepositoryFormatVersion="
385 				+ badvers + "\n";
386 		write(cfg, configStr);
387 
388 		try (FileRepository unused = new FileRepository(db.getDirectory())) {
389 			fail("incorrectly opened a bad repository");
390 		} catch (IllegalArgumentException ioe) {
391 			assertNotNull(ioe.getMessage());
392 		}
393 	}
394 
395 	@Test
396 	public void test009_CreateCommitOldFormat() throws IOException {
397 		final ObjectId treeId = insertTree(new TreeFormatter());
398 		final CommitBuilder c = new CommitBuilder();
399 		c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
400 		c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
401 		c.setMessage("A Commit\n");
402 		c.setTreeId(treeId);
403 		assertEquals(treeId, c.getTreeId());
404 
405 		ObjectId actid = insertCommit(c);
406 
407 		final ObjectId cmtid = ObjectId
408 				.fromString("9208b2459ea6609a5af68627cc031796d0d9329b");
409 		assertEquals(cmtid, actid);
410 
411 		// Verify the commit we just wrote is in the correct format.
412 		ObjectDatabase odb = db.getObjectDatabase();
413 		assertTrue("is ObjectDirectory", odb instanceof ObjectDirectory);
414 		final XInputStream xis = new XInputStream(new FileInputStream(
415 				((ObjectDirectory) odb).fileFor(cmtid)));
416 		try {
417 			assertEquals(0x78, xis.readUInt8());
418 			assertEquals(0x9c, xis.readUInt8());
419 			assertEquals(0, 0x789c % 31);
420 		} finally {
421 			xis.close();
422 		}
423 
424 		// Verify we can read it.
425 		RevCommit c2 = parseCommit(actid);
426 		assertNotNull(c2);
427 		assertEquals(c.getMessage(), c2.getFullMessage());
428 		assertEquals(c.getTreeId(), c2.getTree());
429 		assertEquals(c.getAuthor(), c2.getAuthorIdent());
430 		assertEquals(c.getCommitter(), c2.getCommitterIdent());
431 	}
432 
433 	@Test
434 	public void test020_createBlobTag() throws IOException {
435 		final ObjectId emptyId = insertEmptyBlob();
436 		final TagBuilder t = new TagBuilder();
437 		t.setObjectId(emptyId, Constants.OBJ_BLOB);
438 		t.setTag("test020");
439 		t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
440 		t.setMessage("test020 tagged\n");
441 		ObjectId actid = insertTag(t);
442 		assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", actid.name());
443 
444 		RevTag mapTag = parseTag(actid);
445 		assertEquals(Constants.OBJ_BLOB, mapTag.getObject().getType());
446 		assertEquals("test020 tagged\n", mapTag.getFullMessage());
447 		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
448 				.getTaggerIdent());
449 		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag
450 				.getObject().getId().name());
451 	}
452 
453 	@Test
454 	public void test021_createTreeTag() throws IOException {
455 		final ObjectId emptyId = insertEmptyBlob();
456 		TreeFormatter almostEmptyTree = new TreeFormatter();
457 		almostEmptyTree.append("empty", FileMode.REGULAR_FILE, emptyId);
458 		final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
459 		final TagBuilder t = new TagBuilder();
460 		t.setObjectId(almostEmptyTreeId, Constants.OBJ_TREE);
461 		t.setTag("test021");
462 		t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
463 		t.setMessage("test021 tagged\n");
464 		ObjectId actid = insertTag(t);
465 		assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", actid.name());
466 
467 		RevTag mapTag = parseTag(actid);
468 		assertEquals(Constants.OBJ_TREE, mapTag.getObject().getType());
469 		assertEquals("test021 tagged\n", mapTag.getFullMessage());
470 		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
471 				.getTaggerIdent());
472 		assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag
473 				.getObject().getId().name());
474 	}
475 
476 	@Test
477 	public void test022_createCommitTag() throws IOException {
478 		final ObjectId emptyId = insertEmptyBlob();
479 		TreeFormatter almostEmptyTree = new TreeFormatter();
480 		almostEmptyTree.append("empty", FileMode.REGULAR_FILE, emptyId);
481 		final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
482 		final CommitBuilder almostEmptyCommit = new CommitBuilder();
483 		almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L,
484 				-2 * 60)); // not exactly the same
485 		almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L,
486 				-2 * 60));
487 		almostEmptyCommit.setMessage("test022\n");
488 		almostEmptyCommit.setTreeId(almostEmptyTreeId);
489 		ObjectId almostEmptyCommitId = insertCommit(almostEmptyCommit);
490 		final TagBuilder t = new TagBuilder();
491 		t.setObjectId(almostEmptyCommitId, Constants.OBJ_COMMIT);
492 		t.setTag("test022");
493 		t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
494 		t.setMessage("test022 tagged\n");
495 		ObjectId actid = insertTag(t);
496 		assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", actid.name());
497 
498 		RevTag mapTag = parseTag(actid);
499 		assertEquals(Constants.OBJ_COMMIT, mapTag.getObject().getType());
500 		assertEquals("test022 tagged\n", mapTag.getFullMessage());
501 		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
502 				.getTaggerIdent());
503 		assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag
504 				.getObject().getId().name());
505 	}
506 
507 	@Test
508 	public void test023_createCommitNonAnullii() throws IOException {
509 		final ObjectId emptyId = insertEmptyBlob();
510 		TreeFormatter almostEmptyTree = new TreeFormatter();
511 		almostEmptyTree.append("empty", FileMode.REGULAR_FILE, emptyId);
512 		final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
513 		CommitBuilder commit = new CommitBuilder();
514 		commit.setTreeId(almostEmptyTreeId);
515 		commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
516 				4294967295000L, 60));
517 		commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
518 				4294967295000L, 60));
519 		commit.setEncoding("UTF-8");
520 		commit.setMessage("\u00dcbergeeks");
521 		ObjectId cid = insertCommit(commit);
522 		assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
523 
524 		RevCommit loadedCommit = parseCommit(cid);
525 		assertEquals(commit.getMessage(), loadedCommit.getFullMessage());
526 	}
527 
528 	@Test
529 	public void test024_createCommitNonAscii() throws IOException {
530 		final ObjectId emptyId = insertEmptyBlob();
531 		TreeFormatter almostEmptyTree = new TreeFormatter();
532 		almostEmptyTree.append("empty", FileMode.REGULAR_FILE, emptyId);
533 		final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
534 		CommitBuilder commit = new CommitBuilder();
535 		commit.setTreeId(almostEmptyTreeId);
536 		commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
537 				4294967295000L, 60));
538 		commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
539 				4294967295000L, 60));
540 		commit.setEncoding("ISO-8859-1");
541 		commit.setMessage("\u00dcbergeeks");
542 		ObjectId cid = insertCommit(commit);
543 		assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
544 	}
545 
546 	@Test
547 	public void test025_computeSha1NoStore() throws IOException {
548 		byte[] data = "test025 some data, more than 16 bytes to get good coverage"
549 				.getBytes("ISO-8859-1");
550 		try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
551 			final ObjectId id = formatter.idFor(Constants.OBJ_BLOB, data);
552 			assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
553 		}
554 	}
555 
556 	@Test
557 	public void test026_CreateCommitMultipleparents() throws IOException {
558 		final ObjectId treeId;
559 		try (final ObjectInserter oi = db.newObjectInserter()) {
560 			final ObjectId blobId = oi.insert(Constants.OBJ_BLOB,
561 					"and this is the data in me\n".getBytes(Constants.CHARSET
562 							.name()));
563 			TreeFormatter fmt = new TreeFormatter();
564 			fmt.append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
565 			treeId = oi.insert(fmt);
566 			oi.flush();
567 		}
568 		assertEquals(ObjectId
569 				.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"), treeId);
570 
571 		final CommitBuilder c1 = new CommitBuilder();
572 		c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
573 		c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
574 		c1.setMessage("A Commit\n");
575 		c1.setTreeId(treeId);
576 		assertEquals(treeId, c1.getTreeId());
577 		ObjectId actid1 = insertCommit(c1);
578 		final ObjectId cmtid1 = ObjectId
579 				.fromString("803aec4aba175e8ab1d666873c984c0308179099");
580 		assertEquals(cmtid1, actid1);
581 
582 		final CommitBuilder c2 = new CommitBuilder();
583 		c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
584 		c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
585 		c2.setMessage("A Commit 2\n");
586 		c2.setTreeId(treeId);
587 		assertEquals(treeId, c2.getTreeId());
588 		c2.setParentIds(actid1);
589 		ObjectId actid2 = insertCommit(c2);
590 		final ObjectId cmtid2 = ObjectId
591 				.fromString("95d068687c91c5c044fb8c77c5154d5247901553");
592 		assertEquals(cmtid2, actid2);
593 
594 		RevCommit rm2 = parseCommit(cmtid2);
595 		assertNotSame(c2, rm2); // assert the parsed objects is not from the
596 		// cache
597 		assertEquals(c2.getAuthor(), rm2.getAuthorIdent());
598 		assertEquals(actid2, rm2.getId());
599 		assertEquals(c2.getMessage(), rm2.getFullMessage());
600 		assertEquals(c2.getTreeId(), rm2.getTree().getId());
601 		assertEquals(1, rm2.getParentCount());
602 		assertEquals(actid1, rm2.getParent(0));
603 
604 		final CommitBuilder c3 = new CommitBuilder();
605 		c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
606 		c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
607 		c3.setMessage("A Commit 3\n");
608 		c3.setTreeId(treeId);
609 		assertEquals(treeId, c3.getTreeId());
610 		c3.setParentIds(actid1, actid2);
611 		ObjectId actid3 = insertCommit(c3);
612 		final ObjectId cmtid3 = ObjectId
613 				.fromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
614 		assertEquals(cmtid3, actid3);
615 
616 		RevCommit rm3 = parseCommit(cmtid3);
617 		assertNotSame(c3, rm3); // assert the parsed objects is not from the
618 		// cache
619 		assertEquals(c3.getAuthor(), rm3.getAuthorIdent());
620 		assertEquals(actid3, rm3.getId());
621 		assertEquals(c3.getMessage(), rm3.getFullMessage());
622 		assertEquals(c3.getTreeId(), rm3.getTree().getId());
623 		assertEquals(2, rm3.getParentCount());
624 		assertEquals(actid1, rm3.getParent(0));
625 		assertEquals(actid2, rm3.getParent(1));
626 
627 		final CommitBuilder c4 = new CommitBuilder();
628 		c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
629 		c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
630 		c4.setMessage("A Commit 4\n");
631 		c4.setTreeId(treeId);
632 		assertEquals(treeId, c3.getTreeId());
633 		c4.setParentIds(actid1, actid2, actid3);
634 		ObjectId actid4 = insertCommit(c4);
635 		final ObjectId cmtid4 = ObjectId
636 				.fromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
637 		assertEquals(cmtid4, actid4);
638 
639 		RevCommit rm4 = parseCommit(cmtid4);
640 		assertNotSame(c4, rm3); // assert the parsed objects is not from the
641 		// cache
642 		assertEquals(c4.getAuthor(), rm4.getAuthorIdent());
643 		assertEquals(actid4, rm4.getId());
644 		assertEquals(c4.getMessage(), rm4.getFullMessage());
645 		assertEquals(c4.getTreeId(), rm4.getTree().getId());
646 		assertEquals(3, rm4.getParentCount());
647 		assertEquals(actid1, rm4.getParent(0));
648 		assertEquals(actid2, rm4.getParent(1));
649 		assertEquals(actid3, rm4.getParent(2));
650 	}
651 
652 	@Test
653 	public void test027_UnpackedRefHigherPriorityThanPacked()
654 			throws IOException {
655 		String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
656 		write(new File(db.getDirectory(), "refs/heads/a"), unpackedId + "\n");
657 
658 		ObjectId resolved = db.resolve("refs/heads/a");
659 		assertEquals(unpackedId, resolved.name());
660 	}
661 
662 	@Test
663 	public void test028_LockPackedRef() throws IOException {
664 		writeTrashFile(".git/packed-refs",
665 				"7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
666 		writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
667 		BUG_WorkAroundRacyGitIssues("packed-refs");
668 		BUG_WorkAroundRacyGitIssues("HEAD");
669 
670 		ObjectId resolve = db.resolve("HEAD");
671 		assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
672 
673 		RefUpdate lockRef = db.updateRef("HEAD");
674 		ObjectId newId = ObjectId
675 				.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
676 		lockRef.setNewObjectId(newId);
677 		assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
678 
679 		assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
680 		assertEquals(newId, db.resolve("refs/heads/foobar"));
681 
682 		// Again. The ref already exists
683 		RefUpdate lockRef2 = db.updateRef("HEAD");
684 		ObjectId newId2 = ObjectId
685 				.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
686 		lockRef2.setNewObjectId(newId2);
687 		assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
688 
689 		assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
690 		assertEquals(newId2, db.resolve("refs/heads/foobar"));
691 	}
692 
693 	@Test
694 	public void test30_stripWorkDir() {
695 		File relCwd = new File(".");
696 		File absCwd = relCwd.getAbsoluteFile();
697 		File absBase = new File(new File(absCwd, "repo"), "workdir");
698 		File relBase = new File(new File(relCwd, "repo"), "workdir");
699 		assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
700 
701 		File relBaseFile = new File(new File(relBase, "other"), "module.c");
702 		File absBaseFile = new File(new File(absBase, "other"), "module.c");
703 		assertEquals("other/module.c", Repository.stripWorkDir(relBase,
704 				relBaseFile));
705 		assertEquals("other/module.c", Repository.stripWorkDir(relBase,
706 				absBaseFile));
707 		assertEquals("other/module.c", Repository.stripWorkDir(absBase,
708 				relBaseFile));
709 		assertEquals("other/module.c", Repository.stripWorkDir(absBase,
710 				absBaseFile));
711 
712 		File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
713 		File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
714 		assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
715 		assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
716 
717 		assertEquals("", Repository.stripWorkDir(db.getWorkTree(), db
718 				.getWorkTree()));
719 
720 		File file = new File(new File(db.getWorkTree(), "subdir"), "File.java");
721 		assertEquals("subdir/File.java", Repository.stripWorkDir(db
722 				.getWorkTree(), file));
723 
724 	}
725 
726 	private ObjectId insertEmptyBlob() throws IOException {
727 		final ObjectId emptyId;
728 		try (ObjectInserter oi = db.newObjectInserter()) {
729 			emptyId = oi.insert(Constants.OBJ_BLOB, new byte[] {});
730 			oi.flush();
731 		}
732 		return emptyId;
733 	}
734 
735 	private ObjectId insertTree(TreeFormatter tree) throws IOException {
736 		try (ObjectInserter oi = db.newObjectInserter()) {
737 			ObjectId id = oi.insert(tree);
738 			oi.flush();
739 			return id;
740 		}
741 	}
742 
743 	private ObjectId insertCommit(final CommitBuilder builder)
744 			throws IOException, UnsupportedEncodingException {
745 		try (ObjectInserter oi = db.newObjectInserter()) {
746 			ObjectId id = oi.insert(builder);
747 			oi.flush();
748 			return id;
749 		}
750 	}
751 
752 	private RevCommit parseCommit(AnyObjectId id)
753 			throws MissingObjectException, IncorrectObjectTypeException,
754 			IOException {
755 		try (RevWalk rw = new RevWalk(db)) {
756 			return rw.parseCommit(id);
757 		}
758 	}
759 
760 	private ObjectId insertTag(final TagBuilder tag) throws IOException,
761 			UnsupportedEncodingException {
762 		try (ObjectInserter oi = db.newObjectInserter()) {
763 			ObjectId id = oi.insert(tag);
764 			oi.flush();
765 			return id;
766 		}
767 	}
768 
769 	private RevTag parseTag(AnyObjectId id) throws MissingObjectException,
770 			IncorrectObjectTypeException, IOException {
771 		try (RevWalk rw = new RevWalk(db)) {
772 			return rw.parseTag(id);
773 		}
774 	}
775 
776 	/**
777 	 * Kick the timestamp of a local file.
778 	 * <p>
779 	 * We shouldn't have to make these method calls. The cache is using file
780 	 * system timestamps, and on many systems unit tests run faster than the
781 	 * modification clock. Dumping the cache after we make an edit behind
782 	 * RefDirectory's back allows the tests to pass.
783 	 *
784 	 * @param name
785 	 *            the file in the repository to force a time change on.
786 	 */
787 	private void BUG_WorkAroundRacyGitIssues(String name) {
788 		File path = new File(db.getDirectory(), name);
789 		long old = path.lastModified();
790 		long set = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
791 		path.setLastModified(set);
792 		assertTrue("time changed", old != path.lastModified());
793 	}
794 }