您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

T0003_Basic.java 31KB

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