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

T0003_Basic.java 30KB

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