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

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