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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 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 (IOException ioe) {
  346. assertTrue(ioe.getMessage().indexOf("format") > 0);
  347. assertTrue(ioe.getMessage().indexOf(badvers) > 0);
  348. }
  349. }
  350. @Test
  351. public void test009_CreateCommitOldFormat() throws IOException {
  352. final ObjectId treeId = insertTree(new TreeFormatter());
  353. final CommitBuilder c = new CommitBuilder();
  354. c.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  355. c.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  356. c.setMessage("A Commit\n");
  357. c.setTreeId(treeId);
  358. assertEquals(treeId, c.getTreeId());
  359. ObjectId actid = insertCommit(c);
  360. final ObjectId cmtid = ObjectId
  361. .fromString("9208b2459ea6609a5af68627cc031796d0d9329b");
  362. assertEquals(cmtid, actid);
  363. // Verify the commit we just wrote is in the correct format.
  364. ObjectDatabase odb = db.getObjectDatabase();
  365. assertTrue("is ObjectDirectory", odb instanceof ObjectDirectory);
  366. final XInputStream xis = new XInputStream(new FileInputStream(
  367. ((ObjectDirectory) odb).fileFor(cmtid)));
  368. try {
  369. assertEquals(0x78, xis.readUInt8());
  370. assertEquals(0x9c, xis.readUInt8());
  371. assertTrue(0x789c % 31 == 0);
  372. } finally {
  373. xis.close();
  374. }
  375. // Verify we can read it.
  376. RevCommit c2 = parseCommit(actid);
  377. assertNotNull(c2);
  378. assertEquals(c.getMessage(), c2.getFullMessage());
  379. assertEquals(c.getTreeId(), c2.getTree());
  380. assertEquals(c.getAuthor(), c2.getAuthorIdent());
  381. assertEquals(c.getCommitter(), c2.getCommitterIdent());
  382. }
  383. @Test
  384. public void test012_SubtreeExternalSorting() throws IOException {
  385. final ObjectId emptyBlob = insertEmptyBlob();
  386. final Tree t = new Tree(db);
  387. final FileTreeEntry e0 = t.addFile("a-");
  388. final FileTreeEntry e1 = t.addFile("a-b");
  389. final FileTreeEntry e2 = t.addFile("a/b");
  390. final FileTreeEntry e3 = t.addFile("a=");
  391. final FileTreeEntry e4 = t.addFile("a=b");
  392. e0.setId(emptyBlob);
  393. e1.setId(emptyBlob);
  394. e2.setId(emptyBlob);
  395. e3.setId(emptyBlob);
  396. e4.setId(emptyBlob);
  397. final Tree a = (Tree) t.findTreeMember("a");
  398. a.setId(insertTree(a));
  399. assertEquals(ObjectId
  400. .fromString("b47a8f0a4190f7572e11212769090523e23eb1ea"),
  401. insertTree(t));
  402. }
  403. @Test
  404. public void test020_createBlobTag() throws IOException {
  405. final ObjectId emptyId = insertEmptyBlob();
  406. final TagBuilder t = new TagBuilder();
  407. t.setObjectId(emptyId, Constants.OBJ_BLOB);
  408. t.setTag("test020");
  409. t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
  410. t.setMessage("test020 tagged\n");
  411. ObjectId actid = insertTag(t);
  412. assertEquals("6759556b09fbb4fd8ae5e315134481cc25d46954", actid.name());
  413. RevTag mapTag = parseTag(actid);
  414. assertEquals(Constants.OBJ_BLOB, mapTag.getObject().getType());
  415. assertEquals("test020 tagged\n", mapTag.getFullMessage());
  416. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
  417. .getTaggerIdent());
  418. assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag
  419. .getObject().getId().name());
  420. }
  421. @Test
  422. public void test021_createTreeTag() throws IOException {
  423. final ObjectId emptyId = insertEmptyBlob();
  424. final Tree almostEmptyTree = new Tree(db);
  425. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
  426. "empty".getBytes(), false));
  427. final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
  428. final TagBuilder t = new TagBuilder();
  429. t.setObjectId(almostEmptyTreeId, Constants.OBJ_TREE);
  430. t.setTag("test021");
  431. t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
  432. t.setMessage("test021 tagged\n");
  433. ObjectId actid = insertTag(t);
  434. assertEquals("b0517bc8dbe2096b419d42424cd7030733f4abe5", actid.name());
  435. RevTag mapTag = parseTag(actid);
  436. assertEquals(Constants.OBJ_TREE, mapTag.getObject().getType());
  437. assertEquals("test021 tagged\n", mapTag.getFullMessage());
  438. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
  439. .getTaggerIdent());
  440. assertEquals("417c01c8795a35b8e835113a85a5c0c1c77f67fb", mapTag
  441. .getObject().getId().name());
  442. }
  443. @Test
  444. public void test022_createCommitTag() throws IOException {
  445. final ObjectId emptyId = insertEmptyBlob();
  446. final Tree almostEmptyTree = new Tree(db);
  447. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
  448. "empty".getBytes(), false));
  449. final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
  450. final CommitBuilder almostEmptyCommit = new CommitBuilder();
  451. almostEmptyCommit.setAuthor(new PersonIdent(author, 1154236443000L,
  452. -2 * 60)); // not exactly the same
  453. almostEmptyCommit.setCommitter(new PersonIdent(author, 1154236443000L,
  454. -2 * 60));
  455. almostEmptyCommit.setMessage("test022\n");
  456. almostEmptyCommit.setTreeId(almostEmptyTreeId);
  457. ObjectId almostEmptyCommitId = insertCommit(almostEmptyCommit);
  458. final TagBuilder t = new TagBuilder();
  459. t.setObjectId(almostEmptyCommitId, Constants.OBJ_COMMIT);
  460. t.setTag("test022");
  461. t.setTagger(new PersonIdent(author, 1154236443000L, -4 * 60));
  462. t.setMessage("test022 tagged\n");
  463. ObjectId actid = insertTag(t);
  464. assertEquals("0ce2ebdb36076ef0b38adbe077a07d43b43e3807", actid.name());
  465. RevTag mapTag = parseTag(actid);
  466. assertEquals(Constants.OBJ_COMMIT, mapTag.getObject().getType());
  467. assertEquals("test022 tagged\n", mapTag.getFullMessage());
  468. assertEquals(new PersonIdent(author, 1154236443000L, -4 * 60), mapTag
  469. .getTaggerIdent());
  470. assertEquals("b5d3b45a96b340441f5abb9080411705c51cc86c", mapTag
  471. .getObject().getId().name());
  472. }
  473. @Test
  474. public void test023_createCommitNonAnullii() throws IOException {
  475. final ObjectId emptyId = insertEmptyBlob();
  476. final Tree almostEmptyTree = new Tree(db);
  477. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
  478. "empty".getBytes(), false));
  479. final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
  480. CommitBuilder commit = new CommitBuilder();
  481. commit.setTreeId(almostEmptyTreeId);
  482. commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
  483. 4294967295000L, 60));
  484. commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
  485. 4294967295000L, 60));
  486. commit.setEncoding("UTF-8");
  487. commit.setMessage("\u00dcbergeeks");
  488. ObjectId cid = insertCommit(commit);
  489. assertEquals("4680908112778718f37e686cbebcc912730b3154", cid.name());
  490. RevCommit loadedCommit = parseCommit(cid);
  491. assertEquals(commit.getMessage(), loadedCommit.getFullMessage());
  492. }
  493. @Test
  494. public void test024_createCommitNonAscii() throws IOException {
  495. final ObjectId emptyId = insertEmptyBlob();
  496. final Tree almostEmptyTree = new Tree(db);
  497. almostEmptyTree.addEntry(new FileTreeEntry(almostEmptyTree, emptyId,
  498. "empty".getBytes(), false));
  499. final ObjectId almostEmptyTreeId = insertTree(almostEmptyTree);
  500. CommitBuilder commit = new CommitBuilder();
  501. commit.setTreeId(almostEmptyTreeId);
  502. commit.setAuthor(new PersonIdent("Joe H\u00e4cker", "joe@example.com",
  503. 4294967295000L, 60));
  504. commit.setCommitter(new PersonIdent("Joe Hacker", "joe2@example.com",
  505. 4294967295000L, 60));
  506. commit.setEncoding("ISO-8859-1");
  507. commit.setMessage("\u00dcbergeeks");
  508. ObjectId cid = insertCommit(commit);
  509. assertEquals("2979b39d385014b33287054b87f77bcb3ecb5ebf", cid.name());
  510. }
  511. @Test
  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. final ObjectId id = new ObjectInserter.Formatter().idFor(
  516. Constants.OBJ_BLOB, data);
  517. assertEquals("4f561df5ecf0dfbd53a0dc0f37262fef075d9dde", id.name());
  518. }
  519. @Test
  520. public void test026_CreateCommitMultipleparents() throws IOException {
  521. final ObjectId treeId;
  522. final ObjectInserter oi = db.newObjectInserter();
  523. try {
  524. final ObjectId blobId = oi.insert(Constants.OBJ_BLOB,
  525. "and this is the data in me\n".getBytes(Constants.CHARSET
  526. .name()));
  527. TreeFormatter fmt = new TreeFormatter();
  528. fmt.append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
  529. treeId = oi.insert(fmt);
  530. oi.flush();
  531. } finally {
  532. oi.release();
  533. }
  534. assertEquals(ObjectId
  535. .fromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"), treeId);
  536. final CommitBuilder c1 = new CommitBuilder();
  537. c1.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  538. c1.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  539. c1.setMessage("A Commit\n");
  540. c1.setTreeId(treeId);
  541. assertEquals(treeId, c1.getTreeId());
  542. ObjectId actid1 = insertCommit(c1);
  543. final ObjectId cmtid1 = ObjectId
  544. .fromString("803aec4aba175e8ab1d666873c984c0308179099");
  545. assertEquals(cmtid1, actid1);
  546. final CommitBuilder c2 = new CommitBuilder();
  547. c2.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  548. c2.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  549. c2.setMessage("A Commit 2\n");
  550. c2.setTreeId(treeId);
  551. assertEquals(treeId, c2.getTreeId());
  552. c2.setParentIds(actid1);
  553. ObjectId actid2 = insertCommit(c2);
  554. final ObjectId cmtid2 = ObjectId
  555. .fromString("95d068687c91c5c044fb8c77c5154d5247901553");
  556. assertEquals(cmtid2, actid2);
  557. RevCommit rm2 = parseCommit(cmtid2);
  558. assertNotSame(c2, rm2); // assert the parsed objects is not from the
  559. // cache
  560. assertEquals(c2.getAuthor(), rm2.getAuthorIdent());
  561. assertEquals(actid2, rm2.getId());
  562. assertEquals(c2.getMessage(), rm2.getFullMessage());
  563. assertEquals(c2.getTreeId(), rm2.getTree().getId());
  564. assertEquals(1, rm2.getParentCount());
  565. assertEquals(actid1, rm2.getParent(0));
  566. final CommitBuilder c3 = new CommitBuilder();
  567. c3.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  568. c3.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  569. c3.setMessage("A Commit 3\n");
  570. c3.setTreeId(treeId);
  571. assertEquals(treeId, c3.getTreeId());
  572. c3.setParentIds(actid1, actid2);
  573. ObjectId actid3 = insertCommit(c3);
  574. final ObjectId cmtid3 = ObjectId
  575. .fromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
  576. assertEquals(cmtid3, actid3);
  577. RevCommit rm3 = parseCommit(cmtid3);
  578. assertNotSame(c3, rm3); // assert the parsed objects is not from the
  579. // cache
  580. assertEquals(c3.getAuthor(), rm3.getAuthorIdent());
  581. assertEquals(actid3, rm3.getId());
  582. assertEquals(c3.getMessage(), rm3.getFullMessage());
  583. assertEquals(c3.getTreeId(), rm3.getTree().getId());
  584. assertEquals(2, rm3.getParentCount());
  585. assertEquals(actid1, rm3.getParent(0));
  586. assertEquals(actid2, rm3.getParent(1));
  587. final CommitBuilder c4 = new CommitBuilder();
  588. c4.setAuthor(new PersonIdent(author, 1154236443000L, -4 * 60));
  589. c4.setCommitter(new PersonIdent(committer, 1154236443000L, -4 * 60));
  590. c4.setMessage("A Commit 4\n");
  591. c4.setTreeId(treeId);
  592. assertEquals(treeId, c3.getTreeId());
  593. c4.setParentIds(actid1, actid2, actid3);
  594. ObjectId actid4 = insertCommit(c4);
  595. final ObjectId cmtid4 = ObjectId
  596. .fromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
  597. assertEquals(cmtid4, actid4);
  598. RevCommit rm4 = parseCommit(cmtid4);
  599. assertNotSame(c4, rm3); // assert the parsed objects is not from the
  600. // cache
  601. assertEquals(c4.getAuthor(), rm4.getAuthorIdent());
  602. assertEquals(actid4, rm4.getId());
  603. assertEquals(c4.getMessage(), rm4.getFullMessage());
  604. assertEquals(c4.getTreeId(), rm4.getTree().getId());
  605. assertEquals(3, rm4.getParentCount());
  606. assertEquals(actid1, rm4.getParent(0));
  607. assertEquals(actid2, rm4.getParent(1));
  608. assertEquals(actid3, rm4.getParent(2));
  609. }
  610. @Test
  611. public void test027_UnpackedRefHigherPriorityThanPacked()
  612. throws IOException {
  613. String unpackedId = "7f822839a2fe9760f386cbbbcb3f92c5fe81def7";
  614. write(new File(db.getDirectory(), "refs/heads/a"), unpackedId + "\n");
  615. ObjectId resolved = db.resolve("refs/heads/a");
  616. assertEquals(unpackedId, resolved.name());
  617. }
  618. @Test
  619. public void test028_LockPackedRef() throws IOException {
  620. writeTrashFile(".git/packed-refs",
  621. "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar");
  622. writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n");
  623. BUG_WorkAroundRacyGitIssues("packed-refs");
  624. BUG_WorkAroundRacyGitIssues("HEAD");
  625. ObjectId resolve = db.resolve("HEAD");
  626. assertEquals("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.name());
  627. RefUpdate lockRef = db.updateRef("HEAD");
  628. ObjectId newId = ObjectId
  629. .fromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def");
  630. lockRef.setNewObjectId(newId);
  631. assertEquals(RefUpdate.Result.FORCED, lockRef.forceUpdate());
  632. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  633. assertEquals(newId, db.resolve("refs/heads/foobar"));
  634. // Again. The ref already exists
  635. RefUpdate lockRef2 = db.updateRef("HEAD");
  636. ObjectId newId2 = ObjectId
  637. .fromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7");
  638. lockRef2.setNewObjectId(newId2);
  639. assertEquals(RefUpdate.Result.FORCED, lockRef2.forceUpdate());
  640. assertTrue(new File(db.getDirectory(), "refs/heads/foobar").exists());
  641. assertEquals(newId2, db.resolve("refs/heads/foobar"));
  642. }
  643. @Test
  644. public void test30_stripWorkDir() {
  645. File relCwd = new File(".");
  646. File absCwd = relCwd.getAbsoluteFile();
  647. File absBase = new File(new File(absCwd, "repo"), "workdir");
  648. File relBase = new File(new File(relCwd, "repo"), "workdir");
  649. assertEquals(absBase.getAbsolutePath(), relBase.getAbsolutePath());
  650. File relBaseFile = new File(new File(relBase, "other"), "module.c");
  651. File absBaseFile = new File(new File(absBase, "other"), "module.c");
  652. assertEquals("other/module.c", Repository.stripWorkDir(relBase,
  653. relBaseFile));
  654. assertEquals("other/module.c", Repository.stripWorkDir(relBase,
  655. absBaseFile));
  656. assertEquals("other/module.c", Repository.stripWorkDir(absBase,
  657. relBaseFile));
  658. assertEquals("other/module.c", Repository.stripWorkDir(absBase,
  659. absBaseFile));
  660. File relNonFile = new File(new File(relCwd, "not-repo"), ".gitignore");
  661. File absNonFile = new File(new File(absCwd, "not-repo"), ".gitignore");
  662. assertEquals("", Repository.stripWorkDir(relBase, relNonFile));
  663. assertEquals("", Repository.stripWorkDir(absBase, absNonFile));
  664. assertEquals("", Repository.stripWorkDir(db.getWorkTree(), db
  665. .getWorkTree()));
  666. File file = new File(new File(db.getWorkTree(), "subdir"), "File.java");
  667. assertEquals("subdir/File.java", Repository.stripWorkDir(db
  668. .getWorkTree(), file));
  669. }
  670. private ObjectId insertEmptyBlob() throws IOException {
  671. final ObjectId emptyId;
  672. ObjectInserter oi = db.newObjectInserter();
  673. try {
  674. emptyId = oi.insert(Constants.OBJ_BLOB, new byte[] {});
  675. oi.flush();
  676. } finally {
  677. oi.release();
  678. }
  679. return emptyId;
  680. }
  681. private ObjectId insertTree(Tree tree) throws IOException {
  682. ObjectInserter oi = db.newObjectInserter();
  683. try {
  684. ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
  685. oi.flush();
  686. return id;
  687. } finally {
  688. oi.release();
  689. }
  690. }
  691. private ObjectId insertTree(TreeFormatter tree) throws IOException {
  692. ObjectInserter oi = db.newObjectInserter();
  693. try {
  694. ObjectId id = oi.insert(tree);
  695. oi.flush();
  696. return id;
  697. } finally {
  698. oi.release();
  699. }
  700. }
  701. private ObjectId insertCommit(final CommitBuilder builder)
  702. throws IOException, UnsupportedEncodingException {
  703. ObjectInserter oi = db.newObjectInserter();
  704. try {
  705. ObjectId id = oi.insert(builder);
  706. oi.flush();
  707. return id;
  708. } finally {
  709. oi.release();
  710. }
  711. }
  712. private RevCommit parseCommit(AnyObjectId id)
  713. throws MissingObjectException, IncorrectObjectTypeException,
  714. IOException {
  715. RevWalk rw = new RevWalk(db);
  716. try {
  717. return rw.parseCommit(id);
  718. } finally {
  719. rw.release();
  720. }
  721. }
  722. private ObjectId insertTag(final TagBuilder tag) throws IOException,
  723. UnsupportedEncodingException {
  724. ObjectInserter oi = db.newObjectInserter();
  725. try {
  726. ObjectId id = oi.insert(tag);
  727. oi.flush();
  728. return id;
  729. } finally {
  730. oi.release();
  731. }
  732. }
  733. private RevTag parseTag(AnyObjectId id) throws MissingObjectException,
  734. IncorrectObjectTypeException, IOException {
  735. RevWalk rw = new RevWalk(db);
  736. try {
  737. return rw.parseTag(id);
  738. } finally {
  739. rw.release();
  740. }
  741. }
  742. /**
  743. * Kick the timestamp of a local file.
  744. * <p>
  745. * We shouldn't have to make these method calls. The cache is using file
  746. * system timestamps, and on many systems unit tests run faster than the
  747. * modification clock. Dumping the cache after we make an edit behind
  748. * RefDirectory's back allows the tests to pass.
  749. *
  750. * @param name
  751. * the file in the repository to force a time change on.
  752. */
  753. private void BUG_WorkAroundRacyGitIssues(String name) {
  754. File path = new File(db.getDirectory(), name);
  755. long old = path.lastModified();
  756. long set = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  757. path.setLastModified(set);
  758. assertTrue("time changed", old != path.lastModified());
  759. }
  760. }