Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SubmoduleAddTest.java 12KB

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