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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2007-2009, 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, ".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 Repository(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 Repository(new File(repo1Parent, ".git"));
  94. repo1initial.create();
  95. repo1initial.close();
  96. File theDir = new File(repo1Parent, ".git");
  97. Repository r = new Repository(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 Repository(new File(repo1Parent, ".git"));
  112. repo1initial.create();
  113. repo1initial.close();
  114. File theDir = new File(repo1Parent, ".git");
  115. Repository r = new Repository(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 Repository(new File(repo1Parent, ".git"));
  130. repo1initial.create();
  131. repo1initial.close();
  132. File theDir = new File(repo1Parent, ".git");
  133. Repository r = new Repository(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. Repository repo1initial = new Repository(new File(repo1Parent, ".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, ".git");
  156. Repository r = new Repository(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. Repository repo1initial = new Repository(new File(repo1Parent, ".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, ".git");
  179. Repository r = new Repository(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 Repository(new File(repo1Parent, ".git"));
  198. repo1initial.create();
  199. repo1initial.close();
  200. File theDir = new File(repo1Parent, ".git");
  201. Repository r = new Repository(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 RepositoryConfig 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 RepositoryConfig c = db.getConfig();
  272. final File cfg = new File(db.getDirectory(), "config");
  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 Repository db2 = new Repository(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 Repository(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. ConfigInvalidException {
  320. writeTrashFile(".git/config", "[core]\n" + "legacyHeaders=1\n");
  321. db.getConfig().load();
  322. final Tree t = new Tree(db);
  323. final FileTreeEntry f = t.addFile("i-am-a-file");
  324. writeTrashFile(f.getName(), "and this is the data in me\n");
  325. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  326. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  327. t.getTreeId());
  328. final Commit c = new Commit(db);
  329. c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  330. c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  331. c.setMessage("A Commit\n");
  332. c.setTree(t);
  333. assertEquals(t.getTreeId(), c.getTreeId());
  334. c.commit();
  335. final ObjectId cmtid = ObjectId.fromString(
  336. "803aec4aba175e8ab1d666873c984c0308179099");
  337. assertEquals(cmtid, c.getCommitId());
  338. // Verify the commit we just wrote is in the correct format.
  339. final XInputStream xis = new XInputStream(new FileInputStream(db
  340. .toFile(cmtid)));
  341. try {
  342. assertEquals(0x78, xis.readUInt8());
  343. assertEquals(0x9c, xis.readUInt8());
  344. assertTrue(0x789c % 31 == 0);
  345. } finally {
  346. xis.close();
  347. }
  348. // Verify we can read it.
  349. final Commit c2 = db.mapCommit(cmtid);
  350. assertNotNull(c2);
  351. assertEquals(c.getMessage(), c2.getMessage());
  352. assertEquals(c.getTreeId(), c2.getTreeId());
  353. assertEquals(c.getAuthor(), c2.getAuthor());
  354. assertEquals(c.getCommitter(), c2.getCommitter());
  355. }
  356. public void test012_SubtreeExternalSorting() throws IOException {
  357. final ObjectId emptyBlob = new ObjectWriter(db).writeBlob(new byte[0]);
  358. final Tree t = new Tree(db);
  359. final FileTreeEntry e0 = t.addFile("a-");
  360. final FileTreeEntry e1 = t.addFile("a-b");
  361. final FileTreeEntry e2 = t.addFile("a/b");
  362. final FileTreeEntry e3 = t.addFile("a=");
  363. final FileTreeEntry e4 = t.addFile("a=b");
  364. e0.setId(emptyBlob);
  365. e1.setId(emptyBlob);
  366. e2.setId(emptyBlob);
  367. e3.setId(emptyBlob);
  368. e4.setId(emptyBlob);
  369. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  370. assertEquals(ObjectId.fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
  371. t.getId());
  372. }
  373. public void test020_createBlobTag() throws IOException {
  374. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  375. final Tag t = new Tag(db);
  376. t.setObjId(emptyId);
  377. t.setType("blob");
  378. t.setTag("test020");
  379. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  380. t.setMessage("test020 tagged\n");
  381. t.tag();
  382. assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", t.getTagId().name());
  383. Tag mapTag = db.mapTag("test020");
  384. assertEquals("blob", mapTag.getType());
  385. assertEquals("test020 tagged\n", mapTag.getMessage());
  386. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  387. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  388. }
  389. public void test020b_createBlobPlainTag() throws IOException {
  390. test020_createBlobTag();
  391. Tag t = new Tag(db);
  392. t.setTag("test020b");
  393. t.setObjId(ObjectId.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
  394. t.tag();
  395. Tag mapTag = db.mapTag("test020b");
  396. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().name());
  397. // We do not repeat the plain tag test for other object types
  398. }
  399. public void test021_createTreeTag() throws IOException {
  400. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  401. final Tree almostEmptyTree = new Tree(db);
  402. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  403. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  404. final Tag t = new Tag(db);
  405. t.setObjId(almostEmptyTreeId);
  406. t.setType("tree");
  407. t.setTag("test021");
  408. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  409. t.setMessage("test021 tagged\n");
  410. t.tag();
  411. assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", t.getTagId().name());
  412. Tag mapTag = db.mapTag("test021");
  413. assertEquals("tree", mapTag.getType());
  414. assertEquals("test021 tagged\n", mapTag.getMessage());
  415. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  416. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag.getObjId().name());
  417. }
  418. public void test022_createCommitTag() throws IOException {
  419. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  420. final Tree almostEmptyTree = new Tree(db);
  421. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  422. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  423. final Commit almostEmptyCommit = new Commit(db);
  424. almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L, -2 * 60)); // not exactly the same
  425. almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L, -2 * 60));
  426. almostEmptyCommit.setMessage("test022\n");
  427. almostEmptyCommit.setTreeId(almostEmptyTreeId);
  428. ObjectId almostEmptyCommitId = new ObjectWriter(db).writeCommit(almostEmptyCommit);
  429. final Tag t = new Tag(db);
  430. t.setObjId(almostEmptyCommitId);
  431. t.setType("commit");
  432. t.setTag("test022");
  433. t.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  434. t.setMessage("test022 tagged\n");
  435. t.tag();
  436. assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", t.getTagId().name());
  437. Tag mapTag = db.mapTag("test022");
  438. assertEquals("commit", mapTag.getType());
  439. assertEquals("test022 tagged\n", mapTag.getMessage());
  440. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag.getAuthor());
  441. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag.getObjId().name());
  442. }
  443. public void test023_createCommitNonAnullii() throws IOException {
  444. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  445. final Tree almostEmptyTree = new Tree(db);
  446. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  447. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  448. Commit commit = new Commit(db);
  449. commit.setTreeId(almostEmptyTreeId);
  450. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  451. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  452. commit.setEncoding("UTF-8");
  453. commit.setMessage("\u00dcbergeeks");
  454. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  455. assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
  456. Commit loadedCommit = db.mapCommit(cid);
  457. assertNotSame(loadedCommit, commit);
  458. assertEquals(commit.getMessage(), loadedCommit.getMessage());
  459. }
  460. public void test024_createCommitNonAscii() throws IOException {
  461. final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
  462. final Tree almostEmptyTree = new Tree(db);
  463. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId, "empty".getBytes(), false));
  464. final ObjectId almostEmptyTreeId = new ObjectWriter(db).writeTree(almostEmptyTree);
  465. Commit commit = new Commit(db);
  466. commit.setTreeId(almostEmptyTreeId);
  467. commit.setAuthor(new PersonIdent("Joe H\u00e4cker","joe@example.com",4294967295000L,60));
  468. commit.setCommitter(new PersonIdent("Joe Hacker","joe2@example.com",4294967295000L,60));
  469. commit.setEncoding("ISO-8859-1");
  470. commit.setMessage("\u00dcbergeeks");
  471. ObjectId cid = new ObjectWriter(db).writeCommit(commit);
  472. assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
  473. }
  474. public void test025_packedRefs() throws IOException {
  475. test020_createBlobTag();
  476. test021_createTreeTag();
  477. test022_createCommitTag();
  478. if (!new File(db.getDirectory(),"refs/tags/test020").delete()) throw new Error("Cannot delete unpacked tag");
  479. if (!new File(db.getDirectory(),"refs/tags/test021").delete()) throw new Error("Cannot delete unpacked tag");
  480. if (!new File(db.getDirectory(),"refs/tags/test022").delete()) throw new Error("Cannot delete unpacked tag");
  481. // We cannot resolve it now, since we have no ref
  482. Tag mapTag20missing = db.mapTag("test020");
  483. assertNull(mapTag20missing);
  484. // Construct packed refs file
  485. PrintWriter w = new PrintWriter(new FileWriter(new File(db.getDirectory(), "packed-refs")));
  486. w.println("# packed-refs with: peeled");
  487. w.println("6759556b09fbb4fd8ae5e315134481cc25d46954 refs/tags/test020");
  488. w.println("^e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  489. w.println("b0517bc8dbe2096b419d42424cd7030733f4abe5 refs/tags/test021");
  490. w.println("^417c01c8795a35b8e835113a85a5c0c1c77f67fb");
  491. w.println("0ce2ebdb36076ef0b38adbe077a07d43b43e3807 refs/tags/test022");
  492. w.println("^b5d3b45a96b340441f5abb9080411705c51cc86c");
  493. w.close();
  494. ((RefDirectory)db.getRefDatabase()).rescan();
  495. Tag mapTag20 = db.mapTag("test020");
  496. assertNotNull("have tag test020", mapTag20);
  497. assertEquals("blob", mapTag20.getType());
  498. assertEquals("test020 tagged\n", mapTag20.getMessage());
  499. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag20.getAuthor());
  500. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag20.getObjId().name());
  501. Tag mapTag21 = db.mapTag("test021");
  502. assertEquals("tree", mapTag21.getType());
  503. assertEquals("test021 tagged\n", mapTag21.getMessage());
  504. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag21.getAuthor());
  505. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag21.getObjId().name());
  506. Tag mapTag22 = db.mapTag("test022");
  507. assertEquals("commit", mapTag22.getType());
  508. assertEquals("test022 tagged\n", mapTag22.getMessage());
  509. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag22.getAuthor());
  510. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag22.getObjId().name());
  511. }
  512. public void test025_computeSha1NoStore() throws IOException {
  513. byte[] data = "test025 some data, more than 16 bytes to get good coverage"
  514. .getBytes("ISO-8859-1");
  515. // TODO: but we do not test legacy header writing
  516. final ObjectId id = new ObjectWriter(db).computeBlobSha1(data.length,
  517. new ByteArrayInputStream(data));
  518. assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
  519. }
  520. public void test026_CreateCommitMultipleparents() throws IOException {
  521. final Tree t = new Tree(db);
  522. final FileTreeEntry f = t.addFile("i-am-a-file");
  523. writeTrashFile(f.getName(), "and this is the data in me\n");
  524. t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY);
  525. assertEquals(ObjectId.fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"),
  526. t.getTreeId());
  527. final Commit c1 = new Commit(db);
  528. c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  529. c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  530. c1.setMessage("A Commit\n");
  531. c1.setTree(t);
  532. assertEquals(t.getTreeId(), c1.getTreeId());
  533. c1.commit();
  534. final ObjectId cmtid1 = ObjectId.fromString(
  535. "803aec4aba175e8ab1d666873c984c0308179099");
  536. assertEquals(cmtid1, c1.getCommitId());
  537. final Commit c2 = new Commit(db);
  538. c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  539. c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  540. c2.setMessage("A Commit 2\n");
  541. c2.setTree(t);
  542. assertEquals(t.getTreeId(), c2.getTreeId());
  543. c2.setParentIds(new ObjectId[] { c1.getCommitId() } );
  544. c2.commit();
  545. final ObjectId cmtid2 = ObjectId.fromString(
  546. "95d068687c91c5c044fb8c77c5154d5247901553");
  547. assertEquals(cmtid2, c2.getCommitId());
  548. Commit rm2 = db.mapCommit(cmtid2);
  549. assertNotSame(c2, rm2); // assert the parsed objects is not from the cache
  550. assertEquals(c2.getAuthor(), rm2.getAuthor());
  551. assertEquals(c2.getCommitId(), rm2.getCommitId());
  552. assertEquals(c2.getMessage(), rm2.getMessage());
  553. assertEquals(c2.getTree().getTreeId(), rm2.getTree().getTreeId());
  554. assertEquals(1, rm2.getParentIds().length);
  555. assertEquals(c1.getCommitId(), rm2.getParentIds()[0]);
  556. final Commit c3 = new Commit(db);
  557. c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  558. c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  559. c3.setMessage("A Commit 3\n");
  560. c3.setTree(t);
  561. assertEquals(t.getTreeId(), c3.getTreeId());
  562. c3.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId() });
  563. c3.commit();
  564. final ObjectId cmtid3 = ObjectId.fromString(
  565. "ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
  566. assertEquals(cmtid3, c3.getCommitId());
  567. Commit rm3 = db.mapCommit(cmtid3);
  568. assertNotSame(c3, rm3); // assert the parsed objects is not from the cache
  569. assertEquals(c3.getAuthor(), rm3.getAuthor());
  570. assertEquals(c3.getCommitId(), rm3.getCommitId());
  571. assertEquals(c3.getMessage(), rm3.getMessage());
  572. assertEquals(c3.getTree().getTreeId(), rm3.getTree().getTreeId());
  573. assertEquals(2, rm3.getParentIds().length);
  574. assertEquals(c1.getCommitId(), rm3.getParentIds()[0]);
  575. assertEquals(c2.getCommitId(), rm3.getParentIds()[1]);
  576. final Commit c4 = new Commit(db);
  577. c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  578. c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  579. c4.setMessage("A Commit 4\n");
  580. c4.setTree(t);
  581. assertEquals(t.getTreeId(), c3.getTreeId());
  582. c4.setParentIds(new ObjectId[] { c1.getCommitId(), c2.getCommitId(), c3.getCommitId() });
  583. c4.commit();
  584. final ObjectId cmtid4 = ObjectId.fromString(
  585. "d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
  586. assertEquals(cmtid4, c4.getCommitId());
  587. Commit rm4 = db.mapCommit(cmtid4);
  588. assertNotSame(c4, rm3); // assert the parsed objects is not from the cache
  589. assertEquals(c4.getAuthor(), rm4.getAuthor());
  590. assertEquals(c4.getCommitId(), rm4.getCommitId());
  591. assertEquals(c4.getMessage(), rm4.getMessage());
  592. assertEquals(c4.getTree().getTreeId(), rm4.getTree().getTreeId());
  593. assertEquals(3, rm4.getParentIds().length);
  594. assertEquals(c1.getCommitId(), rm4.getParentIds()[0]);
  595. assertEquals(c2.getCommitId(), rm4.getParentIds()[1]);
  596. assertEquals(c3.getCommitId(), rm4.getParentIds()[2]);
  597. }
  598. public void test027_UnpackedRefHigherPriorityThanPacked() throws IOException {
  599. PrintWriter writer = new PrintWriter(new FileWriter(new File(db.getDirectory(), "refs/heads/a")));
  600. String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
  601. writer.print(unpackedId);
  602. writer.print('\n');
  603. writer.close();
  604. ObjectId resolved = db.resolve("refs/heads/a");
  605. assertEquals(unpackedId, resolved.name());
  606. }
  607. public void test028_LockPackedRef() throws IOException {
  608. writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
  609. writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
  610. BUG_WorkAroundRacyGitIssues("packed-refs");
  611. BUG_WorkAroundRacyGitIssues("HEAD");
  612. ObjectId resolve = db.resolve("HEAD");
  613. assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
  614. RefUpdate lockRef = db.updateRef("HEAD");
  615. ObjectId newId = ObjectId.fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
  616. lockRef.setNewObjectId(newId);
  617. assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
  618. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  619. assertEquals(newId, db.resolve("refs/heads/foobar"));
  620. // Again. The ref already exists
  621. RefUpdate lockRef2 = db.updateRef("HEAD");
  622. ObjectId newId2 = ObjectId.fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
  623. lockRef2.setNewObjectId(newId2);
  624. assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
  625. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  626. assertEquals(newId2, db.resolve("refs/heads/foobar"));
  627. }
  628. public void test029_mapObject() throws IOException {
  629. assertEquals(new byte[0].getClass(), db.mapObject(ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"), null).getClass());
  630. assertEquals(Commit.class, db.mapObject(ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"), null).getClass());
  631. assertEquals(Tree.class, db.mapObject(ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"), null).getClass());
  632. assertEquals(Tag.class, db.mapObject(ObjectId.fromString("17768080a2318cd89bba4c8b87834401e2095703"), null).getClass());
  633. }
  634. public void test30_stripWorkDir() {
  635. File relCwd = new File(".");
  636. File absCwd = relCwd.getAbsoluteFile();
  637. File absBase = new File(new File(absCwd, "repo"), "workdir");
  638. File relBase = new File(new File(relCwd, "repo"), "workdir");
  639. assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
  640. File relBaseFile = new File(new File(relBase, "other"), "module.c");
  641. File absBaseFile = new File(new File(absBase, "other"), "module.c");
  642. assertEquals("other/module.c", Repository.stripWorkDir(relBase, relBaseFile));
  643. assertEquals("other/module.c", Repository.stripWorkDir(relBase, absBaseFile));
  644. assertEquals("other/module.c", Repository.stripWorkDir(absBase, relBaseFile));
  645. assertEquals("other/module.c", Repository.stripWorkDir(absBase, absBaseFile));
  646. File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
  647. File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
  648. assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
  649. assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
  650. assertEquals("", Repository.stripWorkDir(db.getWorkDir(), db.getWorkDir()));
  651. File file = new File(new File(db.getWorkDir(), "subdir"), "File.java");
  652. assertEquals("subdir/File.java", Repository.stripWorkDir(db.getWorkDir(), file));
  653. }
  654. /**
  655. * Kick the timestamp of a local file.
  656. * <p>
  657. * We shouldn't have to make these method calls. The cache is using file
  658. * system timestamps, and on many systems unit tests run faster than the
  659. * modification clock. Dumping the cache after we make an edit behind
  660. * RefDirectory's back allows the tests to pass.
  661. *
  662. * @param name
  663. * the file in the repository to force a time change on.
  664. */
  665. private void BUG_WorkAroundRacyGitIssues(String name) {
  666. File path = new File(db.getDirectory(), name);
  667. long old = path.lastModified();
  668. long set = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  669. path.setLastModified(set);
  670. assertTrue("time changed", old != path.lastModified());
  671. }
  672. }