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

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