You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

T0003_Basic.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import java.io.ByteArrayInputStream;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileReader;
  50. import java.io.FileWriter;
  51. import java.io.IOException;
  52. import java.io.PrintWriter;
  53. import org.eclipse.jgit.errors.ConfigInvalidException;
  54. public class T0003_Basic extends RepositoryTestCase {
  55. public void test001_Initalize() {
  56. final File gitdir = new File(trash, ".git");
  57. final File objects = new File(gitdir, "objects");
  58. final File objects_pack = new File(objects, "pack");
  59. final File objects_info = new File(objects, "info");
  60. final File refs = new File(gitdir, "refs");
  61. final File refs_heads = new File(refs, "heads");
  62. final File refs_tags = new File(refs, "tags");
  63. final File HEAD = new File(gitdir, "HEAD");
  64. assertTrue("Exists " + trash, trash.isDirectory());
  65. assertTrue("Exists " + objects, objects.isDirectory());
  66. assertTrue("Exists " + objects_pack, objects_pack.isDirectory());
  67. assertTrue("Exists " + objects_info, objects_info.isDirectory());
  68. assertEquals(2, objects.listFiles().length);
  69. assertTrue("Exists " + refs, refs.isDirectory());
  70. assertTrue("Exists " + refs_heads, refs_heads.isDirectory());
  71. assertTrue("Exists " + refs_tags, refs_tags.isDirectory());
  72. assertTrue("Exists " + HEAD, HEAD.isFile());
  73. assertEquals(23, HEAD.length());
  74. }
  75. public void test002_WriteEmptyTree() throws IOException {
  76. // One of our test packs contains the empty tree object. If the pack is
  77. // open when we create it we won't write the object file out as a loose
  78. // object (as it already exists in the pack).
  79. //
  80. final Repository newdb = createNewEmptyRepo();
  81. final Tree t = new Tree(newdb);
  82. t.accept(new WriteTree(trash, newdb), TreeEntry.MODIFIED_ONLY);
  83. assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
  84. .name());
  85. final File o = new File(new File(new File(newdb.getDirectory(),
  86. "objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904");
  87. assertTrue("Exists " + o, o.isFile());
  88. assertTrue("Read-only " + o, !o.canWrite());
  89. }
  90. public void test002_WriteEmptyTree2() throws IOException {
  91. // File shouldn't exist as it is in a test pack.
  92. //
  93. final Tree t = new Tree(db);
  94. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  95. assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
  96. .name());
  97. final File o = new File(new File(new File(trash_git, "objects"), "4b"),
  98. "825dc642cb6eb9a060e54bf8d69288fbee4904");
  99. assertFalse("Exists " + o, o.isFile());
  100. }
  101. public void test003_WriteShouldBeEmptyTree() throws IOException {
  102. final Tree t = new Tree(db);
  103. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  104. t.addFile("should-be-empty").setId(emptyId);
  105. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  106. assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.getId()
  107. .name());
  108. File o;
  109. o = new File(new File(new File(trash_git, "objects"), "7b"),
  110. "b943559a305bdd6bdee2cef6e5df2413c3d30a");
  111. assertTrue("Exists " + o, o.isFile());
  112. assertTrue("Read-only " + o, !o.canWrite());
  113. o = new File(new File(new File(trash_git, "objects"), "e6"),
  114. "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  115. assertTrue("Exists " + o, o.isFile());
  116. assertTrue("Read-only " + o, !o.canWrite());
  117. }
  118. public void test005_ReadSimpleConfig() {
  119. final RepositoryConfig c = db.getConfig();
  120. assertNotNull(c);
  121. assertEquals("0", c.getString("core", null, "repositoryformatversion"));
  122. assertEquals("0", c.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN"));
  123. assertEquals("true", c.getString("core", null, "filemode"));
  124. assertEquals("true", c.getString("cOrE", null, "fIlEModE"));
  125. assertNull(c.getString("notavalue", null, "reallyNotAValue"));
  126. }
  127. public void test006_ReadUglyConfig() throws IOException,
  128. ConfigInvalidException {
  129. final RepositoryConfig c = db.getConfig();
  130. final File cfg = new File(db.getDirectory(), "config");
  131. final FileWriter pw = new FileWriter(cfg);
  132. final String configStr = " [core];comment\n\tfilemode = yes\n"
  133. + "[user]\n"
  134. + " email = A U Thor <thor@example.com> # Just an example...\n"
  135. + " name = \"A Thor \\\\ \\\"\\t \"\n"
  136. + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
  137. + " to test\n";
  138. pw.write(configStr);
  139. pw.close();
  140. c.load();
  141. assertEquals("yes", c.getString("core", null, "filemode"));
  142. assertEquals("A U Thor <thor@example.com>", c
  143. .getString("user", null, "email"));
  144. assertEquals("A Thor \\ \"\t ", c.getString("user", null, "name"));
  145. assertEquals("a many line\ncomment\n to test", c.getString("user",
  146. null, "defaultCheckInComment"));
  147. c.save();
  148. final FileReader fr = new FileReader(cfg);
  149. final char[] cbuf = new char[configStr.length()];
  150. fr.read(cbuf);
  151. fr.close();
  152. assertEquals(configStr, new String(cbuf));
  153. }
  154. public void test007_Open() throws IOException {
  155. final Repository db2 = new Repository(db.getDirectory());
  156. assertEquals(db.getDirectory(), db2.getDirectory());
  157. assertEquals(db.getObjectsDirectory(), db2.getObjectsDirectory());
  158. assertNotSame(db.getConfig(), db2.getConfig());
  159. }
  160. public void test008_FailOnWrongVersion() throws IOException {
  161. final File cfg = new File(db.getDirectory(), "config");
  162. final FileWriter pw = new FileWriter(cfg);
  163. final String badvers = "ihopethisisneveraversion";
  164. final String configStr = "[core]\n" + "\trepositoryFormatVersion="
  165. + badvers + "\n";
  166. pw.write(configStr);
  167. pw.close();
  168. try {
  169. new Repository(db.getDirectory());
  170. fail("incorrectly opened a bad repository");
  171. } catch (IOException ioe) {
  172. assertTrue(ioe.getMessage().indexOf("format") > 0);
  173. assertTrue(ioe.getMessage().indexOf(badvers) > 0);
  174. }
  175. }
  176. public void test009_CreateCommitOldFormat() throws IOException,
  177. ConfigInvalidException {
  178. writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
  179. db.getConfig().load();
  180. final Tree t = new Tree(db);
  181. final FileTreeEntry f = t.addFile("i-am-a-file");
  182. writeTrashFile(f.getName(), "and this is the data in me\n");
  183. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  184. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  185. t.getTreeId());
  186. final Commit c = new Commit(db);
  187. c.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  188. c.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
  189. c.setMessage("A Commit\n");
  190. c.setTree(t);
  191. assertEquals(t.getTreeId(), c.getTreeId());
  192. c.commit();
  193. final ObjectId cmtid = ObjectId.fromString(
  194. "803aec4aba175e8ab1d666873c984c0308179099");
  195. assertEquals(cmtid, c.getCommitId());
  196. // Verify the commit we just wrote is in the correct format.
  197. final XInputStream xis = new XInputStream(new FileInputStream(db
  198. .toFile(cmtid)));
  199. try {
  200. assertEquals(0x78, xis.readUInt8());
  201. assertEquals(0x9c, xis.readUInt8());
  202. assertTrue(0x789c % 31 == 0);
  203. } finally {
  204. xis.close();
  205. }
  206. // Verify we can read it.
  207. final Commit c2 = db.mapCommit(cmtid);
  208. assertNotNull(c2);
  209. assertEquals(c.getMessage(), c2.getMessage());
  210. assertEquals(c.getTreeId(), c2.getTreeId());
  211. assertEquals(c.getAuthor(), c2.getAuthor());
  212. assertEquals(c.getCommitter(), c2.getCommitter());
  213. }
  214. public void test012_SubtreeExternalSorting() throws IOException {
  215. final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]);
  216. final Tree t = new Tree(db);
  217. final FileTreeEntry e0 = t.addFile("a-");
  218. final FileTreeEntry e1 = t.addFile("a-b");
  219. final FileTreeEntry e2 = t.addFile("a/b");
  220. final FileTreeEntry e3 = t.addFile("a=");
  221. final FileTreeEntry e4 = t.addFile("a=b");
  222. e0.setId(emptyBlob);
  223. e1.setId(emptyBlob);
  224. e2.setId(emptyBlob);
  225. e3.setId(emptyBlob);
  226. e4.setId(emptyBlob);
  227. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  228. assertEquals(ObjectId.fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
  229. t.getId());
  230. }
  231. public void test020_createBlobTag() throws IOException {
  232. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  233. final Tag t = new Tag(db);
  234. t.setObjId(emptyId);
  235. t.setType("blob");
  236. t.setTag("test020");
  237. t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  238. t.setMessage("test020 tagged\n");
  239. t.tag();
  240. assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t.getTagId().name());
  241. Tag mapTag = db.mapTag("test020");
  242. assertEquals("blob", mapTag.getType());
  243. assertEquals("test020 tagged\n", mapTag.getMessage());
  244. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
  245. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  246. }
  247. public void test020b_createBlobPlainTag() throws IOException {
  248. test020_createBlobTag();
  249. Tag t = new Tag(db);
  250. t.setTag("test020b");
  251. t.setObjId(ObjectId.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
  252. t.tag();
  253. Tag mapTag = db.mapTag("test020b");
  254. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  255. // We do not repeat the plain tag test for other object types
  256. }
  257. public void test021_createTreeTag() throws IOException {
  258. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  259. final Tree almostEmptyTree = new Tree(db);
  260. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  261. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  262. final Tag t = new Tag(db);
  263. t.setObjId(almostEmptyTreeId);
  264. t.setType("tree");
  265. t.setTag("test021");
  266. t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  267. t.setMessage("test021 tagged\n");
  268. t.tag();
  269. assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t.getTagId().name());
  270. Tag mapTag = db.mapTag("test021");
  271. assertEquals("tree", mapTag.getType());
  272. assertEquals("test021 tagged\n", mapTag.getMessage());
  273. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
  274. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag.getObjId().name());
  275. }
  276. public void test022_createCommitTag() throws IOException {
  277. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  278. final Tree almostEmptyTree = new Tree(db);
  279. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  280. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  281. final Commit almostEmptyCommit = new Commit(db);
  282. almostEmptyCommit.setAuthor(new PersonIdent(jauthor, 1154236443000L, -2 * 60)); // not exactly the same
  283. almostEmptyCommit.setCommitter(new PersonIdent(jauthor, 1154236443000L, -2 * 60));
  284. almostEmptyCommit.setMessage("test022\n");
  285. almostEmptyCommit.setTreeId(almostEmptyTreeId);
  286. ObjectId almostEmptyCommitId = new ObjectWriter(db).writeCommit(almostEmptyCommit);
  287. final Tag t = new Tag(db);
  288. t.setObjId(almostEmptyCommitId);
  289. t.setType("commit");
  290. t.setTag("test022");
  291. t.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  292. t.setMessage("test022 tagged\n");
  293. t.tag();
  294. assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t.getTagId().name());
  295. Tag mapTag = db.mapTag("test022");
  296. assertEquals("commit", mapTag.getType());
  297. assertEquals("test022 tagged\n", mapTag.getMessage());
  298. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag.getAuthor());
  299. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().name());
  300. }
  301. public void test023_createCommitNonAnullii() throws IOException {
  302. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  303. final Tree almostEmptyTree = new Tree(db);
  304. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  305. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  306. Commit commit = new Commit(db);
  307. commit.setTreeId(almostEmptyTreeId);
  308. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  309. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  310. commit.setEncoding("UTF-8");
  311. commit.setMessage("\u00dcbergeeks");
  312. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  313. assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
  314. }
  315. public void test024_createCommitNonAscii() throws IOException {
  316. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  317. final Tree almostEmptyTree = new Tree(db);
  318. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  319. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  320. Commit commit = new Commit(db);
  321. commit.setTreeId(almostEmptyTreeId);
  322. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  323. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  324. commit.setEncoding("ISO-8859-1");
  325. commit.setMessage("\u00dcbergeeks");
  326. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  327. assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
  328. }
  329. public void test025_packedRefs() throws IOException {
  330. test020_createBlobTag();
  331. test021_createTreeTag();
  332. test022_createCommitTag();
  333. if (!new File(db.getDirectory(),"refs/tags/test020").delete()) throw new Error("Cannot delete unpacked tag");
  334. if (!new File(db.getDirectory(),"refs/tags/test021").delete()) throw new Error("Cannot delete unpacked tag");
  335. if (!new File(db.getDirectory(),"refs/tags/test022").delete()) throw new Error("Cannot delete unpacked tag");
  336. // We cannot resolve it now, since we have no ref
  337. Tag mapTag20missing = db.mapTag("test020");
  338. assertNull(mapTag20missing);
  339. // Construct packed refs file
  340. PrintWriter w = new PrintWriter(new FileWriter(new File(db.getDirectory(), "packed-refs")));
  341. w.println("# packed-refs with: peeled");
  342. w.println("6759556b09fbb4fd8ae5e315134481cc25d46954 refs/tags/test020");
  343. w.println("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  344. w.println("b0517bc8dbe2096b419d42424cd7030733f4abe5 refs/tags/test021");
  345. w.println("^417c01c8795a35b8e835113a85a5c0c1c77f67fb");
  346. w.println("0ce2ebdb36076ef0b38adbe077a07d43b43e3807 refs/tags/test022");
  347. w.println("^b5d3b45a96b340441f5abb9080411705c51cc86c");
  348. w.close();
  349. Tag mapTag20 = db.mapTag("test020");
  350. assertNotNull("have tag test020", mapTag20);
  351. assertEquals("blob", mapTag20.getType());
  352. assertEquals("test020 tagged\n", mapTag20.getMessage());
  353. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag20.getAuthor());
  354. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag20.getObjId().name());
  355. Tag mapTag21 = db.mapTag("test021");
  356. assertEquals("tree", mapTag21.getType());
  357. assertEquals("test021 tagged\n", mapTag21.getMessage());
  358. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag21.getAuthor());
  359. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag21.getObjId().name());
  360. Tag mapTag22 = db.mapTag("test022");
  361. assertEquals("commit", mapTag22.getType());
  362. assertEquals("test022 tagged\n", mapTag22.getMessage());
  363. assertEquals(new PersonIdent(jauthor, 1154236443000L, -4 * 60), mapTag22.getAuthor());
  364. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag22.getObjId().name());
  365. }
  366. public void test025_computeSha1NoStore() throws IOException {
  367. byte[] data = "test025 some data, more than 16 bytes to get good coverage"
  368. .getBytes("ISO-8859-1");
  369. // TODO: but we do not test legacy header writing
  370. final ObjectId id = new ObjectWriter(db).computeBlobSha1(data.length,
  371. new ByteArrayInputStream(data));
  372. assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
  373. }
  374. public void test026_CreateCommitMultipleparents() throws IOException {
  375. final Tree t = new Tree(db);
  376. final FileTreeEntry f = t.addFile("i-am-a-file");
  377. writeTrashFile(f.getName(), "and this is the data in me\n");
  378. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  379. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  380. t.getTreeId());
  381. final Commit c1 = new Commit(db);
  382. c1.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  383. c1.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
  384. c1.setMessage("A Commit\n");
  385. c1.setTree(t);
  386. assertEquals(t.getTreeId(), c1.getTreeId());
  387. c1.commit();
  388. final ObjectId cmtid1 = ObjectId.fromString(
  389. "803aec4aba175e8ab1d666873c984c0308179099");
  390. assertEquals(cmtid1, c1.getCommitId());
  391. final Commit c2 = new Commit(db);
  392. c2.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  393. c2.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
  394. c2.setMessage("A Commit 2\n");
  395. c2.setTree(t);
  396. assertEquals(t.getTreeId(), c2.getTreeId());
  397. c2.setParentIds(new ObjectId[] { c1.getCommitId() } );
  398. c2.commit();
  399. final ObjectId cmtid2 = ObjectId.fromString(
  400. "95d068687c91c5c044fb8c77c5154d5247901553");
  401. assertEquals(cmtid2, c2.getCommitId());
  402. Commit rm2 = db.mapCommit(cmtid2);
  403. assertNotSame(c2, rm2); // assert the parsed objects is not from the cache
  404. assertEquals(c2.getAuthor(), rm2.getAuthor());
  405. assertEquals(c2.getCommitId(), rm2.getCommitId());
  406. assertEquals(c2.getMessage(), rm2.getMessage());
  407. assertEquals(c2.getTree().getTreeId(), rm2.getTree().getTreeId());
  408. assertEquals(1, rm2.getParentIds().length);
  409. assertEquals(c1.getCommitId(), rm2.getParentIds()[0]);
  410. final Commit c3 = new Commit(db);
  411. c3.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  412. c3.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
  413. c3.setMessage("A Commit 3\n");
  414. c3.setTree(t);
  415. assertEquals(t.getTreeId(), c3.getTreeId());
  416. c3.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId() });
  417. c3.commit();
  418. final ObjectId cmtid3 = ObjectId.fromString(
  419. "ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
  420. assertEquals(cmtid3, c3.getCommitId());
  421. Commit rm3 = db.mapCommit(cmtid3);
  422. assertNotSame(c3, rm3); // assert the parsed objects is not from the cache
  423. assertEquals(c3.getAuthor(), rm3.getAuthor());
  424. assertEquals(c3.getCommitId(), rm3.getCommitId());
  425. assertEquals(c3.getMessage(), rm3.getMessage());
  426. assertEquals(c3.getTree().getTreeId(), rm3.getTree().getTreeId());
  427. assertEquals(2, rm3.getParentIds().length);
  428. assertEquals(c1.getCommitId(), rm3.getParentIds()[0]);
  429. assertEquals(c2.getCommitId(), rm3.getParentIds()[1]);
  430. final Commit c4 = new Commit(db);
  431. c4.setAuthor(new PersonIdent(jauthor, 1154236443000L, -4 * 60));
  432. c4.setCommitter(new PersonIdent(jcommitter, 1154236443000L, -4 * 60));
  433. c4.setMessage("A Commit 4\n");
  434. c4.setTree(t);
  435. assertEquals(t.getTreeId(), c3.getTreeId());
  436. c4.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId(), c3.getCommitId() });
  437. c4.commit();
  438. final ObjectId cmtid4 = ObjectId.fromString(
  439. "d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
  440. assertEquals(cmtid4, c4.getCommitId());
  441. Commit rm4 = db.mapCommit(cmtid4);
  442. assertNotSame(c4, rm3); // assert the parsed objects is not from the cache
  443. assertEquals(c4.getAuthor(), rm4.getAuthor());
  444. assertEquals(c4.getCommitId(), rm4.getCommitId());
  445. assertEquals(c4.getMessage(), rm4.getMessage());
  446. assertEquals(c4.getTree().getTreeId(), rm4.getTree().getTreeId());
  447. assertEquals(3, rm4.getParentIds().length);
  448. assertEquals(c1.getCommitId(), rm4.getParentIds()[0]);
  449. assertEquals(c2.getCommitId(), rm4.getParentIds()[1]);
  450. assertEquals(c3.getCommitId(), rm4.getParentIds()[2]);
  451. }
  452. public void test027_UnpackedRefHigherPriorityThanPacked() throws IOException {
  453. PrintWriter writer = new PrintWriter(new FileWriter(new File(db.getDirectory(), "refs/heads/a")));
  454. String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
  455. writer.print(unpackedId);
  456. writer.print('\n');
  457. writer.close();
  458. ObjectId resolved = db.resolve("refs/heads/a");
  459. assertEquals(unpackedId, resolved.name());
  460. }
  461. public void test028_LockPackedRef() throws IOException {
  462. writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
  463. writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
  464. ObjectId resolve = db.resolve("HEAD");
  465. assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
  466. RefUpdate lockRef = db.updateRef("HEAD");
  467. ObjectId newId = ObjectId.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
  468. lockRef.setNewObjectId(newId);
  469. assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
  470. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  471. assertEquals(newId, db.resolve("refs/heads/foobar"));
  472. // Again. The ref already exists
  473. RefUpdate lockRef2 = db.updateRef("HEAD");
  474. ObjectId newId2 = ObjectId.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
  475. lockRef2.setNewObjectId(newId2);
  476. assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
  477. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  478. assertEquals(newId2, db.resolve("refs/heads/foobar"));
  479. }
  480. public void test029_mapObject() throws IOException {
  481. assertEquals(new byte[0].getClass(), db.mapObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), null).getClass());
  482. assertEquals(Commit.class, db.mapObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"), null).getClass());
  483. assertEquals(Tree.class, db.mapObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"), null).getClass());
  484. assertEquals(Tag.class, db.mapObject(ObjectId.fromString("17768080a2318cd89bba4c8b87834401e2095703"), null).getClass());
  485. }
  486. public void test30_stripWorkDir() {
  487. File relCwd = new File(".");
  488. File absCwd = relCwd.getAbsoluteFile();
  489. File absBase = new File(new File(absCwd, "repo"), "workdir");
  490. File relBase = new File(new File(relCwd, "repo"), "workdir");
  491. assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
  492. File relBaseFile = new File(new File(relBase, "other"), "module.c");
  493. File absBaseFile = new File(new File(absBase, "other"), "module.c");
  494. assertEquals("other/module.c", Repository.stripWorkDir(relBase, relBaseFile));
  495. assertEquals("other/module.c", Repository.stripWorkDir(relBase, absBaseFile));
  496. assertEquals("other/module.c", Repository.stripWorkDir(absBase, relBaseFile));
  497. assertEquals("other/module.c", Repository.stripWorkDir(absBase, absBaseFile));
  498. File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
  499. File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
  500. assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
  501. assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
  502. assertEquals("", Repository.stripWorkDir(db.getWorkDir(), db.getWorkDir()));
  503. File file = new File(new File(db.getWorkDir(), "subdir"), "File.java");
  504. assertEquals("subdir/File.java", Repository.stripWorkDir(db.getWorkDir(), file));
  505. }
  506. }