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

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