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

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