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

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