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 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. * 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 SampleDataRepositoryTestCase {
  55. public void test001_Initalize() {
  56. final File gitdir = new File(trash, Constants.DOT_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 test000_openRepoBadArgs() throws IOException {
  76. try {
  77. new FileRepository(null, null);
  78. fail("Must pass either GIT_DIR or GIT_WORK_TREE");
  79. } catch (IllegalArgumentException e) {
  80. assertEquals(
  81. "Either GIT_DIR or GIT_WORK_TREE must be passed to Repository constructor",
  82. e.getMessage());
  83. }
  84. }
  85. /**
  86. * Check the default rules for looking up directories and files within a
  87. * repo when the gitDir is given.
  88. *
  89. * @throws IOException
  90. */
  91. public void test000_openrepo_default_gitDirSet() throws IOException {
  92. File repo1Parent = new File(trash.getParentFile(), "r1");
  93. Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  94. repo1initial.create();
  95. repo1initial.close();
  96. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  97. Repository r = new FileRepository(theDir, null);
  98. assertEqualsPath(theDir, r.getDirectory());
  99. assertEqualsPath(repo1Parent, r.getWorkDir());
  100. assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
  101. assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
  102. }
  103. /**
  104. * Check that we can pass both a git directory and a work tree
  105. * repo when the gitDir is given.
  106. *
  107. * @throws IOException
  108. */
  109. public void test000_openrepo_default_gitDirAndWorkTreeSet() throws IOException {
  110. File repo1Parent = new File(trash.getParentFile(), "r1");
  111. Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  112. repo1initial.create();
  113. repo1initial.close();
  114. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  115. Repository r = new FileRepository(theDir, repo1Parent.getParentFile());
  116. assertEqualsPath(theDir, r.getDirectory());
  117. assertEqualsPath(repo1Parent.getParentFile(), r.getWorkDir());
  118. assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
  119. assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
  120. }
  121. /**
  122. * Check the default rules for looking up directories and files within a
  123. * repo when the workTree is given.
  124. *
  125. * @throws IOException
  126. */
  127. public void test000_openrepo_default_workDirSet() throws IOException {
  128. File repo1Parent = new File(trash.getParentFile(), "r1");
  129. Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  130. repo1initial.create();
  131. repo1initial.close();
  132. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  133. Repository r = new FileRepository(null, repo1Parent);
  134. assertEqualsPath(theDir, r.getDirectory());
  135. assertEqualsPath(repo1Parent, r.getWorkDir());
  136. assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
  137. assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
  138. }
  139. /**
  140. * Check that worktree config has an effect, given absolute path.
  141. *
  142. * @throws IOException
  143. */
  144. public void test000_openrepo_default_absolute_workdirconfig()
  145. throws IOException {
  146. File repo1Parent = new File(trash.getParentFile(), "r1");
  147. File workdir = new File(trash.getParentFile(), "rw");
  148. workdir.mkdir();
  149. FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  150. repo1initial.create();
  151. repo1initial.getConfig().setString("core", null, "worktree",
  152. workdir.getAbsolutePath());
  153. repo1initial.getConfig().save();
  154. repo1initial.close();
  155. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  156. Repository r = new FileRepository(theDir, null);
  157. assertEqualsPath(theDir, r.getDirectory());
  158. assertEqualsPath(workdir, r.getWorkDir());
  159. assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
  160. assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
  161. }
  162. /**
  163. * Check that worktree config has an effect, given a relative path.
  164. *
  165. * @throws IOException
  166. */
  167. public void test000_openrepo_default_relative_workdirconfig()
  168. throws IOException {
  169. File repo1Parent = new File(trash.getParentFile(), "r1");
  170. File workdir = new File(trash.getParentFile(), "rw");
  171. workdir.mkdir();
  172. FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  173. repo1initial.create();
  174. repo1initial.getConfig()
  175. .setString("core", null, "worktree", "../../rw");
  176. repo1initial.getConfig().save();
  177. repo1initial.close();
  178. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  179. Repository r = new FileRepository(theDir, null);
  180. assertEqualsPath(theDir, r.getDirectory());
  181. assertEqualsPath(workdir, r.getWorkDir());
  182. assertEqualsPath(new File(theDir, "index"), r.getIndexFile());
  183. assertEqualsPath(new File(theDir, "objects"), r.getObjectsDirectory());
  184. }
  185. /**
  186. * Check that the given index file is honored and the alternate object
  187. * directories too
  188. *
  189. * @throws IOException
  190. */
  191. public void test000_openrepo_alternate_index_file_and_objdirs()
  192. throws IOException {
  193. File repo1Parent = new File(trash.getParentFile(), "r1");
  194. File indexFile = new File(trash, "idx");
  195. File objDir = new File(trash, "../obj");
  196. File[] altObjDirs = new File[] { db.getObjectsDirectory() };
  197. Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
  198. repo1initial.create();
  199. repo1initial.close();
  200. File theDir = new File(repo1Parent, Constants.DOT_GIT);
  201. Repository r = new FileRepository(theDir, null, objDir, altObjDirs,
  202. indexFile);
  203. assertEqualsPath(theDir, r.getDirectory());
  204. assertEqualsPath(theDir.getParentFile(), r.getWorkDir());
  205. assertEqualsPath(indexFile, r.getIndexFile());
  206. assertEqualsPath(objDir, r.getObjectsDirectory());
  207. assertNotNull(r.mapCommit("6db9c2ebf75590eef973081736730a9ea169a0c4"));
  208. // Must close or the default repo pack files created by this test gets
  209. // locked via the alternate object directories on Windows.
  210. r.close();
  211. }
  212. protected void assertEqualsPath(File expected, File actual)
  213. throws IOException {
  214. assertEquals(expected.getCanonicalPath(), actual.getCanonicalPath());
  215. }
  216. public void test002_WriteEmptyTree() throws IOException {
  217. // One of our test packs contains the empty tree object. If the pack is
  218. // open when we create it we won't write the object file out as a loose
  219. // object (as it already exists in the pack).
  220. //
  221. final Repository newdb = createBareRepository();
  222. final Tree t = new Tree(newdb);
  223. t.accept(new WriteTree(trash, newdb), TreeEntry.MODIFIED_ONLY);
  224. assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
  225. .name());
  226. final File o = new File(new File(new File(newdb.getDirectory(),
  227. "objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904");
  228. assertTrue("Exists " + o, o.isFile());
  229. assertTrue("Read-only " + o, !o.canWrite());
  230. }
  231. public void test002_WriteEmptyTree2() throws IOException {
  232. // File shouldn't exist as it is in a test pack.
  233. //
  234. final Tree t = new Tree(db);
  235. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  236. assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId()
  237. .name());
  238. final File o = new File(new File(
  239. new File(db.getDirectory(), "objects"), "4b"),
  240. "825dc642cb6eb9a060e54bf8d69288fbee4904");
  241. assertFalse("Exists " + o, o.isFile());
  242. }
  243. public void test003_WriteShouldBeEmptyTree() throws IOException {
  244. final Tree t = new Tree(db);
  245. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  246. t.addFile("should-be-empty").setId(emptyId);
  247. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  248. assertEquals("7bb943559a305bdd6bdee2cef6e5df2413c3d30a", t.getId()
  249. .name());
  250. File o;
  251. o = new File(new File(new File(db.getDirectory(), "objects"), "7b"),
  252. "b943559a305bdd6bdee2cef6e5df2413c3d30a");
  253. assertTrue("Exists " + o, o.isFile());
  254. assertTrue("Read-only " + o, !o.canWrite());
  255. o = new File(new File(new File(db.getDirectory(), "objects"), "e6"),
  256. "9de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  257. assertTrue("Exists " + o, o.isFile());
  258. assertTrue("Read-only " + o, !o.canWrite());
  259. }
  260. public void test005_ReadSimpleConfig() {
  261. final Config c = db.getConfig();
  262. assertNotNull(c);
  263. assertEquals("0", c.getString("core", null, "repositoryformatversion"));
  264. assertEquals("0", c.getString("CoRe", null, "REPOSITORYFoRmAtVeRsIoN"));
  265. assertEquals("true", c.getString("core", null, "filemode"));
  266. assertEquals("true", c.getString("cOrE", null, "fIlEModE"));
  267. assertNull(c.getString("notavalue", null, "reallyNotAValue"));
  268. }
  269. public void test006_ReadUglyConfig() throws IOException,
  270. ConfigInvalidException {
  271. final File cfg = new File(db.getDirectory(), "config");
  272. final FileBasedConfig c = new FileBasedConfig(cfg);
  273. final FileWriter pw = new FileWriter(cfg);
  274. final String configStr = " [core];comment\n\tfilemode = yes\n"
  275. + "[user]\n"
  276. + " email = A U Thor <thor@example.com> # Just an example...\n"
  277. + " name = \"A Thor \\\\ \\\"\\t \"\n"
  278. + " defaultCheckInComment = a many line\\n\\\ncomment\\n\\\n"
  279. + " to test\n";
  280. pw.write(configStr);
  281. pw.close();
  282. c.load();
  283. assertEquals("yes", c.getString("core", null, "filemode"));
  284. assertEquals("A U Thor <thor@example.com>", c
  285. .getString("user", null, "email"));
  286. assertEquals("A Thor \\ \"\t ", c.getString("user", null, "name"));
  287. assertEquals("a many line\ncomment\n to test", c.getString("user",
  288. null, "defaultCheckInComment"));
  289. c.save();
  290. final FileReader fr = new FileReader(cfg);
  291. final char[] cbuf = new char[configStr.length()];
  292. fr.read(cbuf);
  293. fr.close();
  294. assertEquals(configStr, new String(cbuf));
  295. }
  296. public void test007_Open() throws IOException {
  297. final FileRepository db2 = new FileRepository(db.getDirectory());
  298. assertEquals(db.getDirectory(), db2.getDirectory());
  299. assertEquals(db.getObjectsDirectory(), db2.getObjectsDirectory());
  300. assertNotSame(db.getConfig(), db2.getConfig());
  301. }
  302. public void test008_FailOnWrongVersion() throws IOException {
  303. final File cfg = new File(db.getDirectory(), "config");
  304. final FileWriter pw = new FileWriter(cfg);
  305. final String badvers = "ihopethisisneveraversion";
  306. final String configStr = "[core]\n" + "\trepositoryFormatVersion="
  307. + badvers + "\n";
  308. pw.write(configStr);
  309. pw.close();
  310. try {
  311. new FileRepository(db.getDirectory());
  312. fail("incorrectly opened a bad repository");
  313. } catch (IOException ioe) {
  314. assertTrue(ioe.getMessage().indexOf("format") > 0);
  315. assertTrue(ioe.getMessage().indexOf(badvers) > 0);
  316. }
  317. }
  318. public void test009_CreateCommitOldFormat() throws IOException {
  319. final Tree t = new Tree(db);
  320. final FileTreeEntry f = t.addFile("i-am-a-file");
  321. writeTrashFile(f.getName(), "and this is the data in me\n");
  322. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  323. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  324. t.getTreeId());
  325. final Commit c = new Commit(db);
  326. c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  327. c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  328. c.setMessage("A Commit\n");
  329. c.setTree(t);
  330. assertEquals(t.getTreeId(), c.getTreeId());
  331. c.commit();
  332. final ObjectId cmtid = ObjectId.fromString(
  333. "803aec4aba175e8ab1d666873c984c0308179099");
  334. assertEquals(cmtid, c.getCommitId());
  335. // Verify the commit we just wrote is in the correct format.
  336. final XInputStream xis = new XInputStream(new FileInputStream(db
  337. .toFile(cmtid)));
  338. try {
  339. assertEquals(0x78, xis.readUInt8());
  340. assertEquals(0x9c, xis.readUInt8());
  341. assertTrue(0x789c % 31 == 0);
  342. } finally {
  343. xis.close();
  344. }
  345. // Verify we can read it.
  346. final Commit c2 = db.mapCommit(cmtid);
  347. assertNotNull(c2);
  348. assertEquals(c.getMessage(), c2.getMessage());
  349. assertEquals(c.getTreeId(), c2.getTreeId());
  350. assertEquals(c.getAuthor(), c2.getAuthor());
  351. assertEquals(c.getCommitter(), c2.getCommitter());
  352. }
  353. public void test012_SubtreeExternalSorting() throws IOException {
  354. final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]);
  355. final Tree t = new Tree(db);
  356. final FileTreeEntry e0 = t.addFile("a-");
  357. final FileTreeEntry e1 = t.addFile("a-b");
  358. final FileTreeEntry e2 = t.addFile("a/b");
  359. final FileTreeEntry e3 = t.addFile("a=");
  360. final FileTreeEntry e4 = t.addFile("a=b");
  361. e0.setId(emptyBlob);
  362. e1.setId(emptyBlob);
  363. e2.setId(emptyBlob);
  364. e3.setId(emptyBlob);
  365. e4.setId(emptyBlob);
  366. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  367. assertEquals(ObjectId.fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
  368. t.getId());
  369. }
  370. public void test020_createBlobTag() throws IOException {
  371. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  372. final Tag t = new Tag(db);
  373. t.setObjId(emptyId);
  374. t.setType("blob");
  375. t.setTag("test020");
  376. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  377. t.setMessage("test020 tagged\n");
  378. t.tag();
  379. assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t.getTagId().name());
  380. Tag mapTag = db.mapTag("test020");
  381. assertEquals("blob", mapTag.getType());
  382. assertEquals("test020 tagged\n", mapTag.getMessage());
  383. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  384. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  385. }
  386. public void test020b_createBlobPlainTag() throws IOException {
  387. test020_createBlobTag();
  388. Tag t = new Tag(db);
  389. t.setTag("test020b");
  390. t.setObjId(ObjectId.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
  391. t.tag();
  392. Tag mapTag = db.mapTag("test020b");
  393. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  394. // We do not repeat the plain tag test for other object types
  395. }
  396. public void test021_createTreeTag() throws IOException {
  397. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  398. final Tree almostEmptyTree = new Tree(db);
  399. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  400. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  401. final Tag t = new Tag(db);
  402. t.setObjId(almostEmptyTreeId);
  403. t.setType("tree");
  404. t.setTag("test021");
  405. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  406. t.setMessage("test021 tagged\n");
  407. t.tag();
  408. assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t.getTagId().name());
  409. Tag mapTag = db.mapTag("test021");
  410. assertEquals("tree", mapTag.getType());
  411. assertEquals("test021 tagged\n", mapTag.getMessage());
  412. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  413. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag.getObjId().name());
  414. }
  415. public void test022_createCommitTag() throws IOException {
  416. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  417. final Tree almostEmptyTree = new Tree(db);
  418. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  419. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  420. final Commit almostEmptyCommit = new Commit(db);
  421. almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L, -2 * 60)); // not exactly the same
  422. almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L, -2 * 60));
  423. almostEmptyCommit.setMessage("test022\n");
  424. almostEmptyCommit.setTreeId(almostEmptyTreeId);
  425. ObjectId almostEmptyCommitId = new ObjectWriter(db).writeCommit(almostEmptyCommit);
  426. final Tag t = new Tag(db);
  427. t.setObjId(almostEmptyCommitId);
  428. t.setType("commit");
  429. t.setTag("test022");
  430. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  431. t.setMessage("test022 tagged\n");
  432. t.tag();
  433. assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t.getTagId().name());
  434. Tag mapTag = db.mapTag("test022");
  435. assertEquals("commit", mapTag.getType());
  436. assertEquals("test022 tagged\n", mapTag.getMessage());
  437. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  438. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().name());
  439. }
  440. public void test023_createCommitNonAnullii() throws IOException {
  441. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  442. final Tree almostEmptyTree = new Tree(db);
  443. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  444. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  445. Commit commit = new Commit(db);
  446. commit.setTreeId(almostEmptyTreeId);
  447. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  448. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  449. commit.setEncoding("UTF-8");
  450. commit.setMessage("\u00dcbergeeks");
  451. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  452. assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
  453. Commit loadedCommit = db.mapCommit(cid);
  454. assertNotSame(loadedCommit, commit);
  455. assertEquals(commit.getMessage(), loadedCommit.getMessage());
  456. }
  457. public void test024_createCommitNonAscii() throws IOException {
  458. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  459. final Tree almostEmptyTree = new Tree(db);
  460. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  461. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  462. Commit commit = new Commit(db);
  463. commit.setTreeId(almostEmptyTreeId);
  464. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  465. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  466. commit.setEncoding("ISO-8859-1");
  467. commit.setMessage("\u00dcbergeeks");
  468. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  469. assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
  470. }
  471. public void test025_packedRefs() throws IOException {
  472. test020_createBlobTag();
  473. test021_createTreeTag();
  474. test022_createCommitTag();
  475. if (!new File(db.getDirectory(),"refs/tags/test020").delete()) throw new Error("Cannot delete unpacked tag");
  476. if (!new File(db.getDirectory(),"refs/tags/test021").delete()) throw new Error("Cannot delete unpacked tag");
  477. if (!new File(db.getDirectory(),"refs/tags/test022").delete()) throw new Error("Cannot delete unpacked tag");
  478. // We cannot resolve it now, since we have no ref
  479. Tag mapTag20missing = db.mapTag("test020");
  480. assertNull(mapTag20missing);
  481. // Construct packed refs file
  482. PrintWriter w = new PrintWriter(new FileWriter(new File(db.getDirectory(), "packed-refs")));
  483. w.println("# packed-refs with: peeled");
  484. w.println("6759556b09fbb4fd8ae5e315134481cc25d46954 refs/tags/test020");
  485. w.println("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  486. w.println("b0517bc8dbe2096b419d42424cd7030733f4abe5 refs/tags/test021");
  487. w.println("^417c01c8795a35b8e835113a85a5c0c1c77f67fb");
  488. w.println("0ce2ebdb36076ef0b38adbe077a07d43b43e3807 refs/tags/test022");
  489. w.println("^b5d3b45a96b340441f5abb9080411705c51cc86c");
  490. w.close();
  491. ((RefDirectory)db.getRefDatabase()).rescan();
  492. Tag mapTag20 = db.mapTag("test020");
  493. assertNotNull("have tag test020", mapTag20);
  494. assertEquals("blob", mapTag20.getType());
  495. assertEquals("test020 tagged\n", mapTag20.getMessage());
  496. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag20.getAuthor());
  497. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag20.getObjId().name());
  498. Tag mapTag21 = db.mapTag("test021");
  499. assertEquals("tree", mapTag21.getType());
  500. assertEquals("test021 tagged\n", mapTag21.getMessage());
  501. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag21.getAuthor());
  502. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag21.getObjId().name());
  503. Tag mapTag22 = db.mapTag("test022");
  504. assertEquals("commit", mapTag22.getType());
  505. assertEquals("test022 tagged\n", mapTag22.getMessage());
  506. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag22.getAuthor());
  507. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag22.getObjId().name());
  508. }
  509. public void test025_computeSha1NoStore() throws IOException {
  510. byte[] data = "test025 some data, more than 16 bytes to get good coverage"
  511. .getBytes("ISO-8859-1");
  512. // TODO: but we do not test legacy header writing
  513. final ObjectId id = new ObjectWriter(db).computeBlobSha1(data.length,
  514. new ByteArrayInputStream(data));
  515. assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
  516. }
  517. public void test026_CreateCommitMultipleparents() throws IOException {
  518. final Tree t = new Tree(db);
  519. final FileTreeEntry f = t.addFile("i-am-a-file");
  520. writeTrashFile(f.getName(), "and this is the data in me\n");
  521. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  522. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  523. t.getTreeId());
  524. final Commit c1 = new Commit(db);
  525. c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  526. c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  527. c1.setMessage("A Commit\n");
  528. c1.setTree(t);
  529. assertEquals(t.getTreeId(), c1.getTreeId());
  530. c1.commit();
  531. final ObjectId cmtid1 = ObjectId.fromString(
  532. "803aec4aba175e8ab1d666873c984c0308179099");
  533. assertEquals(cmtid1, c1.getCommitId());
  534. final Commit c2 = new Commit(db);
  535. c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  536. c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  537. c2.setMessage("A Commit 2\n");
  538. c2.setTree(t);
  539. assertEquals(t.getTreeId(), c2.getTreeId());
  540. c2.setParentIds(new ObjectId[] { c1.getCommitId() } );
  541. c2.commit();
  542. final ObjectId cmtid2 = ObjectId.fromString(
  543. "95d068687c91c5c044fb8c77c5154d5247901553");
  544. assertEquals(cmtid2, c2.getCommitId());
  545. Commit rm2 = db.mapCommit(cmtid2);
  546. assertNotSame(c2, rm2); // assert the parsed objects is not from the cache
  547. assertEquals(c2.getAuthor(), rm2.getAuthor());
  548. assertEquals(c2.getCommitId(), rm2.getCommitId());
  549. assertEquals(c2.getMessage(), rm2.getMessage());
  550. assertEquals(c2.getTree().getTreeId(), rm2.getTree().getTreeId());
  551. assertEquals(1, rm2.getParentIds().length);
  552. assertEquals(c1.getCommitId(), rm2.getParentIds()[0]);
  553. final Commit c3 = new Commit(db);
  554. c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  555. c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  556. c3.setMessage("A Commit 3\n");
  557. c3.setTree(t);
  558. assertEquals(t.getTreeId(), c3.getTreeId());
  559. c3.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId() });
  560. c3.commit();
  561. final ObjectId cmtid3 = ObjectId.fromString(
  562. "ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
  563. assertEquals(cmtid3, c3.getCommitId());
  564. Commit rm3 = db.mapCommit(cmtid3);
  565. assertNotSame(c3, rm3); // assert the parsed objects is not from the cache
  566. assertEquals(c3.getAuthor(), rm3.getAuthor());
  567. assertEquals(c3.getCommitId(), rm3.getCommitId());
  568. assertEquals(c3.getMessage(), rm3.getMessage());
  569. assertEquals(c3.getTree().getTreeId(), rm3.getTree().getTreeId());
  570. assertEquals(2, rm3.getParentIds().length);
  571. assertEquals(c1.getCommitId(), rm3.getParentIds()[0]);
  572. assertEquals(c2.getCommitId(), rm3.getParentIds()[1]);
  573. final Commit c4 = new Commit(db);
  574. c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  575. c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  576. c4.setMessage("A Commit 4\n");
  577. c4.setTree(t);
  578. assertEquals(t.getTreeId(), c3.getTreeId());
  579. c4.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId(), c3.getCommitId() });
  580. c4.commit();
  581. final ObjectId cmtid4 = ObjectId.fromString(
  582. "d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
  583. assertEquals(cmtid4, c4.getCommitId());
  584. Commit rm4 = db.mapCommit(cmtid4);
  585. assertNotSame(c4, rm3); // assert the parsed objects is not from the cache
  586. assertEquals(c4.getAuthor(), rm4.getAuthor());
  587. assertEquals(c4.getCommitId(), rm4.getCommitId());
  588. assertEquals(c4.getMessage(), rm4.getMessage());
  589. assertEquals(c4.getTree().getTreeId(), rm4.getTree().getTreeId());
  590. assertEquals(3, rm4.getParentIds().length);
  591. assertEquals(c1.getCommitId(), rm4.getParentIds()[0]);
  592. assertEquals(c2.getCommitId(), rm4.getParentIds()[1]);
  593. assertEquals(c3.getCommitId(), rm4.getParentIds()[2]);
  594. }
  595. public void test027_UnpackedRefHigherPriorityThanPacked() throws IOException {
  596. PrintWriter writer = new PrintWriter(new FileWriter(new File(db.getDirectory(), "refs/heads/a")));
  597. String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
  598. writer.print(unpackedId);
  599. writer.print('\n');
  600. writer.close();
  601. ObjectId resolved = db.resolve("refs/heads/a");
  602. assertEquals(unpackedId, resolved.name());
  603. }
  604. public void test028_LockPackedRef() throws IOException {
  605. writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
  606. writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
  607. BUG_WorkAroundRacyGitIssues("packed-refs");
  608. BUG_WorkAroundRacyGitIssues("HEAD");
  609. ObjectId resolve = db.resolve("HEAD");
  610. assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
  611. RefUpdate lockRef = db.updateRef("HEAD");
  612. ObjectId newId = ObjectId.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
  613. lockRef.setNewObjectId(newId);
  614. assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
  615. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  616. assertEquals(newId, db.resolve("refs/heads/foobar"));
  617. // Again. The ref already exists
  618. RefUpdate lockRef2 = db.updateRef("HEAD");
  619. ObjectId newId2 = ObjectId.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
  620. lockRef2.setNewObjectId(newId2);
  621. assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
  622. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  623. assertEquals(newId2, db.resolve("refs/heads/foobar"));
  624. }
  625. public void test029_mapObject() throws IOException {
  626. assertEquals(new byte[0].getClass(), db.mapObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), null).getClass());
  627. assertEquals(Commit.class, db.mapObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"), null).getClass());
  628. assertEquals(Tree.class, db.mapObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"), null).getClass());
  629. assertEquals(Tag.class, db.mapObject(ObjectId.fromString("17768080a2318cd89bba4c8b87834401e2095703"), null).getClass());
  630. }
  631. public void test30_stripWorkDir() {
  632. File relCwd = new File(".");
  633. File absCwd = relCwd.getAbsoluteFile();
  634. File absBase = new File(new File(absCwd, "repo"), "workdir");
  635. File relBase = new File(new File(relCwd, "repo"), "workdir");
  636. assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
  637. File relBaseFile = new File(new File(relBase, "other"), "module.c");
  638. File absBaseFile = new File(new File(absBase, "other"), "module.c");
  639. assertEquals("other/module.c", Repository.stripWorkDir(relBase, relBaseFile));
  640. assertEquals("other/module.c", Repository.stripWorkDir(relBase, absBaseFile));
  641. assertEquals("other/module.c", Repository.stripWorkDir(absBase, relBaseFile));
  642. assertEquals("other/module.c", Repository.stripWorkDir(absBase, absBaseFile));
  643. File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
  644. File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
  645. assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
  646. assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
  647. assertEquals("", Repository.stripWorkDir(db.getWorkDir(), db.getWorkDir()));
  648. File file = new File(new File(db.getWorkDir(), "subdir"), "File.java");
  649. assertEquals("subdir/File.java", Repository.stripWorkDir(db.getWorkDir(), file));
  650. }
  651. /**
  652. * Kick the timestamp of a local file.
  653. * <p>
  654. * We shouldn't have to make these method calls. The cache is using file
  655. * system timestamps, and on many systems unit tests run faster than the
  656. * modification clock. Dumping the cache after we make an edit behind
  657. * RefDirectory's back allows the tests to pass.
  658. *
  659. * @param name
  660. * the file in the repository to force a time change on.
  661. */
  662. private void BUG_WorkAroundRacyGitIssues(String name) {
  663. File path = new File(db.getDirectory(), name);
  664. long old = path.lastModified();
  665. long set = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  666. path.setLastModified(set);
  667. assertTrue("time changed", old != path.lastModified());
  668. }
  669. }