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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.submodule;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.Collection;
  19. import org.eclipse.jgit.api.SubmoduleInitCommand;
  20. import org.eclipse.jgit.api.errors.GitAPIException;
  21. import org.eclipse.jgit.api.errors.JGitInternalException;
  22. import org.eclipse.jgit.dircache.DirCache;
  23. import org.eclipse.jgit.dircache.DirCacheEditor;
  24. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  25. import org.eclipse.jgit.dircache.DirCacheEntry;
  26. import org.eclipse.jgit.errors.ConfigInvalidException;
  27. import org.eclipse.jgit.junit.RepositoryTestCase;
  28. import org.eclipse.jgit.lib.ConfigConstants;
  29. import org.eclipse.jgit.lib.Constants;
  30. import org.eclipse.jgit.lib.FileMode;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.storage.file.FileBasedConfig;
  33. import org.junit.Test;
  34. /**
  35. * Unit tests of {@link SubmoduleInitCommand}
  36. */
  37. public class SubmoduleInitTest extends RepositoryTestCase {
  38. @Test
  39. public void repositoryWithNoSubmodules() throws GitAPIException {
  40. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  41. Collection<String> modules = command.call();
  42. assertNotNull(modules);
  43. assertTrue(modules.isEmpty());
  44. }
  45. @Test
  46. public void repositoryWithUninitializedModule() throws IOException,
  47. ConfigInvalidException, GitAPIException {
  48. final String path = addSubmoduleToIndex();
  49. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  50. assertTrue(generator.next());
  51. assertNull(generator.getConfigUrl());
  52. assertNull(generator.getConfigUpdate());
  53. }
  54. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  55. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  56. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  57. ConfigConstants.CONFIG_KEY_PATH, path);
  58. String url = "git://server/repo.git";
  59. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  60. ConfigConstants.CONFIG_KEY_URL, url);
  61. String update = "rebase";
  62. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  63. ConfigConstants.CONFIG_KEY_UPDATE, update);
  64. modulesConfig.save();
  65. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  66. Collection<String> modules = command.call();
  67. assertNotNull(modules);
  68. assertEquals(1, modules.size());
  69. assertEquals(path, modules.iterator().next());
  70. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  71. assertTrue(generator.next());
  72. assertEquals(url, generator.getConfigUrl());
  73. assertEquals(update, generator.getConfigUpdate());
  74. }
  75. }
  76. @Test
  77. public void resolveSameLevelRelativeUrl() throws Exception {
  78. final String path = addSubmoduleToIndex();
  79. String base = "git://server/repo.git";
  80. FileBasedConfig config = db.getConfig();
  81. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  82. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  83. base);
  84. config.save();
  85. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  86. assertTrue(generator.next());
  87. assertNull(generator.getConfigUrl());
  88. assertNull(generator.getConfigUpdate());
  89. }
  90. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  91. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  92. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  93. ConfigConstants.CONFIG_KEY_PATH, path);
  94. String url = "./sub.git";
  95. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  96. ConfigConstants.CONFIG_KEY_URL, url);
  97. String update = "rebase";
  98. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  99. ConfigConstants.CONFIG_KEY_UPDATE, update);
  100. modulesConfig.save();
  101. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  102. Collection<String> modules = command.call();
  103. assertNotNull(modules);
  104. assertEquals(1, modules.size());
  105. assertEquals(path, modules.iterator().next());
  106. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  107. assertTrue(generator.next());
  108. assertEquals("git://server/repo.git/sub.git",
  109. generator.getConfigUrl());
  110. assertEquals(update, generator.getConfigUpdate());
  111. }
  112. }
  113. @Test
  114. public void resolveOneLevelHigherRelativeUrl() throws IOException,
  115. ConfigInvalidException, GitAPIException {
  116. final String path = addSubmoduleToIndex();
  117. String base = "git://server/repo.git";
  118. FileBasedConfig config = db.getConfig();
  119. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  120. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  121. base);
  122. config.save();
  123. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  124. assertTrue(generator.next());
  125. assertNull(generator.getConfigUrl());
  126. assertNull(generator.getConfigUpdate());
  127. }
  128. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  129. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  130. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  131. ConfigConstants.CONFIG_KEY_PATH, path);
  132. String url = "../sub.git";
  133. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  134. ConfigConstants.CONFIG_KEY_URL, url);
  135. String update = "rebase";
  136. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  137. ConfigConstants.CONFIG_KEY_UPDATE, update);
  138. modulesConfig.save();
  139. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  140. Collection<String> modules = command.call();
  141. assertNotNull(modules);
  142. assertEquals(1, modules.size());
  143. assertEquals(path, modules.iterator().next());
  144. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  145. assertTrue(generator.next());
  146. assertEquals("git://server/sub.git", generator.getConfigUrl());
  147. assertEquals(update, generator.getConfigUpdate());
  148. }
  149. }
  150. @Test
  151. public void resolveTwoLevelHigherRelativeUrl() throws IOException,
  152. ConfigInvalidException, GitAPIException {
  153. final String path = addSubmoduleToIndex();
  154. String base = "git://server/repo.git";
  155. FileBasedConfig config = db.getConfig();
  156. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  157. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  158. base);
  159. config.save();
  160. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  161. assertTrue(generator.next());
  162. assertNull(generator.getConfigUrl());
  163. assertNull(generator.getConfigUpdate());
  164. }
  165. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  166. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  167. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  168. ConfigConstants.CONFIG_KEY_PATH, path);
  169. String url = "../../server2/sub.git";
  170. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  171. ConfigConstants.CONFIG_KEY_URL, url);
  172. String update = "rebase";
  173. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  174. ConfigConstants.CONFIG_KEY_UPDATE, update);
  175. modulesConfig.save();
  176. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  177. Collection<String> modules = command.call();
  178. assertNotNull(modules);
  179. assertEquals(1, modules.size());
  180. assertEquals(path, modules.iterator().next());
  181. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  182. assertTrue(generator.next());
  183. assertEquals("git://server2/sub.git", generator.getConfigUrl());
  184. assertEquals(update, generator.getConfigUpdate());
  185. }
  186. }
  187. @Test
  188. public void resolveWorkingDirectoryRelativeUrl() throws IOException,
  189. GitAPIException, ConfigInvalidException {
  190. final String path = addSubmoduleToIndex();
  191. String base = db.getWorkTree().getAbsolutePath();
  192. if (File.separatorChar == '\\')
  193. base = base.replace('\\', '/');
  194. FileBasedConfig config = db.getConfig();
  195. config.unset(ConfigConstants.CONFIG_REMOTE_SECTION,
  196. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL);
  197. config.save();
  198. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  199. assertTrue(generator.next());
  200. assertNull(generator.getConfigUrl());
  201. assertNull(generator.getConfigUpdate());
  202. }
  203. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  204. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  205. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  206. ConfigConstants.CONFIG_KEY_PATH, path);
  207. String url = "./sub.git";
  208. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  209. ConfigConstants.CONFIG_KEY_URL, url);
  210. String update = "rebase";
  211. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  212. ConfigConstants.CONFIG_KEY_UPDATE, update);
  213. modulesConfig.save();
  214. SubmoduleInitCommand command = new SubmoduleInitCommand(db);
  215. Collection<String> modules = command.call();
  216. assertNotNull(modules);
  217. assertEquals(1, modules.size());
  218. assertEquals(path, modules.iterator().next());
  219. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  220. assertTrue(generator.next());
  221. assertEquals(base + "/sub.git", generator.getConfigUrl());
  222. assertEquals(update, generator.getConfigUpdate());
  223. }
  224. }
  225. @Test
  226. public void resolveInvalidParentUrl() throws IOException,
  227. ConfigInvalidException, GitAPIException {
  228. final String path = addSubmoduleToIndex();
  229. String base = "no_slash";
  230. FileBasedConfig config = db.getConfig();
  231. config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
  232. Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
  233. base);
  234. config.save();
  235. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  236. assertTrue(generator.next());
  237. assertNull(generator.getConfigUrl());
  238. assertNull(generator.getConfigUpdate());
  239. }
  240. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  241. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  242. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  243. ConfigConstants.CONFIG_KEY_PATH, path);
  244. String url = "../sub.git";
  245. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  246. ConfigConstants.CONFIG_KEY_URL, url);
  247. modulesConfig.save();
  248. try {
  249. new SubmoduleInitCommand(db).call();
  250. fail("Exception not thrown");
  251. } catch (JGitInternalException e) {
  252. assertTrue(e.getCause() instanceof IOException);
  253. }
  254. }
  255. private String addSubmoduleToIndex() throws IOException {
  256. final ObjectId id = ObjectId
  257. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  258. final String path = "sub";
  259. DirCache cache = db.lockDirCache();
  260. DirCacheEditor editor = cache.editor();
  261. editor.add(new PathEdit(path) {
  262. @Override
  263. public void apply(DirCacheEntry ent) {
  264. ent.setFileMode(FileMode.GITLINK);
  265. ent.setObjectId(id);
  266. }
  267. });
  268. editor.commit();
  269. return path;
  270. }
  271. }