aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0003_Basic.java
blob: aab57aa594014ccaad60c788dc923d49cd4b20a9 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
 * Copyright (C) 2007-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
 * and other copyright owners as documented in the project's IP log.
 *
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Distribution License v1.0 which
 * accompanies this distribution, is reproduced below, and is
 * available at http://www.eclipse.org/org/documents/edl-v10.php
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials provided
 *   with the distribution.
 *
 * - Neither the name of the Eclipse Foundation, Inc. nor the
 *   names of its contributors may be used to endorse or promote
 *   products derived from this software without specific prior
 *   written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.eclipse.jgit.lib;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.jgit.errors.ConfigInvalidException;

public class T0003_Basic extends SampleDataRepositoryTestCase {
	public void test001_Initalize() {
		final File gitdir = new File(trash, Constants.DOT_GIT);
		final File objects = new File(gitdir, "objects");
		final File objects_pack = new File(objects, "pack");
		final File objects_info = new File(objects, "info");
		final File refs = new File(gitdir, "refs");
		final File refs_heads = new File(refs, "heads");
		final File refs_tags = new File(refs, "tags");
		final File HEAD = new File(gitdir, "HEAD");

		assertTrue("Exists " + trash, trash.isDirectory());
		assertTrue("Exists " + objects, objects.isDirectory());
		assertTrue("Exists " + objects_pack, objects_pack.isDirectory());
		assertTrue("Exists " + objects_info, objects_info.isDirectory());
		assertEquals(2, objects.listFiles().length);
		assertTrue("Exists " + refs, refs.isDirectory());
		assertTrue("Exists " + refs_heads, refs_heads.isDirectory());
		assertTrue("Exists " + refs_tags, refs_tags.isDirectory());
		assertTrue("Exists " + HEAD, HEAD.isFile());
		assertEquals(23, HEAD.length());
	}

	public void test000_openRepoBadArgs() throws IOException {
		try {
			new FileRepositoryBuilder().build();
			fail("Must pass either GIT_DIR or GIT_WORK_TREE");
		} catch (IllegalArgumentException e) {
			assertEquals(
					"Either GIT_DIR or GIT_WORK_TREE must be passed to Repository constructor",
					e.getMessage());
		}
	}

	/**
	 * Check the default rules for looking up directories and files within a
	 * repo when the gitDir is given.
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_default_gitDirSet() throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(repo1Parent, r.getWorkTree());
		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
		assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
	}

	/**
	 * Check that we can pass both a git directory and a work tree
	 * repo when the gitDir is given.
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_default_gitDirAndWorkTreeSet() throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder().setGitDir(theDir)
				.setWorkTree(repo1Parent.getParentFile()).build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(repo1Parent.getParentFile(), r.getWorkTree());
		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
		assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
	}

	/**
	 * Check the default rules for looking up directories and files within a
	 * repo when the workTree is given.
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_default_workDirSet() throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder().setWorkTree(repo1Parent).build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(repo1Parent, r.getWorkTree());
		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
		assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
	}

	/**
	 * Check that worktree config has an effect, given absolute path.
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_default_absolute_workdirconfig()
			throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		File workdir = new File(trash.getParentFile(), "rw");
		workdir.mkdir();
		FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.getConfig().setString("core", null, "worktree",
				workdir.getAbsolutePath());
		repo1initial.getConfig().save();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(workdir, r.getWorkTree());
		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
		assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
	}

	/**
	 * Check that worktree config has an effect, given a relative path.
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_default_relative_workdirconfig()
			throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		File workdir = new File(trash.getParentFile(), "rw");
		workdir.mkdir();
		FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.getConfig()
				.setString("core", null, "worktree", "../../rw");
		repo1initial.getConfig().save();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder().setGitDir(theDir).build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(workdir, r.getWorkTree());
		assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
		assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
	}

	/**
	 * Check that the given index file is honored and the alternate object
	 * directories too
	 *
	 * @throws IOException
	 */
	public void test000_openrepo_alternate_index_file_and_objdirs()
			throws IOException {
		File repo1Parent = new File(trash.getParentFile(), "r1");
		File indexFile = new File(trash, "idx");
		File objDir = new File(trash, "../obj");
		File altObjDir = db.getObjectsDirectory();
		Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
		repo1initial.create();
		repo1initial.close();

		File theDir = new File(repo1Parent, Constants.DOT_GIT);
		FileRepository r = new FileRepositoryBuilder() //
				.setGitDir(theDir).setObjectDirectory(objDir) //
				.addAlternateObjectDirectory(altObjDir) //
				.setIndexFile(indexFile) //
				.build();
		assertEqualsPath(theDir, r.getDirectory());
		assertEqualsPath(theDir.getParentFile(), r.getWorkTree());
		assertEqualsPath(indexFile, r.getIndexFile());
		assertEqualsPath(objDir, r.getObjectsDirectory());
		assertNotNull(r.mapCommit("6db9c2ebf75590eef973081736730a9ea169a0c4"));
		// Must close or the default repo pack files created by this test gets
		// locked via the alternate object directories on Windows.
		r.close();
	}

	protected void assertEqualsPath(File expected, File actual)
			throws IOException {
		assertEquals(expected.getCanonicalPath(), actual.getCanonicalPath());
	}

	public void test002_WriteEmptyTree() throws IOException {
		// One of our test packs contains the empty tree object. If the pack is
		// open when we create it we won't write the object file out as a loose
		// object (as it already exists in the pack).
		//
		final Repository newdb = createBareRepository();
		final Tree t = new Tree(newdb);
		t.accept(new WriteTree(trash, newdb), TreeEntry.MODIFIED_ONLY);
		assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
				.name());
		final File o = new File(new File(new File(newdb.getDirectory(),
				"objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904");
		assertTrue("Exists " + o, o.isFile());
		assertTrue("Read-only " + o, !o.canWrite());
	}

	public void test002_WriteEmptyTree2() throws IOException {
		// File shouldn't exist as it is in a test pack.
		//
		final Tree t = new Tree(db);
		t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
		assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
				.name());
		final File o = new File(new File(
				new File(db.getDirectory(), "objects"), "4b"),
				"825dc642cb6eb9a060e54bf8d69288fbee4904");
		assertFalse("Exists " + o, o.isFile());
	}

	public void test003_WriteShouldBeEmptyTree() throws IOException {
		final Tree t = new Tree(db);
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		t.addFile("should-be-empty").setId(emptyId);
		t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
		assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.getId()
				.name());

		File o;
		o = new File(new File(new File(db.getDirectory(), "objects"), "7b"),
				"b943559a305bdd6bdee2cef6e5df2413c3d30a");
		assertTrue("Exists " + o, o.isFile());
		assertTrue("Read-only " + o, !o.canWrite());

		o = new File(new File(new File(db.getDirectory(), "objects"), "e6"),
				"9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
		assertTrue("Exists " + o, o.isFile());
		assertTrue("Read-only " + o, !o.canWrite());
	}

	public void test005_ReadSimpleConfig() {
		final Config c = db.getConfig();
		assertNotNull(c);
		assertEquals("0", c.getString("core", null, "repositoryformatversion"));
		assertEquals("0", c.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN"));
		assertEquals("true", c.getString("core", null, "filemode"));
		assertEquals("true", c.getString("cOrE", null, "fIlEModE"));
		assertNull(c.getString("notavalue", null, "reallyNotAValue"));
	}

	public void test006_ReadUglyConfig() throws IOException,
			ConfigInvalidException {
		final File cfg = new File(db.getDirectory(), "config");
		final FileBasedConfig c = new FileBasedConfig(cfg);
		final FileWriter pw = new FileWriter(cfg);
		final String configStr = "  [core];comment\n\tfilemode = yes\n"
				+ "[user]\n"
				+ "  email = A U Thor <thor@example.com> # Just an example...\n"
				+ " name = \"A  Thor \\\\ \\\"\\t \"\n"
				+ "    defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
				+ " to test\n";
		pw.write(configStr);
		pw.close();
		c.load();
		assertEquals("yes", c.getString("core", null, "filemode"));
		assertEquals("A U Thor <thor@example.com>", c
				.getString("user", null, "email"));
		assertEquals("A  Thor \\ \"\t ", c.getString("user", null, "name"));
		assertEquals("a many line\ncomment\n to test", c.getString("user",
				null, "defaultCheckInComment"));
		c.save();
		final FileReader fr = new FileReader(cfg);
		final char[] cbuf = new char[configStr.length()];
		fr.read(cbuf);
		fr.close();
		assertEquals(configStr, new String(cbuf));
	}

	public void test007_Open() throws IOException {
		final FileRepository db2 = new FileRepository(db.getDirectory());
		assertEquals(db.getDirectory(), db2.getDirectory());
		assertEquals(db.getObjectsDirectory(), db2.getObjectsDirectory());
		assertNotSame(db.getConfig(), db2.getConfig());
	}

	public void test008_FailOnWrongVersion() throws IOException {
		final File cfg = new File(db.getDirectory(), "config");
		final FileWriter pw = new FileWriter(cfg);
		final String badvers = "ihopethisisneveraversion";
		final String configStr = "[core]\n" + "\trepositoryFormatVersion="
				+ badvers + "\n";
		pw.write(configStr);
		pw.close();

		try {
			new FileRepository(db.getDirectory());
			fail("incorrectly opened a bad repository");
		} catch (IOException ioe) {
			assertTrue(ioe.getMessage().indexOf("format") > 0);
			assertTrue(ioe.getMessage().indexOf(badvers) > 0);
		}
	}

	public void test009_CreateCommitOldFormat() throws IOException {
		final Tree t = new Tree(db);
		final FileTreeEntry f = t.addFile("i-am-a-file");
		writeTrashFile(f.getName(), "and this is the data in me\n");
		t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
		assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
				t.getTreeId());

		final Commit c = new Commit(db);
		c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
		c.setMessage("A Commit\n");
		c.setTree(t);
		assertEquals(t.getTreeId(), c.getTreeId());
		c.commit();
		final ObjectId cmtid = ObjectId.fromString(
				"803aec4aba175e8ab1d666873c984c0308179099");
		assertEquals(cmtid, c.getCommitId());

		// Verify the commit we just wrote is in the correct format.
		ObjectDatabase odb = db.getObjectDatabase();
		assertTrue("is ObjectDirectory", odb instanceof ObjectDirectory);
		final XInputStream xis = new XInputStream(new FileInputStream(
				((ObjectDirectory) odb).fileFor(cmtid)));
		try {
			assertEquals(0x78, xis.readUInt8());
			assertEquals(0x9c, xis.readUInt8());
			assertTrue(0x789c % 31 == 0);
		} finally {
			xis.close();
		}

		// Verify we can read it.
		final Commit c2 = db.mapCommit(cmtid);
		assertNotNull(c2);
		assertEquals(c.getMessage(), c2.getMessage());
		assertEquals(c.getTreeId(), c2.getTreeId());
		assertEquals(c.getAuthor(), c2.getAuthor());
		assertEquals(c.getCommitter(), c2.getCommitter());
	}

	public void test012_SubtreeExternalSorting() throws IOException {
		final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tree t = new Tree(db);
		final FileTreeEntry e0 = t.addFile("a-");
		final FileTreeEntry e1 = t.addFile("a-b");
		final FileTreeEntry e2 = t.addFile("a/b");
		final FileTreeEntry e3 = t.addFile("a=");
		final FileTreeEntry e4 = t.addFile("a=b");

		e0.setId(emptyBlob);
		e1.setId(emptyBlob);
		e2.setId(emptyBlob);
		e3.setId(emptyBlob);
		e4.setId(emptyBlob);

		t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
		assertEquals(ObjectId.fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
				t.getId());
	}

	public void test020_createBlobTag() throws IOException {
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tag t = new Tag(db);
		t.setObjId(emptyId);
		t.setType("blob");
		t.setTag("test020");
		t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		t.setMessage("test020 tagged\n");
		t.tag();
		assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t.getTagId().name());

		Tag mapTag = db.mapTag("test020");
		assertEquals("blob", mapTag.getType());
		assertEquals("test020 tagged\n", mapTag.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
	}

	public void test020b_createBlobPlainTag() throws IOException {
		test020_createBlobTag();
		Tag t = new Tag(db);
		t.setTag("test020b");
		t.setObjId(ObjectId.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
		t.tag();

		Tag mapTag = db.mapTag("test020b");
		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());

		// We do not repeat the plain tag test for other object types
	}

	public void test021_createTreeTag() throws IOException {
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tree almostEmptyTree = new Tree(db);
		almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
		final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
		final Tag t = new Tag(db);
		t.setObjId(almostEmptyTreeId);
		t.setType("tree");
		t.setTag("test021");
		t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		t.setMessage("test021 tagged\n");
		t.tag();
		assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t.getTagId().name());

		Tag mapTag = db.mapTag("test021");
		assertEquals("tree", mapTag.getType());
		assertEquals("test021 tagged\n", mapTag.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
		assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag.getObjId().name());
	}

	public void test022_createCommitTag() throws IOException {
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tree almostEmptyTree = new Tree(db);
		almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
		final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
		final Commit almostEmptyCommit = new Commit(db);
		almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L, -2 * 60)); // not exactly the same
		almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L, -2 * 60));
		almostEmptyCommit.setMessage("test022\n");
		almostEmptyCommit.setTreeId(almostEmptyTreeId);
		ObjectId almostEmptyCommitId = new ObjectWriter(db).writeCommit(almostEmptyCommit);
		final Tag t = new Tag(db);
		t.setObjId(almostEmptyCommitId);
		t.setType("commit");
		t.setTag("test022");
		t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		t.setMessage("test022 tagged\n");
		t.tag();
		assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t.getTagId().name());

		Tag mapTag = db.mapTag("test022");
		assertEquals("commit", mapTag.getType());
		assertEquals("test022 tagged\n", mapTag.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
		assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().name());
	}

	public void test023_createCommitNonAnullii() throws IOException {
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tree almostEmptyTree = new Tree(db);
		almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
		final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
		Commit commit = new Commit(db);
		commit.setTreeId(almostEmptyTreeId);
		commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
		commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
		commit.setEncoding("UTF-8");
		commit.setMessage("\u00dcbergeeks");
		ObjectId cid = new ObjectWriter(db).writeCommit(commit);
		assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
		Commit loadedCommit = db.mapCommit(cid);
		assertNotSame(loadedCommit, commit);
		assertEquals(commit.getMessage(), loadedCommit.getMessage());
	}

	public void test024_createCommitNonAscii() throws IOException {
		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
		final Tree almostEmptyTree = new Tree(db);
		almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
		final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
		Commit commit = new Commit(db);
		commit.setTreeId(almostEmptyTreeId);
		commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
		commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
		commit.setEncoding("ISO-8859-1");
		commit.setMessage("\u00dcbergeeks");
		ObjectId cid = new ObjectWriter(db).writeCommit(commit);
		assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
	}

	public void test025_packedRefs() throws IOException {
		test020_createBlobTag();
		test021_createTreeTag();
		test022_createCommitTag();

		if (!new File(db.getDirectory(),"refs/tags/test020").delete()) throw new Error("Cannot delete unpacked tag");
		if (!new File(db.getDirectory(),"refs/tags/test021").delete()) throw new Error("Cannot delete unpacked tag");
		if (!new File(db.getDirectory(),"refs/tags/test022").delete()) throw new Error("Cannot delete unpacked tag");

		// We cannot resolve it now, since we have no ref
		Tag mapTag20missing = db.mapTag("test020");
		assertNull(mapTag20missing);

		// Construct packed refs file
		PrintWriter w = new PrintWriter(new FileWriter(new File(db.getDirectory(), "packed-refs")));
		w.println("# packed-refs with: peeled");
		w.println("6759556b09fbb4fd8ae5e315134481cc25d46954 refs/tags/test020");
		w.println("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
		w.println("b0517bc8dbe2096b419d42424cd7030733f4abe5 refs/tags/test021");
		w.println("^417c01c8795a35b8e835113a85a5c0c1c77f67fb");
		w.println("0ce2ebdb36076ef0b38adbe077a07d43b43e3807 refs/tags/test022");
		w.println("^b5d3b45a96b340441f5abb9080411705c51cc86c");
		w.close();
		((RefDirectory)db.getRefDatabase()).rescan();

		Tag mapTag20 = db.mapTag("test020");
		assertNotNull("have tag test020", mapTag20);
		assertEquals("blob", mapTag20.getType());
		assertEquals("test020 tagged\n", mapTag20.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag20.getAuthor());
		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag20.getObjId().name());

		Tag mapTag21 = db.mapTag("test021");
		assertEquals("tree", mapTag21.getType());
		assertEquals("test021 tagged\n", mapTag21.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag21.getAuthor());
		assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag21.getObjId().name());

		Tag mapTag22 = db.mapTag("test022");
		assertEquals("commit", mapTag22.getType());
		assertEquals("test022 tagged\n", mapTag22.getMessage());
		assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag22.getAuthor());
		assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag22.getObjId().name());
	}

	public void test025_computeSha1NoStore() throws IOException {
		byte[] data = "test025 some data, more than 16 bytes to get good coverage"
				.getBytes("ISO-8859-1");
		// TODO: but we do not test legacy header writing
		final ObjectId id = new ObjectWriter(db).computeBlobSha1(data.length,
				new ByteArrayInputStream(data));
		assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
	}

	public void test026_CreateCommitMultipleparents() throws IOException {
		final Tree t = new Tree(db);
		final FileTreeEntry f = t.addFile("i-am-a-file");
		writeTrashFile(f.getName(), "and this is the data in me\n");
		t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
		assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
				t.getTreeId());

		final Commit c1 = new Commit(db);
		c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
		c1.setMessage("A Commit\n");
		c1.setTree(t);
		assertEquals(t.getTreeId(), c1.getTreeId());
		c1.commit();
		final ObjectId cmtid1 = ObjectId.fromString(
				"803aec4aba175e8ab1d666873c984c0308179099");
		assertEquals(cmtid1, c1.getCommitId());

		final Commit c2 = new Commit(db);
		c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
		c2.setMessage("A Commit 2\n");
		c2.setTree(t);
		assertEquals(t.getTreeId(), c2.getTreeId());
		c2.setParentIds(new ObjectId[] { c1.getCommitId() } );
		c2.commit();
		final ObjectId cmtid2 = ObjectId.fromString(
				"95d068687c91c5c044fb8c77c5154d5247901553");
		assertEquals(cmtid2, c2.getCommitId());

		Commit rm2 = db.mapCommit(cmtid2);
		assertNotSame(c2, rm2); // assert the parsed objects is not from the cache
		assertEquals(c2.getAuthor(), rm2.getAuthor());
		assertEquals(c2.getCommitId(), rm2.getCommitId());
		assertEquals(c2.getMessage(), rm2.getMessage());
		assertEquals(c2.getTree().getTreeId(), rm2.getTree().getTreeId());
		assertEquals(1, rm2.getParentIds().length);
		assertEquals(c1.getCommitId(), rm2.getParentIds()[0]);

		final Commit c3 = new Commit(db);
		c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
		c3.setMessage("A Commit 3\n");
		c3.setTree(t);
		assertEquals(t.getTreeId(), c3.getTreeId());
		c3.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId() });
		c3.commit();
		final ObjectId cmtid3 = ObjectId.fromString(
				"ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
		assertEquals(cmtid3, c3.getCommitId());

		Commit rm3 = db.mapCommit(cmtid3);
		assertNotSame(c3, rm3); // assert the parsed objects is not from the cache
		assertEquals(c3.getAuthor(), rm3.getAuthor());
		assertEquals(c3.getCommitId(), rm3.getCommitId());
		assertEquals(c3.getMessage(), rm3.getMessage());
		assertEquals(c3.getTree().getTreeId(), rm3.getTree().getTreeId());
		assertEquals(2, rm3.getParentIds().length);
		assertEquals(c1.getCommitId(), rm3.getParentIds()[0]);
		assertEquals(c2.getCommitId(), rm3.getParentIds()[1]);

		final Commit c4 = new Commit(db);
		c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
		c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
		c4.setMessage("A Commit 4\n");
		c4.setTree(t);
		assertEquals(t.getTreeId(), c3.getTreeId());
		c4.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId(), c3.getCommitId() });
		c4.commit();
		final ObjectId cmtid4 = ObjectId.fromString(
				"d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
		assertEquals(cmtid4, c4.getCommitId());

		Commit rm4 = db.mapCommit(cmtid4);
		assertNotSame(c4, rm3); // assert the parsed objects is not from the cache
		assertEquals(c4.getAuthor(), rm4.getAuthor());
		assertEquals(c4.getCommitId(), rm4.getCommitId());
		assertEquals(c4.getMessage(), rm4.getMessage());
		assertEquals(c4.getTree().getTreeId(), rm4.getTree().getTreeId());
		assertEquals(3, rm4.getParentIds().length);
		assertEquals(c1.getCommitId(), rm4.getParentIds()[0]);
		assertEquals(c2.getCommitId(), rm4.getParentIds()[1]);
		assertEquals(c3.getCommitId(), rm4.getParentIds()[2]);
	}

	public void test027_UnpackedRefHigherPriorityThanPacked() throws IOException {
		PrintWriter writer = new PrintWriter(new FileWriter(new File(db.getDirectory(), "refs/heads/a")));
		String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
		writer.print(unpackedId);
		writer.print('\n');
		writer.close();

		ObjectId resolved = db.resolve("refs/heads/a");
		assertEquals(unpackedId, resolved.name());
	}

	public void test028_LockPackedRef() throws IOException {
		writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
		writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
		BUG_WorkAroundRacyGitIssues("packed-refs");
		BUG_WorkAroundRacyGitIssues("HEAD");

		ObjectId resolve = db.resolve("HEAD");
		assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());

		RefUpdate lockRef = db.updateRef("HEAD");
		ObjectId newId = ObjectId.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
		lockRef.setNewObjectId(newId);
		assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());

		assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
		assertEquals(newId, db.resolve("refs/heads/foobar"));

		// Again. The ref already exists
		RefUpdate lockRef2 = db.updateRef("HEAD");
		ObjectId newId2 = ObjectId.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
		lockRef2.setNewObjectId(newId2);
		assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());

		assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
		assertEquals(newId2, db.resolve("refs/heads/foobar"));
	}

	public void test029_mapObject() throws IOException {
		assertEquals(new byte[0].getClass(), db.mapObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), null).getClass());
		assertEquals(Commit.class, db.mapObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"), null).getClass());
		assertEquals(Tree.class, db.mapObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"), null).getClass());
		assertEquals(Tag.class, db.mapObject(ObjectId.fromString("17768080a2318cd89bba4c8b87834401e2095703"), null).getClass());
	}

	public void test30_stripWorkDir() {
		File relCwd = new File(".");
		File absCwd = relCwd.getAbsoluteFile();
		File absBase = new File(new File(absCwd, "repo"), "workdir");
		File relBase = new File(new File(relCwd, "repo"), "workdir");
		assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());

		File relBaseFile = new File(new File(relBase, "other"), "module.c");
		File absBaseFile = new File(new File(absBase, "other"), "module.c");
		assertEquals("other/module.c", Repository.stripWorkDir(relBase, relBaseFile));
		assertEquals("other/module.c", Repository.stripWorkDir(relBase, absBaseFile));
		assertEquals("other/module.c", Repository.stripWorkDir(absBase, relBaseFile));
		assertEquals("other/module.c", Repository.stripWorkDir(absBase, absBaseFile));

		File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
		File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
		assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
		assertEquals("", Repository.stripWorkDir(absBase, absNonFile));

		assertEquals("", Repository.stripWorkDir(db.getWorkTree(), db.getWorkTree()));

		File file = new File(new File(db.getWorkTree(), "subdir"), "File.java");
		assertEquals("subdir/File.java", Repository.stripWorkDir(db.getWorkTree(), file));

	}

	/**
	 * Kick the timestamp of a local file.
	 * <p>
	 * We shouldn't have to make these method calls. The cache is using file
	 * system timestamps, and on many systems unit tests run faster than the
	 * modification clock. Dumping the cache after we make an edit behind
	 * RefDirectory's back allows the tests to pass.
	 *
	 * @param name
	 *            the file in the repository to force a time change on.
	 */
	private void BUG_WorkAroundRacyGitIssues(String name) {
		File path = new File(db.getDirectory(), name);
		long old = path.lastModified();
		long set = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
		path.setLastModified(set);
		assertTrue("time changed", old != path.lastModified());
	}
}