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

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