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.

SubmoduleInitTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.util.Collection;
  52. import org.eclipse.jgit.api.SubmoduleInitCommand;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.api.errors.JGitInternalException;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.dircache.DirCacheEditor;
  57. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  58. import org.eclipse.jgit.dircache.DirCacheEntry;
  59. import org.eclipse.jgit.errors.ConfigInvalidException;
  60. import org.eclipse.jgit.lib.ConfigConstants;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.RepositoryTestCase;
  65. import org.eclipse.jgit.storage.file.FileBasedConfig;
  66. import org.junit.Test;
  67. /**
  68. * Unit tests of {@link SubmoduleInitCommand}
  69. */
  70. public class SubmoduleInitTest extends RepositoryTestCase {
  71. @Test
  72. public void repositoryWithNoSubmodules() throws GitAPIException {
  73. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  74. Collection<String> modules = command.call();
  75. assertNotNull(modules);
  76. assertTrue(modules.isEmpty());
  77. }
  78. @Test
  79. public void repositoryWithUninitializedModule() throws IOException,
  80. ConfigInvalidException, GitAPIException {
  81. final String path = addSubmoduleToIndex();
  82. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  83. assertTrue(generator.next());
  84. assertNull(generator.getConfigUrl());
  85. assertNull(generator.getConfigUpdate());
  86. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  87. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  88. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  89. ConfigConstants.CONFIG_KEY_PATH, path);
  90. String url = "git://server/repo.git";
  91. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  92. ConfigConstants.CONFIG_KEY_URL, url);
  93. String update = "rebase";
  94. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  95. ConfigConstants.CONFIG_KEY_UPDATE, update);
  96. modulesConfig.save();
  97. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  98. Collection<String> modules = command.call();
  99. assertNotNull(modules);
  100. assertEquals(1, modules.size());
  101. assertEquals(path, modules.iterator().next());
  102. generator = SubmoduleWalk.forIndex(db);
  103. assertTrue(generator.next());
  104. assertEquals(url, generator.getConfigUrl());
  105. assertEquals(update, generator.getConfigUpdate());
  106. }
  107. @Test
  108. public void resolveSameLevelRelativeUrl() throws Exception {
  109. final String path = addSubmoduleToIndex();
  110. String base = "git://server/repo.git";
  111. FileBasedConfig config = db.getConfig();
  112. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  113. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  114. base);
  115. config.save();
  116. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  117. assertTrue(generator.next());
  118. assertNull(generator.getConfigUrl());
  119. assertNull(generator.getConfigUpdate());
  120. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  121. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  122. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  123. ConfigConstants.CONFIG_KEY_PATH, path);
  124. String url = "./sub.git";
  125. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  126. ConfigConstants.CONFIG_KEY_URL, url);
  127. String update = "rebase";
  128. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  129. ConfigConstants.CONFIG_KEY_UPDATE, update);
  130. modulesConfig.save();
  131. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  132. Collection<String> modules = command.call();
  133. assertNotNull(modules);
  134. assertEquals(1, modules.size());
  135. assertEquals(path, modules.iterator().next());
  136. generator = SubmoduleWalk.forIndex(db);
  137. assertTrue(generator.next());
  138. assertEquals("git://server/repo.git/sub.git", generator.getConfigUrl());
  139. assertEquals(update, generator.getConfigUpdate());
  140. }
  141. @Test
  142. public void resolveOneLevelHigherRelativeUrl() throws IOException,
  143. ConfigInvalidException, GitAPIException {
  144. final String path = addSubmoduleToIndex();
  145. String base = "git://server/repo.git";
  146. FileBasedConfig config = db.getConfig();
  147. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  148. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  149. base);
  150. config.save();
  151. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  152. assertTrue(generator.next());
  153. assertNull(generator.getConfigUrl());
  154. assertNull(generator.getConfigUpdate());
  155. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  156. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  157. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  158. ConfigConstants.CONFIG_KEY_PATH, path);
  159. String url = "../sub.git";
  160. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  161. ConfigConstants.CONFIG_KEY_URL, url);
  162. String update = "rebase";
  163. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  164. ConfigConstants.CONFIG_KEY_UPDATE, update);
  165. modulesConfig.save();
  166. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  167. Collection<String> modules = command.call();
  168. assertNotNull(modules);
  169. assertEquals(1, modules.size());
  170. assertEquals(path, modules.iterator().next());
  171. generator = SubmoduleWalk.forIndex(db);
  172. assertTrue(generator.next());
  173. assertEquals("git://server/sub.git", generator.getConfigUrl());
  174. assertEquals(update, generator.getConfigUpdate());
  175. }
  176. @Test
  177. public void resolveTwoLevelHigherRelativeUrl() throws IOException,
  178. ConfigInvalidException, GitAPIException {
  179. final String path = addSubmoduleToIndex();
  180. String base = "git://server/repo.git";
  181. FileBasedConfig config = db.getConfig();
  182. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  183. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  184. base);
  185. config.save();
  186. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  187. assertTrue(generator.next());
  188. assertNull(generator.getConfigUrl());
  189. assertNull(generator.getConfigUpdate());
  190. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  191. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  192. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  193. ConfigConstants.CONFIG_KEY_PATH, path);
  194. String url = "../../server2/sub.git";
  195. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  196. ConfigConstants.CONFIG_KEY_URL, url);
  197. String update = "rebase";
  198. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  199. ConfigConstants.CONFIG_KEY_UPDATE, update);
  200. modulesConfig.save();
  201. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  202. Collection<String> modules = command.call();
  203. assertNotNull(modules);
  204. assertEquals(1, modules.size());
  205. assertEquals(path, modules.iterator().next());
  206. generator = SubmoduleWalk.forIndex(db);
  207. assertTrue(generator.next());
  208. assertEquals("git://server2/sub.git", generator.getConfigUrl());
  209. assertEquals(update, generator.getConfigUpdate());
  210. }
  211. @Test
  212. public void resolveWorkingDirectoryRelativeUrl() throws IOException,
  213. GitAPIException, ConfigInvalidException {
  214. final String path = addSubmoduleToIndex();
  215. String base = db.getWorkTree().getAbsolutePath();
  216. if (File.separatorChar == '\\')
  217. base = base.replace('\\', '/');
  218. FileBasedConfig config = db.getConfig();
  219. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  220. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  221. null);
  222. config.save();
  223. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  224. assertTrue(generator.next());
  225. assertNull(generator.getConfigUrl());
  226. assertNull(generator.getConfigUpdate());
  227. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  228. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  229. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  230. ConfigConstants.CONFIG_KEY_PATH, path);
  231. String url = "./sub.git";
  232. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  233. ConfigConstants.CONFIG_KEY_URL, url);
  234. String update = "rebase";
  235. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  236. ConfigConstants.CONFIG_KEY_UPDATE, update);
  237. modulesConfig.save();
  238. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  239. Collection<String> modules = command.call();
  240. assertNotNull(modules);
  241. assertEquals(1, modules.size());
  242. assertEquals(path, modules.iterator().next());
  243. generator = SubmoduleWalk.forIndex(db);
  244. assertTrue(generator.next());
  245. assertEquals(base + "/sub.git", generator.getConfigUrl());
  246. assertEquals(update, generator.getConfigUpdate());
  247. }
  248. @Test
  249. public void resolveInvalidParentUrl() throws IOException,
  250. ConfigInvalidException, GitAPIException {
  251. final String path = addSubmoduleToIndex();
  252. String base = "no_slash";
  253. FileBasedConfig config = db.getConfig();
  254. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  255. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  256. base);
  257. config.save();
  258. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  259. assertTrue(generator.next());
  260. assertNull(generator.getConfigUrl());
  261. assertNull(generator.getConfigUpdate());
  262. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  263. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  264. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  265. ConfigConstants.CONFIG_KEY_PATH, path);
  266. String url = "../sub.git";
  267. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  268. ConfigConstants.CONFIG_KEY_URL, url);
  269. modulesConfig.save();
  270. try {
  271. new SubmoduleInitCommand(db).call();
  272. fail("Exception not thrown");
  273. } catch (JGitInternalException e) {
  274. assertTrue(e.getCause() instanceof IOException);
  275. }
  276. }
  277. private String addSubmoduleToIndex() throws IOException {
  278. final ObjectId id = ObjectId
  279. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  280. final String path = "sub";
  281. DirCache cache = db.lockDirCache();
  282. DirCacheEditor editor = cache.editor();
  283. editor.add(new PathEdit(path) {
  284. public void apply(DirCacheEntry ent) {
  285. ent.setFileMode(FileMode.GITLINK);
  286. ent.setObjectId(id);
  287. }
  288. });
  289. editor.commit();
  290. return path;
  291. }
  292. }