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_BasicTest.java 30KB

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