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.

SubmoduleAddTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.File;
  16. import java.text.MessageFormat;
  17. import org.eclipse.jgit.api.Git;
  18. import org.eclipse.jgit.api.Status;
  19. import org.eclipse.jgit.api.SubmoduleAddCommand;
  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.internal.JGitText;
  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.lib.Repository;
  33. import org.eclipse.jgit.revwalk.RevCommit;
  34. import org.eclipse.jgit.storage.file.FileBasedConfig;
  35. import org.junit.Test;
  36. /**
  37. * Unit tests of {@link org.eclipse.jgit.api.SubmoduleAddCommand}
  38. */
  39. public class SubmoduleAddTest extends RepositoryTestCase {
  40. @Test
  41. public void commandWithNullPath() throws GitAPIException {
  42. try {
  43. new SubmoduleAddCommand(db).setURI("uri").call().close();
  44. fail("Exception not thrown");
  45. } catch (IllegalArgumentException e) {
  46. assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
  47. }
  48. }
  49. @Test
  50. public void commandWithEmptyPath() throws GitAPIException {
  51. try {
  52. new SubmoduleAddCommand(db).setPath("").setURI("uri").call()
  53. .close();
  54. fail("Exception not thrown");
  55. } catch (IllegalArgumentException e) {
  56. assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
  57. }
  58. }
  59. @Test
  60. public void commandWithNullUri() throws GitAPIException {
  61. try {
  62. new SubmoduleAddCommand(db).setPath("sub").call().close();
  63. fail("Exception not thrown");
  64. } catch (IllegalArgumentException e) {
  65. assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
  66. }
  67. }
  68. @Test
  69. public void commandWithEmptyUri() throws GitAPIException {
  70. try {
  71. new SubmoduleAddCommand(db).setPath("sub").setURI("").call()
  72. .close();
  73. fail("Exception not thrown");
  74. } catch (IllegalArgumentException e) {
  75. assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
  76. }
  77. }
  78. @Test
  79. public void addSubmodule() throws Exception {
  80. try (Git git = new Git(db)) {
  81. writeTrashFile("file.txt", "content");
  82. git.add().addFilepattern("file.txt").call();
  83. RevCommit commit = git.commit().setMessage("create file").call();
  84. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  85. String path = "sub";
  86. command.setPath(path);
  87. String uri = db.getDirectory().toURI().toString();
  88. command.setURI(uri);
  89. ObjectId subCommit;
  90. try (Repository repo = command.call()) {
  91. assertNotNull(repo);
  92. subCommit = repo.resolve(Constants.HEAD);
  93. }
  94. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  95. generator.loadModulesConfig();
  96. assertTrue(generator.next());
  97. assertEquals(path, generator.getModuleName());
  98. assertEquals(path, generator.getPath());
  99. assertEquals(commit, generator.getObjectId());
  100. assertEquals(uri, generator.getModulesUrl());
  101. assertEquals(path, generator.getModulesPath());
  102. assertEquals(uri, generator.getConfigUrl());
  103. try (Repository subModRepo = generator.getRepository()) {
  104. assertNotNull(subModRepo);
  105. assertEquals(subCommit, commit);
  106. }
  107. }
  108. Status status = Git.wrap(db).status().call();
  109. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  110. assertTrue(status.getAdded().contains(path));
  111. }
  112. }
  113. @Test
  114. public void addSubmoduleWithName() throws Exception {
  115. try (Git git = new Git(db)) {
  116. writeTrashFile("file.txt", "content");
  117. git.add().addFilepattern("file.txt").call();
  118. RevCommit commit = git.commit().setMessage("create file").call();
  119. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  120. String name = "testsub";
  121. command.setName(name);
  122. String path = "sub";
  123. command.setPath(path);
  124. String uri = db.getDirectory().toURI().toString();
  125. command.setURI(uri);
  126. ObjectId subCommit;
  127. try (Repository repo = command.call()) {
  128. assertNotNull(repo);
  129. subCommit = repo.resolve(Constants.HEAD);
  130. }
  131. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  132. generator.loadModulesConfig();
  133. assertTrue(generator.next());
  134. assertEquals(name, generator.getModuleName());
  135. assertEquals(path, generator.getPath());
  136. assertEquals(commit, generator.getObjectId());
  137. assertEquals(uri, generator.getModulesUrl());
  138. assertEquals(path, generator.getModulesPath());
  139. assertEquals(uri, generator.getConfigUrl());
  140. try (Repository subModRepo = generator.getRepository()) {
  141. assertNotNull(subModRepo);
  142. assertEquals(subCommit, commit);
  143. }
  144. }
  145. Status status = Git.wrap(db).status().call();
  146. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  147. assertTrue(status.getAdded().contains(path));
  148. }
  149. }
  150. @Test
  151. public void addExistentSubmodule() throws Exception {
  152. final ObjectId id = ObjectId
  153. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  154. final String path = "sub";
  155. DirCache cache = db.lockDirCache();
  156. DirCacheEditor editor = cache.editor();
  157. editor.add(new PathEdit(path) {
  158. @Override
  159. public void apply(DirCacheEntry ent) {
  160. ent.setFileMode(FileMode.GITLINK);
  161. ent.setObjectId(id);
  162. }
  163. });
  164. editor.commit();
  165. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  166. command.setPath(path);
  167. command.setURI("git://server/repo.git");
  168. try {
  169. command.call().close();
  170. fail("Exception not thrown");
  171. } catch (JGitInternalException e) {
  172. assertEquals(
  173. MessageFormat.format(JGitText.get().submoduleExists, path),
  174. e.getMessage());
  175. }
  176. }
  177. @Test
  178. public void addSubmoduleWithInvalidPath() throws Exception {
  179. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  180. command.setPath("-invalid-path");
  181. command.setName("sub");
  182. command.setURI("http://example.com/repo/x.git");
  183. try {
  184. command.call().close();
  185. fail("Exception not thrown");
  186. } catch (IllegalArgumentException e) {
  187. assertEquals("Invalid submodule path '-invalid-path'",
  188. e.getMessage());
  189. }
  190. }
  191. @Test
  192. public void addSubmoduleWithInvalidUri() throws Exception {
  193. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  194. command.setPath("valid-path");
  195. command.setURI("-upstream");
  196. try {
  197. command.call().close();
  198. fail("Exception not thrown");
  199. } catch (IllegalArgumentException e) {
  200. assertEquals("Invalid submodule URL '-upstream'", e.getMessage());
  201. }
  202. }
  203. @Test
  204. public void addSubmoduleWithRelativeUri() throws Exception {
  205. try (Git git = new Git(db)) {
  206. writeTrashFile("file.txt", "content");
  207. git.add().addFilepattern("file.txt").call();
  208. RevCommit commit = git.commit().setMessage("create file").call();
  209. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  210. String path = "sub";
  211. String uri = "./.git";
  212. command.setPath(path);
  213. command.setURI(uri);
  214. Repository repo = command.call();
  215. assertNotNull(repo);
  216. addRepoToClose(repo);
  217. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
  218. assertTrue(generator.next());
  219. assertEquals(path, generator.getPath());
  220. assertEquals(commit, generator.getObjectId());
  221. assertEquals(uri, generator.getModulesUrl());
  222. assertEquals(path, generator.getModulesPath());
  223. String fullUri = db.getDirectory().getAbsolutePath();
  224. if (File.separatorChar == '\\') {
  225. fullUri = fullUri.replace('\\', '/');
  226. }
  227. assertEquals(fullUri, generator.getConfigUrl());
  228. try (Repository subModRepo = generator.getRepository()) {
  229. assertNotNull(subModRepo);
  230. assertEquals(fullUri,
  231. subModRepo.getConfig().getString(
  232. ConfigConstants.CONFIG_REMOTE_SECTION,
  233. Constants.DEFAULT_REMOTE_NAME,
  234. ConfigConstants.CONFIG_KEY_URL));
  235. }
  236. }
  237. assertEquals(commit, repo.resolve(Constants.HEAD));
  238. Status status = Git.wrap(db).status().call();
  239. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  240. assertTrue(status.getAdded().contains(path));
  241. }
  242. }
  243. @Test
  244. public void addSubmoduleWithExistingSubmoduleDefined() throws Exception {
  245. String path1 = "sub1";
  246. String url1 = "git://server/repo1.git";
  247. String path2 = "sub2";
  248. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  249. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  250. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  251. path1, ConfigConstants.CONFIG_KEY_PATH, path1);
  252. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  253. path1, ConfigConstants.CONFIG_KEY_URL, url1);
  254. modulesConfig.save();
  255. try (Git git = new Git(db)) {
  256. writeTrashFile("file.txt", "content");
  257. git.add().addFilepattern("file.txt").call();
  258. assertNotNull(git.commit().setMessage("create file").call());
  259. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  260. command.setPath(path2);
  261. String url2 = db.getDirectory().toURI().toString();
  262. command.setURI(url2);
  263. Repository r = command.call();
  264. assertNotNull(r);
  265. addRepoToClose(r);
  266. modulesConfig.load();
  267. assertEquals(path1, modulesConfig.getString(
  268. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  269. ConfigConstants.CONFIG_KEY_PATH));
  270. assertEquals(url1, modulesConfig.getString(
  271. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  272. ConfigConstants.CONFIG_KEY_URL));
  273. assertEquals(path2, modulesConfig.getString(
  274. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  275. ConfigConstants.CONFIG_KEY_PATH));
  276. assertEquals(url2, modulesConfig.getString(
  277. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  278. ConfigConstants.CONFIG_KEY_URL));
  279. }
  280. }
  281. @Test
  282. public void denySubmoduleWithDotDot() throws Exception {
  283. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  284. command.setName("dir/../");
  285. command.setPath("sub");
  286. command.setURI(db.getDirectory().toURI().toString());
  287. try {
  288. command.call();
  289. fail();
  290. } catch (IllegalArgumentException e) {
  291. // Expected
  292. }
  293. }
  294. }