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

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