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.

FileTreeIteratorTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.treewalk;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.security.MessageDigest;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheCheckout;
  54. import org.eclipse.jgit.dircache.DirCacheEditor;
  55. import org.eclipse.jgit.dircache.DirCacheEntry;
  56. import org.eclipse.jgit.dircache.DirCacheIterator;
  57. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  58. import org.eclipse.jgit.errors.CorruptObjectException;
  59. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  60. import org.eclipse.jgit.errors.MissingObjectException;
  61. import org.eclipse.jgit.junit.RepositoryTestCase;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.FileMode;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectReader;
  66. import org.eclipse.jgit.revwalk.RevCommit;
  67. import org.eclipse.jgit.treewalk.WorkingTreeIterator.MetadataDiff;
  68. import org.eclipse.jgit.treewalk.filter.PathFilter;
  69. import org.eclipse.jgit.util.FileUtils;
  70. import org.eclipse.jgit.util.RawParseUtils;
  71. import org.junit.Before;
  72. import org.junit.Test;
  73. public class FileTreeIteratorTest extends RepositoryTestCase {
  74. private final String[] paths = { "a,", "a,b", "a/b", "a0b" };
  75. private long[] mtime;
  76. @Before
  77. public void setUp() throws Exception {
  78. super.setUp();
  79. // We build the entries backwards so that on POSIX systems we
  80. // are likely to get the entries in the trash directory in the
  81. // opposite order of what they should be in for the iteration.
  82. // This should stress the sorting code better than doing it in
  83. // the correct order.
  84. //
  85. mtime = new long[paths.length];
  86. for (int i = paths.length - 1; i >= 0; i--) {
  87. final String s = paths[i];
  88. writeTrashFile(s, s);
  89. mtime[i] = new File(trash, s).lastModified();
  90. }
  91. }
  92. @Test
  93. public void testEmptyIfRootIsFile() throws Exception {
  94. final File r = new File(trash, paths[0]);
  95. assertTrue(r.isFile());
  96. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  97. db.getConfig().get(WorkingTreeOptions.KEY));
  98. assertTrue(fti.first());
  99. assertTrue(fti.eof());
  100. }
  101. @Test
  102. public void testEmptyIfRootDoesNotExist() throws Exception {
  103. final File r = new File(trash, "not-existing-file");
  104. assertFalse(r.exists());
  105. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  106. db.getConfig().get(WorkingTreeOptions.KEY));
  107. assertTrue(fti.first());
  108. assertTrue(fti.eof());
  109. }
  110. @Test
  111. public void testEmptyIfRootIsEmpty() throws Exception {
  112. final File r = new File(trash, "not-existing-file");
  113. assertFalse(r.exists());
  114. FileUtils.mkdir(r);
  115. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  116. db.getConfig().get(WorkingTreeOptions.KEY));
  117. assertTrue(fti.first());
  118. assertTrue(fti.eof());
  119. }
  120. @Test
  121. public void testSimpleIterate() throws Exception {
  122. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  123. db.getConfig().get(WorkingTreeOptions.KEY));
  124. assertTrue(top.first());
  125. assertFalse(top.eof());
  126. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  127. assertEquals(paths[0], nameOf(top));
  128. assertEquals(paths[0].length(), top.getEntryLength());
  129. assertEquals(mtime[0], top.getEntryLastModified());
  130. top.next(1);
  131. assertFalse(top.first());
  132. assertFalse(top.eof());
  133. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  134. assertEquals(paths[1], nameOf(top));
  135. assertEquals(paths[1].length(), top.getEntryLength());
  136. assertEquals(mtime[1], top.getEntryLastModified());
  137. top.next(1);
  138. assertFalse(top.first());
  139. assertFalse(top.eof());
  140. assertEquals(FileMode.TREE.getBits(), top.mode);
  141. final ObjectReader reader = db.newObjectReader();
  142. final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
  143. assertTrue(sub instanceof FileTreeIterator);
  144. final FileTreeIterator subfti = (FileTreeIterator) sub;
  145. assertTrue(sub.first());
  146. assertFalse(sub.eof());
  147. assertEquals(paths[2], nameOf(sub));
  148. assertEquals(paths[2].length(), subfti.getEntryLength());
  149. assertEquals(mtime[2], subfti.getEntryLastModified());
  150. sub.next(1);
  151. assertTrue(sub.eof());
  152. top.next(1);
  153. assertFalse(top.first());
  154. assertFalse(top.eof());
  155. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  156. assertEquals(paths[3], nameOf(top));
  157. assertEquals(paths[3].length(), top.getEntryLength());
  158. assertEquals(mtime[3], top.getEntryLastModified());
  159. top.next(1);
  160. assertTrue(top.eof());
  161. }
  162. @Test
  163. public void testComputeFileObjectId() throws Exception {
  164. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  165. db.getConfig().get(WorkingTreeOptions.KEY));
  166. final MessageDigest md = Constants.newMessageDigest();
  167. md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
  168. md.update((byte) ' ');
  169. md.update(Constants.encodeASCII(paths[0].length()));
  170. md.update((byte) 0);
  171. md.update(Constants.encode(paths[0]));
  172. final ObjectId expect = ObjectId.fromRaw(md.digest());
  173. assertEquals(expect, top.getEntryObjectId());
  174. // Verify it was cached by removing the file and getting it again.
  175. //
  176. FileUtils.delete(new File(trash, paths[0]));
  177. assertEquals(expect, top.getEntryObjectId());
  178. }
  179. @Test
  180. public void testDirCacheMatchingId() throws Exception {
  181. File f = writeTrashFile("file", "content");
  182. Git git = new Git(db);
  183. writeTrashFile("file", "content");
  184. fsTick(f);
  185. git.add().addFilepattern("file").call();
  186. DirCacheEntry dce = db.readDirCache().getEntry("file");
  187. TreeWalk tw = new TreeWalk(db);
  188. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  189. .getConfig().get(WorkingTreeOptions.KEY));
  190. tw.addTree(fti);
  191. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  192. tw.addTree(dci);
  193. fti.setDirCacheIterator(tw, 1);
  194. while (tw.next() && !tw.getPathString().equals("file")) {
  195. //
  196. }
  197. assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce));
  198. ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset());
  199. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  200. fromRaw.getName());
  201. assertFalse(fti.isModified(dce, false));
  202. }
  203. @Test
  204. public void testIsModifiedSymlink() throws Exception {
  205. File f = writeTrashFile("symlink", "content");
  206. Git git = new Git(db);
  207. git.add().addFilepattern("symlink").call();
  208. git.commit().setMessage("commit").call();
  209. // Modify previously committed DirCacheEntry and write it back to disk
  210. DirCacheEntry dce = db.readDirCache().getEntry("symlink");
  211. dce.setFileMode(FileMode.SYMLINK);
  212. DirCacheCheckout.checkoutEntry(db, f, dce);
  213. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  214. .getConfig().get(WorkingTreeOptions.KEY));
  215. while (!fti.getEntryPathString().equals("symlink"))
  216. fti.next(1);
  217. assertFalse(fti.isModified(dce, false));
  218. }
  219. @Test
  220. public void testIsModifiedFileSmudged() throws Exception {
  221. File f = writeTrashFile("file", "content");
  222. Git git = new Git(db);
  223. // The idea of this test is to check the smudged handling
  224. // Hopefully fsTick will make sure our entry gets smudged
  225. fsTick(f);
  226. writeTrashFile("file", "content");
  227. long lastModified = f.lastModified();
  228. git.add().addFilepattern("file").call();
  229. writeTrashFile("file", "conten2");
  230. f.setLastModified(lastModified);
  231. DirCacheEntry dce = db.readDirCache().getEntry("file");
  232. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  233. .getConfig().get(WorkingTreeOptions.KEY));
  234. while (!fti.getEntryPathString().equals("file"))
  235. fti.next(1);
  236. // If the rounding trick does not work we could skip the compareMetaData
  237. // test and hope that we are usually testing the intended code path.
  238. assertEquals(MetadataDiff.SMUDGED, fti.compareMetadata(dce));
  239. assertTrue(fti.isModified(dce, false));
  240. }
  241. @Test
  242. public void submoduleHeadMatchesIndex() throws Exception {
  243. Git git = new Git(db);
  244. writeTrashFile("file.txt", "content");
  245. git.add().addFilepattern("file.txt").call();
  246. final RevCommit id = git.commit().setMessage("create file").call();
  247. final String path = "sub";
  248. DirCache cache = db.lockDirCache();
  249. DirCacheEditor editor = cache.editor();
  250. editor.add(new PathEdit(path) {
  251. public void apply(DirCacheEntry ent) {
  252. ent.setFileMode(FileMode.GITLINK);
  253. ent.setObjectId(id);
  254. }
  255. });
  256. editor.commit();
  257. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  258. .setDirectory(new File(db.getWorkTree(), path)).call()
  259. .getRepository().close();
  260. TreeWalk walk = new TreeWalk(db);
  261. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  262. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  263. walk.addTree(indexIter);
  264. walk.addTree(workTreeIter);
  265. walk.setFilter(PathFilter.create(path));
  266. assertTrue(walk.next());
  267. assertTrue(indexIter.idEqual(workTreeIter));
  268. }
  269. @Test
  270. public void submoduleWithNoGitDirectory() throws Exception {
  271. Git git = new Git(db);
  272. writeTrashFile("file.txt", "content");
  273. git.add().addFilepattern("file.txt").call();
  274. final RevCommit id = git.commit().setMessage("create file").call();
  275. final String path = "sub";
  276. DirCache cache = db.lockDirCache();
  277. DirCacheEditor editor = cache.editor();
  278. editor.add(new PathEdit(path) {
  279. public void apply(DirCacheEntry ent) {
  280. ent.setFileMode(FileMode.GITLINK);
  281. ent.setObjectId(id);
  282. }
  283. });
  284. editor.commit();
  285. File submoduleRoot = new File(db.getWorkTree(), path);
  286. assertTrue(submoduleRoot.mkdir());
  287. assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
  288. TreeWalk walk = new TreeWalk(db);
  289. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  290. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  291. walk.addTree(indexIter);
  292. walk.addTree(workTreeIter);
  293. walk.setFilter(PathFilter.create(path));
  294. assertTrue(walk.next());
  295. assertFalse(indexIter.idEqual(workTreeIter));
  296. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  297. }
  298. @Test
  299. public void submoduleWithNoHead() throws Exception {
  300. Git git = new Git(db);
  301. writeTrashFile("file.txt", "content");
  302. git.add().addFilepattern("file.txt").call();
  303. final RevCommit id = git.commit().setMessage("create file").call();
  304. final String path = "sub";
  305. DirCache cache = db.lockDirCache();
  306. DirCacheEditor editor = cache.editor();
  307. editor.add(new PathEdit(path) {
  308. public void apply(DirCacheEntry ent) {
  309. ent.setFileMode(FileMode.GITLINK);
  310. ent.setObjectId(id);
  311. }
  312. });
  313. editor.commit();
  314. assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
  315. .call().getRepository());
  316. TreeWalk walk = new TreeWalk(db);
  317. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  318. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  319. walk.addTree(indexIter);
  320. walk.addTree(workTreeIter);
  321. walk.setFilter(PathFilter.create(path));
  322. assertTrue(walk.next());
  323. assertFalse(indexIter.idEqual(workTreeIter));
  324. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  325. }
  326. @Test
  327. public void submoduleDirectoryIterator() throws Exception {
  328. Git git = new Git(db);
  329. writeTrashFile("file.txt", "content");
  330. git.add().addFilepattern("file.txt").call();
  331. final RevCommit id = git.commit().setMessage("create file").call();
  332. final String path = "sub";
  333. DirCache cache = db.lockDirCache();
  334. DirCacheEditor editor = cache.editor();
  335. editor.add(new PathEdit(path) {
  336. public void apply(DirCacheEntry ent) {
  337. ent.setFileMode(FileMode.GITLINK);
  338. ent.setObjectId(id);
  339. }
  340. });
  341. editor.commit();
  342. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  343. .setDirectory(new File(db.getWorkTree(), path)).call()
  344. .getRepository().close();
  345. TreeWalk walk = new TreeWalk(db);
  346. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  347. FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
  348. db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
  349. walk.addTree(indexIter);
  350. walk.addTree(workTreeIter);
  351. walk.setFilter(PathFilter.create(path));
  352. assertTrue(walk.next());
  353. assertTrue(indexIter.idEqual(workTreeIter));
  354. }
  355. @Test
  356. public void submoduleNestedWithHeadMatchingIndex() throws Exception {
  357. Git git = new Git(db);
  358. writeTrashFile("file.txt", "content");
  359. git.add().addFilepattern("file.txt").call();
  360. final RevCommit id = git.commit().setMessage("create file").call();
  361. final String path = "sub/dir1/dir2";
  362. DirCache cache = db.lockDirCache();
  363. DirCacheEditor editor = cache.editor();
  364. editor.add(new PathEdit(path) {
  365. public void apply(DirCacheEntry ent) {
  366. ent.setFileMode(FileMode.GITLINK);
  367. ent.setObjectId(id);
  368. }
  369. });
  370. editor.commit();
  371. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  372. .setDirectory(new File(db.getWorkTree(), path)).call()
  373. .getRepository().close();
  374. TreeWalk walk = new TreeWalk(db);
  375. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  376. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  377. walk.addTree(indexIter);
  378. walk.addTree(workTreeIter);
  379. walk.setFilter(PathFilter.create(path));
  380. assertTrue(walk.next());
  381. assertTrue(indexIter.idEqual(workTreeIter));
  382. }
  383. @Test
  384. public void idOffset() throws Exception {
  385. Git git = new Git(db);
  386. writeTrashFile("fileAinfsonly", "A");
  387. File fileBinindex = writeTrashFile("fileBinindex", "B");
  388. fsTick(fileBinindex);
  389. git.add().addFilepattern("fileBinindex").call();
  390. writeTrashFile("fileCinfsonly", "C");
  391. TreeWalk tw = new TreeWalk(db);
  392. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  393. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  394. tw.addTree(indexIter);
  395. tw.addTree(workTreeIter);
  396. workTreeIter.setDirCacheIterator(tw, 0);
  397. assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
  398. assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
  399. assertEntry("0000000000000000000000000000000000000000", "a", tw);
  400. assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
  401. // The reason for adding this test. Check that the id is correct for
  402. // mixed
  403. assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
  404. "fileAinfsonly", tw);
  405. assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
  406. tw);
  407. assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
  408. "fileCinfsonly", tw);
  409. assertFalse(tw.next());
  410. }
  411. private static void assertEntry(String sha1string, String path, TreeWalk tw)
  412. throws MissingObjectException, IncorrectObjectTypeException,
  413. CorruptObjectException, IOException {
  414. assertTrue(tw.next());
  415. assertEquals(path, tw.getPathString());
  416. assertEquals(sha1string, tw.getObjectId(1).getName() /* 1=filetree here */);
  417. }
  418. private static String nameOf(final AbstractTreeIterator i) {
  419. return RawParseUtils.decode(Constants.CHARSET, i.path, 0, i.pathLen);
  420. }
  421. }