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