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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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, subRepo.getDirectory());
  161. assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());
  162. assertFalse(gen.next());
  163. }
  164. @SuppressWarnings("resource" /* java 7 */)
  165. @Test
  166. public void repositoryWithRootLevelSubmoduleRelativeRef()
  167. throws IOException, ConfigInvalidException {
  168. final ObjectId id = ObjectId
  169. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  170. final String path = "sub";
  171. File dotGit = new File(db.getWorkTree(), path + File.separatorChar
  172. + Constants.DOT_GIT);
  173. if (!dotGit.getParentFile().exists())
  174. dotGit.getParentFile().mkdirs();
  175. File modulesGitDir = new File(db.getDirectory(), "modules"
  176. + File.separatorChar + path);
  177. new FileWriter(dotGit).append(
  178. "gitdir: " + "../" + Constants.DOT_GIT + "/modules/" + path)
  179. .close();
  180. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  181. builder.setWorkTree(new File(db.getWorkTree(), path));
  182. builder.build().create();
  183. DirCache cache = db.lockDirCache();
  184. DirCacheEditor editor = cache.editor();
  185. editor.add(new PathEdit(path) {
  186. public void apply(DirCacheEntry ent) {
  187. ent.setFileMode(FileMode.GITLINK);
  188. ent.setObjectId(id);
  189. }
  190. });
  191. editor.commit();
  192. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  193. assertTrue(gen.next());
  194. assertEquals(path, gen.getPath());
  195. assertEquals(id, gen.getObjectId());
  196. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  197. assertNull(gen.getConfigUpdate());
  198. assertNull(gen.getConfigUrl());
  199. assertNull(gen.getModulesPath());
  200. assertNull(gen.getModulesUpdate());
  201. assertNull(gen.getModulesUrl());
  202. Repository subRepo = gen.getRepository();
  203. addRepoToClose(subRepo);
  204. assertNotNull(subRepo);
  205. assertEquals(modulesGitDir, subRepo.getDirectory());
  206. assertEquals(new File(db.getWorkTree(), path), subRepo.getWorkTree());
  207. assertFalse(gen.next());
  208. }
  209. @Test
  210. public void repositoryWithNestedSubmodule() throws IOException,
  211. ConfigInvalidException {
  212. final ObjectId id = ObjectId
  213. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  214. final String path = "sub/dir/final";
  215. DirCache cache = db.lockDirCache();
  216. DirCacheEditor editor = cache.editor();
  217. editor.add(new PathEdit(path) {
  218. public void apply(DirCacheEntry ent) {
  219. ent.setFileMode(FileMode.GITLINK);
  220. ent.setObjectId(id);
  221. }
  222. });
  223. editor.commit();
  224. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  225. assertTrue(gen.next());
  226. assertEquals(path, gen.getPath());
  227. assertEquals(id, gen.getObjectId());
  228. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  229. assertNull(gen.getConfigUpdate());
  230. assertNull(gen.getConfigUrl());
  231. assertNull(gen.getModulesPath());
  232. assertNull(gen.getModulesUpdate());
  233. assertNull(gen.getModulesUrl());
  234. assertNull(gen.getRepository());
  235. assertFalse(gen.next());
  236. }
  237. @Test
  238. public void generatorFilteredToOneOfTwoSubmodules() throws IOException {
  239. final ObjectId id1 = ObjectId
  240. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  241. final String path1 = "sub1";
  242. final ObjectId id2 = ObjectId
  243. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1235");
  244. final String path2 = "sub2";
  245. DirCache cache = db.lockDirCache();
  246. DirCacheEditor editor = cache.editor();
  247. editor.add(new PathEdit(path1) {
  248. public void apply(DirCacheEntry ent) {
  249. ent.setFileMode(FileMode.GITLINK);
  250. ent.setObjectId(id1);
  251. }
  252. });
  253. editor.add(new PathEdit(path2) {
  254. public void apply(DirCacheEntry ent) {
  255. ent.setFileMode(FileMode.GITLINK);
  256. ent.setObjectId(id2);
  257. }
  258. });
  259. editor.commit();
  260. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  261. gen.setFilter(PathFilter.create(path1));
  262. assertTrue(gen.next());
  263. assertEquals(path1, gen.getPath());
  264. assertEquals(id1, gen.getObjectId());
  265. assertFalse(gen.next());
  266. }
  267. @Test
  268. public void indexWithGitmodules() throws Exception {
  269. final ObjectId subId = ObjectId
  270. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  271. final String path = "sub";
  272. final Config gitmodules = new Config();
  273. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  274. "sub");
  275. // Different config in the index should be overridden by the working tree.
  276. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  277. "git://example.com/bad");
  278. final RevBlob gitmodulesBlob = testDb.blob(gitmodules.toText());
  279. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  280. "git://example.com/sub");
  281. writeTrashFile(DOT_GIT_MODULES, gitmodules.toText());
  282. DirCache cache = db.lockDirCache();
  283. DirCacheEditor editor = cache.editor();
  284. editor.add(new PathEdit(path) {
  285. public void apply(DirCacheEntry ent) {
  286. ent.setFileMode(FileMode.GITLINK);
  287. ent.setObjectId(subId);
  288. }
  289. });
  290. editor.add(new PathEdit(DOT_GIT_MODULES) {
  291. public void apply(DirCacheEntry ent) {
  292. ent.setFileMode(FileMode.REGULAR_FILE);
  293. ent.setObjectId(gitmodulesBlob);
  294. }
  295. });
  296. editor.commit();
  297. SubmoduleWalk gen = SubmoduleWalk.forIndex(db);
  298. assertTrue(gen.next());
  299. assertEquals(path, gen.getPath());
  300. assertEquals(subId, gen.getObjectId());
  301. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  302. assertNull(gen.getConfigUpdate());
  303. assertNull(gen.getConfigUrl());
  304. assertEquals("sub", gen.getModulesPath());
  305. assertNull(gen.getModulesUpdate());
  306. assertEquals("git://example.com/sub", gen.getModulesUrl());
  307. assertNull(gen.getRepository());
  308. assertFalse(gen.next());
  309. }
  310. @Test
  311. public void treeIdWithGitmodules() throws Exception {
  312. final ObjectId subId = ObjectId
  313. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  314. final String path = "sub";
  315. final Config gitmodules = new Config();
  316. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  317. "sub");
  318. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  319. "git://example.com/sub");
  320. RevCommit commit = testDb.getRevWalk().parseCommit(testDb.commit()
  321. .noParents()
  322. .add(DOT_GIT_MODULES, gitmodules.toText())
  323. .edit(new PathEdit(path) {
  324. public void apply(DirCacheEntry ent) {
  325. ent.setFileMode(FileMode.GITLINK);
  326. ent.setObjectId(subId);
  327. }
  328. })
  329. .create());
  330. SubmoduleWalk gen = SubmoduleWalk.forPath(db, commit.getTree(), "sub");
  331. assertEquals(path, gen.getPath());
  332. assertEquals(subId, gen.getObjectId());
  333. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  334. assertNull(gen.getConfigUpdate());
  335. assertNull(gen.getConfigUrl());
  336. assertEquals("sub", gen.getModulesPath());
  337. assertNull(gen.getModulesUpdate());
  338. assertEquals("git://example.com/sub", gen.getModulesUrl());
  339. assertNull(gen.getRepository());
  340. assertFalse(gen.next());
  341. }
  342. @Test
  343. public void testTreeIteratorWithGitmodules() throws Exception {
  344. final ObjectId subId = ObjectId
  345. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  346. final String path = "sub";
  347. final Config gitmodules = new Config();
  348. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_PATH,
  349. "sub");
  350. gitmodules.setString(CONFIG_SUBMODULE_SECTION, path, CONFIG_KEY_URL,
  351. "git://example.com/sub");
  352. RevCommit commit = testDb.getRevWalk().parseCommit(testDb.commit()
  353. .noParents()
  354. .add(DOT_GIT_MODULES, gitmodules.toText())
  355. .edit(new PathEdit(path) {
  356. public void apply(DirCacheEntry ent) {
  357. ent.setFileMode(FileMode.GITLINK);
  358. ent.setObjectId(subId);
  359. }
  360. })
  361. .create());
  362. final CanonicalTreeParser p = new CanonicalTreeParser();
  363. p.reset(testDb.getRevWalk().getObjectReader(), commit.getTree());
  364. SubmoduleWalk gen = SubmoduleWalk.forPath(db, p, "sub");
  365. assertEquals(path, gen.getPath());
  366. assertEquals(subId, gen.getObjectId());
  367. assertEquals(new File(db.getWorkTree(), path), gen.getDirectory());
  368. assertNull(gen.getConfigUpdate());
  369. assertNull(gen.getConfigUrl());
  370. assertEquals("sub", gen.getModulesPath());
  371. assertNull(gen.getModulesUpdate());
  372. assertEquals("git://example.com/sub", gen.getModulesUrl());
  373. assertNull(gen.getRepository());
  374. assertFalse(gen.next());
  375. }
  376. }