Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FileTreeIteratorTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.DirCacheEditor.PathEdit;
  56. import org.eclipse.jgit.dircache.DirCacheEntry;
  57. import org.eclipse.jgit.dircache.DirCacheIterator;
  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 testGetEntryContentLength() throws Exception {
  94. final FileTreeIterator fti = new FileTreeIterator(db);
  95. fti.next(1);
  96. assertEquals(3, fti.getEntryContentLength());
  97. fti.back(1);
  98. assertEquals(2, fti.getEntryContentLength());
  99. fti.next(1);
  100. assertEquals(3, fti.getEntryContentLength());
  101. fti.reset();
  102. assertEquals(2, fti.getEntryContentLength());
  103. }
  104. @Test
  105. public void testEmptyIfRootIsFile() throws Exception {
  106. final File r = new File(trash, paths[0]);
  107. assertTrue(r.isFile());
  108. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  109. db.getConfig().get(WorkingTreeOptions.KEY));
  110. assertTrue(fti.first());
  111. assertTrue(fti.eof());
  112. }
  113. @Test
  114. public void testEmptyIfRootDoesNotExist() throws Exception {
  115. final File r = new File(trash, "not-existing-file");
  116. assertFalse(r.exists());
  117. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  118. db.getConfig().get(WorkingTreeOptions.KEY));
  119. assertTrue(fti.first());
  120. assertTrue(fti.eof());
  121. }
  122. @Test
  123. public void testEmptyIfRootIsEmpty() throws Exception {
  124. final File r = new File(trash, "not-existing-file");
  125. assertFalse(r.exists());
  126. FileUtils.mkdir(r);
  127. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  128. db.getConfig().get(WorkingTreeOptions.KEY));
  129. assertTrue(fti.first());
  130. assertTrue(fti.eof());
  131. }
  132. @Test
  133. public void testSimpleIterate() throws Exception {
  134. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  135. db.getConfig().get(WorkingTreeOptions.KEY));
  136. assertTrue(top.first());
  137. assertFalse(top.eof());
  138. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  139. assertEquals(paths[0], nameOf(top));
  140. assertEquals(paths[0].length(), top.getEntryLength());
  141. assertEquals(mtime[0], top.getEntryLastModified());
  142. top.next(1);
  143. assertFalse(top.first());
  144. assertFalse(top.eof());
  145. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  146. assertEquals(paths[1], nameOf(top));
  147. assertEquals(paths[1].length(), top.getEntryLength());
  148. assertEquals(mtime[1], top.getEntryLastModified());
  149. top.next(1);
  150. assertFalse(top.first());
  151. assertFalse(top.eof());
  152. assertEquals(FileMode.TREE.getBits(), top.mode);
  153. final ObjectReader reader = db.newObjectReader();
  154. final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
  155. assertTrue(sub instanceof FileTreeIterator);
  156. final FileTreeIterator subfti = (FileTreeIterator) sub;
  157. assertTrue(sub.first());
  158. assertFalse(sub.eof());
  159. assertEquals(paths[2], nameOf(sub));
  160. assertEquals(paths[2].length(), subfti.getEntryLength());
  161. assertEquals(mtime[2], subfti.getEntryLastModified());
  162. sub.next(1);
  163. assertTrue(sub.eof());
  164. top.next(1);
  165. assertFalse(top.first());
  166. assertFalse(top.eof());
  167. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  168. assertEquals(paths[3], nameOf(top));
  169. assertEquals(paths[3].length(), top.getEntryLength());
  170. assertEquals(mtime[3], top.getEntryLastModified());
  171. top.next(1);
  172. assertTrue(top.eof());
  173. }
  174. @Test
  175. public void testComputeFileObjectId() throws Exception {
  176. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  177. db.getConfig().get(WorkingTreeOptions.KEY));
  178. final MessageDigest md = Constants.newMessageDigest();
  179. md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
  180. md.update((byte) ' ');
  181. md.update(Constants.encodeASCII(paths[0].length()));
  182. md.update((byte) 0);
  183. md.update(Constants.encode(paths[0]));
  184. final ObjectId expect = ObjectId.fromRaw(md.digest());
  185. assertEquals(expect, top.getEntryObjectId());
  186. // Verify it was cached by removing the file and getting it again.
  187. //
  188. FileUtils.delete(new File(trash, paths[0]));
  189. assertEquals(expect, top.getEntryObjectId());
  190. }
  191. @Test
  192. public void testDirCacheMatchingId() throws Exception {
  193. File f = writeTrashFile("file", "content");
  194. Git git = new Git(db);
  195. writeTrashFile("file", "content");
  196. fsTick(f);
  197. git.add().addFilepattern("file").call();
  198. DirCacheEntry dce = db.readDirCache().getEntry("file");
  199. TreeWalk tw = new TreeWalk(db);
  200. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  201. .getConfig().get(WorkingTreeOptions.KEY));
  202. tw.addTree(fti);
  203. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  204. tw.addTree(dci);
  205. fti.setDirCacheIterator(tw, 1);
  206. while (tw.next() && !tw.getPathString().equals("file")) {
  207. //
  208. }
  209. assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce));
  210. ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset());
  211. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  212. fromRaw.getName());
  213. ObjectReader objectReader = db.newObjectReader();
  214. assertFalse(fti.isModified(dce, false, objectReader));
  215. objectReader.release();
  216. }
  217. @Test
  218. public void testIsModifiedSymlink() throws Exception {
  219. File f = writeTrashFile("symlink", "content");
  220. Git git = new Git(db);
  221. git.add().addFilepattern("symlink").call();
  222. git.commit().setMessage("commit").call();
  223. // Modify previously committed DirCacheEntry and write it back to disk
  224. DirCacheEntry dce = db.readDirCache().getEntry("symlink");
  225. dce.setFileMode(FileMode.SYMLINK);
  226. DirCacheCheckout.checkoutEntry(db, f, dce);
  227. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  228. .getConfig().get(WorkingTreeOptions.KEY));
  229. while (!fti.getEntryPathString().equals("symlink"))
  230. fti.next(1);
  231. ObjectReader objectReader = db.newObjectReader();
  232. assertFalse(fti.isModified(dce, false, objectReader));
  233. objectReader.release();
  234. }
  235. @Test
  236. public void testIsModifiedFileSmudged() throws Exception {
  237. File f = writeTrashFile("file", "content");
  238. Git git = new Git(db);
  239. // The idea of this test is to check the smudged handling
  240. // Hopefully fsTick will make sure our entry gets smudged
  241. fsTick(f);
  242. writeTrashFile("file", "content");
  243. long lastModified = f.lastModified();
  244. git.add().addFilepattern("file").call();
  245. writeTrashFile("file", "conten2");
  246. f.setLastModified(lastModified);
  247. DirCacheEntry dce = db.readDirCache().getEntry("file");
  248. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  249. .getConfig().get(WorkingTreeOptions.KEY));
  250. while (!fti.getEntryPathString().equals("file"))
  251. fti.next(1);
  252. // If the rounding trick does not work we could skip the compareMetaData
  253. // test and hope that we are usually testing the intended code path.
  254. assertEquals(MetadataDiff.SMUDGED, fti.compareMetadata(dce));
  255. ObjectReader objectReader = db.newObjectReader();
  256. assertTrue(fti.isModified(dce, false, objectReader));
  257. objectReader.release();
  258. }
  259. @Test
  260. public void submoduleHeadMatchesIndex() throws Exception {
  261. Git git = new Git(db);
  262. writeTrashFile("file.txt", "content");
  263. git.add().addFilepattern("file.txt").call();
  264. final RevCommit id = git.commit().setMessage("create file").call();
  265. final String path = "sub";
  266. DirCache cache = db.lockDirCache();
  267. DirCacheEditor editor = cache.editor();
  268. editor.add(new PathEdit(path) {
  269. public void apply(DirCacheEntry ent) {
  270. ent.setFileMode(FileMode.GITLINK);
  271. ent.setObjectId(id);
  272. }
  273. });
  274. editor.commit();
  275. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  276. .setDirectory(new File(db.getWorkTree(), path)).call()
  277. .getRepository().close();
  278. TreeWalk walk = new TreeWalk(db);
  279. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  280. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  281. walk.addTree(indexIter);
  282. walk.addTree(workTreeIter);
  283. walk.setFilter(PathFilter.create(path));
  284. assertTrue(walk.next());
  285. assertTrue(indexIter.idEqual(workTreeIter));
  286. }
  287. @Test
  288. public void submoduleWithNoGitDirectory() throws Exception {
  289. Git git = new Git(db);
  290. writeTrashFile("file.txt", "content");
  291. git.add().addFilepattern("file.txt").call();
  292. final RevCommit id = git.commit().setMessage("create file").call();
  293. final String path = "sub";
  294. DirCache cache = db.lockDirCache();
  295. DirCacheEditor editor = cache.editor();
  296. editor.add(new PathEdit(path) {
  297. public void apply(DirCacheEntry ent) {
  298. ent.setFileMode(FileMode.GITLINK);
  299. ent.setObjectId(id);
  300. }
  301. });
  302. editor.commit();
  303. File submoduleRoot = new File(db.getWorkTree(), path);
  304. assertTrue(submoduleRoot.mkdir());
  305. assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
  306. TreeWalk walk = new TreeWalk(db);
  307. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  308. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  309. walk.addTree(indexIter);
  310. walk.addTree(workTreeIter);
  311. walk.setFilter(PathFilter.create(path));
  312. assertTrue(walk.next());
  313. assertFalse(indexIter.idEqual(workTreeIter));
  314. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  315. }
  316. @Test
  317. public void submoduleWithNoHead() throws Exception {
  318. Git git = new Git(db);
  319. writeTrashFile("file.txt", "content");
  320. git.add().addFilepattern("file.txt").call();
  321. final RevCommit id = git.commit().setMessage("create file").call();
  322. final String path = "sub";
  323. DirCache cache = db.lockDirCache();
  324. DirCacheEditor editor = cache.editor();
  325. editor.add(new PathEdit(path) {
  326. public void apply(DirCacheEntry ent) {
  327. ent.setFileMode(FileMode.GITLINK);
  328. ent.setObjectId(id);
  329. }
  330. });
  331. editor.commit();
  332. assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
  333. .call().getRepository());
  334. TreeWalk walk = new TreeWalk(db);
  335. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  336. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  337. walk.addTree(indexIter);
  338. walk.addTree(workTreeIter);
  339. walk.setFilter(PathFilter.create(path));
  340. assertTrue(walk.next());
  341. assertFalse(indexIter.idEqual(workTreeIter));
  342. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  343. }
  344. @Test
  345. public void submoduleDirectoryIterator() throws Exception {
  346. Git git = new Git(db);
  347. writeTrashFile("file.txt", "content");
  348. git.add().addFilepattern("file.txt").call();
  349. final RevCommit id = git.commit().setMessage("create file").call();
  350. final String path = "sub";
  351. DirCache cache = db.lockDirCache();
  352. DirCacheEditor editor = cache.editor();
  353. editor.add(new PathEdit(path) {
  354. public void apply(DirCacheEntry ent) {
  355. ent.setFileMode(FileMode.GITLINK);
  356. ent.setObjectId(id);
  357. }
  358. });
  359. editor.commit();
  360. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  361. .setDirectory(new File(db.getWorkTree(), path)).call()
  362. .getRepository().close();
  363. TreeWalk walk = new TreeWalk(db);
  364. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  365. FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
  366. db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
  367. walk.addTree(indexIter);
  368. walk.addTree(workTreeIter);
  369. walk.setFilter(PathFilter.create(path));
  370. assertTrue(walk.next());
  371. assertTrue(indexIter.idEqual(workTreeIter));
  372. }
  373. @Test
  374. public void submoduleNestedWithHeadMatchingIndex() throws Exception {
  375. Git git = new Git(db);
  376. writeTrashFile("file.txt", "content");
  377. git.add().addFilepattern("file.txt").call();
  378. final RevCommit id = git.commit().setMessage("create file").call();
  379. final String path = "sub/dir1/dir2";
  380. DirCache cache = db.lockDirCache();
  381. DirCacheEditor editor = cache.editor();
  382. editor.add(new PathEdit(path) {
  383. public void apply(DirCacheEntry ent) {
  384. ent.setFileMode(FileMode.GITLINK);
  385. ent.setObjectId(id);
  386. }
  387. });
  388. editor.commit();
  389. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  390. .setDirectory(new File(db.getWorkTree(), path)).call()
  391. .getRepository().close();
  392. TreeWalk walk = new TreeWalk(db);
  393. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  394. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  395. walk.addTree(indexIter);
  396. walk.addTree(workTreeIter);
  397. walk.setFilter(PathFilter.create(path));
  398. assertTrue(walk.next());
  399. assertTrue(indexIter.idEqual(workTreeIter));
  400. }
  401. @Test
  402. public void idOffset() throws Exception {
  403. Git git = new Git(db);
  404. writeTrashFile("fileAinfsonly", "A");
  405. File fileBinindex = writeTrashFile("fileBinindex", "B");
  406. fsTick(fileBinindex);
  407. git.add().addFilepattern("fileBinindex").call();
  408. writeTrashFile("fileCinfsonly", "C");
  409. TreeWalk tw = new TreeWalk(db);
  410. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  411. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  412. tw.addTree(indexIter);
  413. tw.addTree(workTreeIter);
  414. workTreeIter.setDirCacheIterator(tw, 0);
  415. assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
  416. assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
  417. assertEntry("0000000000000000000000000000000000000000", "a", tw);
  418. assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
  419. // The reason for adding this test. Check that the id is correct for
  420. // mixed
  421. assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
  422. "fileAinfsonly", tw);
  423. assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
  424. tw);
  425. assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
  426. "fileCinfsonly", tw);
  427. assertFalse(tw.next());
  428. }
  429. private static void assertEntry(String sha1string, String path, TreeWalk tw)
  430. throws MissingObjectException, IncorrectObjectTypeException,
  431. CorruptObjectException, IOException {
  432. assertTrue(tw.next());
  433. assertEquals(path, tw.getPathString());
  434. assertEquals(sha1string, tw.getObjectId(1).getName() /* 1=filetree here */);
  435. }
  436. private static String nameOf(final AbstractTreeIterator i) {
  437. return RawParseUtils.decode(Constants.CHARSET, i.path, 0, i.pathLen);
  438. }
  439. }