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.

SubmoduleWalkTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (C) 2011, GitHub 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.submodule;
  44. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
  45. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_URL;
  46. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_SUBMODULE_SECTION;
  47. import static org.eclipse.jgit.lib.Constants.DOT_GIT_MODULES;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertFalse;
  50. import static org.junit.Assert.assertNotNull;
  51. import static org.junit.Assert.assertNull;
  52. import static org.junit.Assert.assertTrue;
  53. import java.io.File;
  54. import java.io.FileWriter;
  55. import java.io.IOException;
  56. import org.eclipse.jgit.dircache.DirCache;
  57. import org.eclipse.jgit.dircache.DirCacheEditor;
  58. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  59. import org.eclipse.jgit.dircache.DirCacheEntry;
  60. import org.eclipse.jgit.errors.ConfigInvalidException;
  61. import org.eclipse.jgit.junit.RepositoryTestCase;
  62. import org.eclipse.jgit.junit.TestRepository;
  63. import org.eclipse.jgit.lib.Config;
  64. import org.eclipse.jgit.lib.Constants;
  65. import org.eclipse.jgit.lib.FileMode;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.Repository;
  68. import org.eclipse.jgit.revwalk.RevBlob;
  69. import org.eclipse.jgit.revwalk.RevCommit;
  70. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  71. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  72. import org.eclipse.jgit.treewalk.filter.PathFilter;
  73. import org.junit.Before;
  74. import org.junit.Test;
  75. /**
  76. * Unit tests of {@link SubmoduleWalk}
  77. */
  78. public class SubmoduleWalkTest extends RepositoryTestCase {
  79. private TestRepository<Repository> testDb;
  80. @Before
  81. public void setUp() throws Exception {
  82. super.setUp();
  83. testDb = new TestRepository<Repository>(db);
  84. }
  85. @Test
  86. public void repositoryWithNoSubmodules() throws IOException {
  87. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  88. assertFalse(gen.next());
  89. assertNull(gen.getPath());
  90. assertEquals(ObjectId.zeroId(), gen.getObjectId());
  91. }
  92. @Test
  93. public void repositoryWithRootLevelSubmodule() throws IOException,
  94. ConfigInvalidException {
  95. final ObjectId id = ObjectId
  96. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  97. final String path = "sub";
  98. DirCache cache = db.lockDirCache();
  99. DirCacheEditor editor = cache.editor();
  100. editor.add(new PathEdit(path) {
  101. public void apply(DirCacheEntry ent) {
  102. ent.setFileMode(FileMode.GITLINK);
  103. ent.setObjectId(id);
  104. }
  105. });
  106. editor.commit();
  107. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  108. assertTrue(gen.next());
  109. assertEquals(path, gen.getPath());
  110. assertEquals(id, gen.getObjectId());
  111. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  112. assertNull(gen.getConfigUpdate());
  113. assertNull(gen.getConfigUrl());
  114. assertNull(gen.getModulesPath());
  115. assertNull(gen.getModulesUpdate());
  116. assertNull(gen.getModulesUrl());
  117. assertNull(gen.getRepository());
  118. assertFalse(gen.next());
  119. }
  120. @SuppressWarnings("resource" /* java 7 */)
  121. @Test
  122. public void repositoryWithRootLevelSubmoduleAbsoluteRef()
  123. throws IOException, ConfigInvalidException {
  124. final ObjectId id = ObjectId
  125. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  126. final String path = "sub";
  127. File dotGit = new File(db.getWorkTree(), path + File.separatorChar
  128. + Constants.DOT_GIT);
  129. if (!dotGit.getParentFile().exists())
  130. dotGit.getParentFile().mkdirs();
  131. File modulesGitDir = new File(db.getDirectory(), "modules"
  132. + File.separatorChar + path);
  133. new FileWriter(dotGit).append(
  134. "gitdir: " + modulesGitDir.getAbsolutePath()).close();
  135. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  136. builder.setWorkTree(new File(db.getWorkTree(), path));
  137. builder.build().create();
  138. DirCache cache = db.lockDirCache();
  139. DirCacheEditor editor = cache.editor();
  140. editor.add(new PathEdit(path) {
  141. public void apply(DirCacheEntry ent) {
  142. ent.setFileMode(FileMode.GITLINK);
  143. ent.setObjectId(id);
  144. }
  145. });
  146. editor.commit();
  147. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  148. assertTrue(gen.next());
  149. assertEquals(path, gen.getPath());
  150. assertEquals(id, gen.getObjectId());
  151. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  152. assertNull(gen.getConfigUpdate());
  153. assertNull(gen.getConfigUrl());
  154. assertNull(gen.getModulesPath());
  155. assertNull(gen.getModulesUpdate());
  156. assertNull(gen.getModulesUrl());
  157. Repository subRepo = gen.getRepository();
  158. addRepoToClose(subRepo);
  159. assertNotNull(subRepo);
  160. assertEquals(modulesGitDir.getAbsolutePath(),
  161. subRepo.getDirectory().getAbsolutePath());
  162. assertEquals(new File(db.getWorkTree(), path).getAbsolutePath(),
  163. subRepo.getWorkTree().getAbsolutePath());
  164. assertFalse(gen.next());
  165. }
  166. @SuppressWarnings("resource" /* java 7 */)
  167. @Test
  168. public void repositoryWithRootLevelSubmoduleRelativeRef()
  169. throws IOException, ConfigInvalidException {
  170. final ObjectId id = ObjectId
  171. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  172. final String path = "sub";
  173. File dotGit = new File(db.getWorkTree(), path + File.separatorChar
  174. + Constants.DOT_GIT);
  175. if (!dotGit.getParentFile().exists())
  176. dotGit.getParentFile().mkdirs();
  177. File modulesGitDir = new File(db.getDirectory(), "modules"
  178. + File.separatorChar + path);
  179. new FileWriter(dotGit).append(
  180. "gitdir: " + "../" + Constants.DOT_GIT + "/modules/" + path)
  181. .close();
  182. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  183. builder.setWorkTree(new File(db.getWorkTree(), path));
  184. builder.build().create();
  185. DirCache cache = db.lockDirCache();
  186. DirCacheEditor editor = cache.editor();
  187. editor.add(new PathEdit(path) {
  188. public void apply(DirCacheEntry ent) {
  189. ent.setFileMode(FileMode.GITLINK);
  190. ent.setObjectId(id);
  191. }
  192. });
  193. editor.commit();
  194. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  195. assertTrue(gen.next());
  196. assertEquals(path, gen.getPath());
  197. assertEquals(id, gen.getObjectId());
  198. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  199. assertNull(gen.getConfigUpdate());
  200. assertNull(gen.getConfigUrl());
  201. assertNull(gen.getModulesPath());
  202. assertNull(gen.getModulesUpdate());
  203. assertNull(gen.getModulesUrl());
  204. Repository subRepo = gen.getRepository();
  205. addRepoToClose(subRepo);
  206. assertNotNull(subRepo);
  207. assertEqualsFile(modulesGitDir, subRepo.getDirectory());
  208. assertEqualsFile(new File(db.getWorkTree(), path),
  209. subRepo.getWorkTree());
  210. assertFalse(gen.next());
  211. }
  212. @Test
  213. public void repositoryWithNestedSubmodule() throws IOException,
  214. ConfigInvalidException {
  215. final ObjectId id = ObjectId
  216. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  217. final String path = "sub/dir/final";
  218. DirCache cache = db.lockDirCache();
  219. DirCacheEditor editor = cache.editor();
  220. editor.add(new PathEdit(path) {
  221. public void apply(DirCacheEntry ent) {
  222. ent.setFileMode(FileMode.GITLINK);
  223. ent.setObjectId(id);
  224. }
  225. });
  226. editor.commit();
  227. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  228. assertTrue(gen.next());
  229. assertEquals(path, gen.getPath());
  230. assertEquals(id, gen.getObjectId());
  231. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  232. assertNull(gen.getConfigUpdate());
  233. assertNull(gen.getConfigUrl());
  234. assertNull(gen.getModulesPath());
  235. assertNull(gen.getModulesUpdate());
  236. assertNull(gen.getModulesUrl());
  237. assertNull(gen.getRepository());
  238. assertFalse(gen.next());
  239. }
  240. @Test
  241. public void generatorFilteredToOneOfTwoSubmodules() throws IOException {
  242. final ObjectId id1 = ObjectId
  243. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  244. final String path1 = "sub1";
  245. final ObjectId id2 = ObjectId
  246. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1235");
  247. final String path2 = "sub2";
  248. DirCache cache = db.lockDirCache();
  249. DirCacheEditor editor = cache.editor();
  250. editor.add(new PathEdit(path1) {
  251. public void apply(DirCacheEntry ent) {
  252. ent.setFileMode(FileMode.GITLINK);
  253. ent.setObjectId(id1);
  254. }
  255. });
  256. editor.add(new PathEdit(path2) {
  257. public void apply(DirCacheEntry ent) {
  258. ent.setFileMode(FileMode.GITLINK);
  259. ent.setObjectId(id2);
  260. }
  261. });
  262. editor.commit();
  263. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  264. gen.setFilter(PathFilter.create(path1));
  265. assertTrue(gen.next());
  266. assertEquals(path1, gen.getPath());
  267. assertEquals(id1, gen.getObjectId());
  268. assertFalse(gen.next());
  269. }
  270. @Test
  271. public void indexWithGitmodules() throws Exception {
  272. final ObjectId subId = ObjectId
  273. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  274. final String path = "sub";
  275. final Config gitmodules = new Config();
  276. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  277. "sub");
  278. // Different config in the index should be overridden by the working tree.
  279. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  280. "git://example.com/bad");
  281. final RevBlob gitmodulesBlob = testDb.blob(gitmodules.toText());
  282. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  283. "git://example.com/sub");
  284. writeTrashFile(DOT_GIT_MODULES, gitmodules.toText());
  285. DirCache cache = db.lockDirCache();
  286. DirCacheEditor editor = cache.editor();
  287. editor.add(new PathEdit(path) {
  288. public void apply(DirCacheEntry ent) {
  289. ent.setFileMode(FileMode.GITLINK);
  290. ent.setObjectId(subId);
  291. }
  292. });
  293. editor.add(new PathEdit(DOT_GIT_MODULES) {
  294. public void apply(DirCacheEntry ent) {
  295. ent.setFileMode(FileMode.REGULAR_FILE);
  296. ent.setObjectId(gitmodulesBlob);
  297. }
  298. });
  299. editor.commit();
  300. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  301. assertTrue(gen.next());
  302. assertEquals(path, gen.getPath());
  303. assertEquals(subId, gen.getObjectId());
  304. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  305. assertNull(gen.getConfigUpdate());
  306. assertNull(gen.getConfigUrl());
  307. assertEquals("sub", gen.getModulesPath());
  308. assertNull(gen.getModulesUpdate());
  309. assertEquals("git://example.com/sub", gen.getModulesUrl());
  310. assertNull(gen.getRepository());
  311. assertFalse(gen.next());
  312. }
  313. @Test
  314. public void treeIdWithGitmodules() throws Exception {
  315. final ObjectId subId = ObjectId
  316. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  317. final String path = "sub";
  318. final Config gitmodules = new Config();
  319. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  320. "sub");
  321. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  322. "git://example.com/sub");
  323. RevCommit commit = testDb.getRevWalk().parseCommit(testDb.commit()
  324. .noParents()
  325. .add(DOT_GIT_MODULES, gitmodules.toText())
  326. .edit(new PathEdit(path) {
  327. public void apply(DirCacheEntry ent) {
  328. ent.setFileMode(FileMode.GITLINK);
  329. ent.setObjectId(subId);
  330. }
  331. })
  332. .create());
  333. SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), "sub");
  334. assertEquals(path, gen.getPath());
  335. assertEquals(subId, gen.getObjectId());
  336. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  337. assertNull(gen.getConfigUpdate());
  338. assertNull(gen.getConfigUrl());
  339. assertEquals("sub", gen.getModulesPath());
  340. assertNull(gen.getModulesUpdate());
  341. assertEquals("git://example.com/sub", gen.getModulesUrl());
  342. assertNull(gen.getRepository());
  343. assertFalse(gen.next());
  344. }
  345. @Test
  346. public void testTreeIteratorWithGitmodules() throws Exception {
  347. final ObjectId subId = ObjectId
  348. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  349. final String path = "sub";
  350. final Config gitmodules = new Config();
  351. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  352. "sub");
  353. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  354. "git://example.com/sub");
  355. RevCommit commit = testDb.getRevWalk().parseCommit(testDb.commit()
  356. .noParents()
  357. .add(DOT_GIT_MODULES, gitmodules.toText())
  358. .edit(new PathEdit(path) {
  359. public void apply(DirCacheEntry ent) {
  360. ent.setFileMode(FileMode.GITLINK);
  361. ent.setObjectId(subId);
  362. }
  363. })
  364. .create());
  365. final CanonicalTreeParser p = new CanonicalTreeParser();
  366. p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree());
  367. SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub");
  368. assertEquals(path, gen.getPath());
  369. assertEquals(subId, gen.getObjectId());
  370. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  371. assertNull(gen.getConfigUpdate());
  372. assertNull(gen.getConfigUrl());
  373. assertEquals("sub", gen.getModulesPath());
  374. assertNull(gen.getModulesUpdate());
  375. assertEquals("git://example.com/sub", gen.getModulesUrl());
  376. assertNull(gen.getRepository());
  377. assertFalse(gen.next());
  378. }
  379. }