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 29KB

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