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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. Repository repo = command.call();
  123. assertNotNull(repo);
  124. ObjectId subCommit = repo.resolve(Constants.HEAD);
  125. repo.close();
  126. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  127. assertTrue(generator.next());
  128. assertEquals(path, generator.getPath());
  129. assertEquals(commit, generator.getObjectId());
  130. assertEquals(uri, generator.getModulesUrl());
  131. assertEquals(path, generator.getModulesPath());
  132. assertEquals(uri, generator.getConfigUrl());
  133. Repository subModRepo = generator.getRepository();
  134. assertNotNull(subModRepo);
  135. assertEquals(subCommit, commit);
  136. subModRepo.close();
  137. Status status = Git.wrap(db).status().call();
  138. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  139. assertTrue(status.getAdded().contains(path));
  140. }
  141. }
  142. @Test
  143. public void addExistentSubmodule() throws Exception {
  144. final ObjectId id = ObjectId
  145. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  146. final String path = "sub";
  147. DirCache cache = db.lockDirCache();
  148. DirCacheEditor editor = cache.editor();
  149. editor.add(new PathEdit(path) {
  150. public void apply(DirCacheEntry ent) {
  151. ent.setFileMode(FileMode.GITLINK);
  152. ent.setObjectId(id);
  153. }
  154. });
  155. editor.commit();
  156. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  157. command.setPath(path);
  158. command.setURI("git://server/repo.git");
  159. try {
  160. command.call().close();
  161. fail("Exception not thrown");
  162. } catch (JGitInternalException e) {
  163. assertEquals(
  164. MessageFormat.format(JGitText.get().submoduleExists, path),
  165. e.getMessage());
  166. }
  167. }
  168. @Test
  169. public void addSubmoduleWithRelativeUri() throws Exception {
  170. try (Git git = new Git(db)) {
  171. writeTrashFile("file.txt", "content");
  172. git.add().addFilepattern("file.txt").call();
  173. RevCommit commit = git.commit().setMessage("create file").call();
  174. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  175. String path = "sub";
  176. String uri = "./.git";
  177. command.setPath(path);
  178. command.setURI(uri);
  179. Repository repo = command.call();
  180. assertNotNull(repo);
  181. addRepoToClose(repo);
  182. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  183. assertTrue(generator.next());
  184. assertEquals(path, generator.getPath());
  185. assertEquals(commit, generator.getObjectId());
  186. assertEquals(uri, generator.getModulesUrl());
  187. assertEquals(path, generator.getModulesPath());
  188. String fullUri = db.getDirectory().getAbsolutePath();
  189. if (File.separatorChar == '\\') {
  190. fullUri = fullUri.replace('\\', '/');
  191. }
  192. assertEquals(fullUri, generator.getConfigUrl());
  193. Repository subModRepo = generator.getRepository();
  194. assertNotNull(subModRepo);
  195. assertEquals(
  196. fullUri,
  197. subModRepo
  198. .getConfig()
  199. .getString(ConfigConstants.CONFIG_REMOTE_SECTION,
  200. Constants.DEFAULT_REMOTE_NAME,
  201. ConfigConstants.CONFIG_KEY_URL));
  202. subModRepo.close();
  203. assertEquals(commit, repo.resolve(Constants.HEAD));
  204. Status status = Git.wrap(db).status().call();
  205. assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
  206. assertTrue(status.getAdded().contains(path));
  207. }
  208. }
  209. @Test
  210. public void addSubmoduleWithExistingSubmoduleDefined() throws Exception {
  211. String path1 = "sub1";
  212. String url1 = "git://server/repo1.git";
  213. String path2 = "sub2";
  214. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  215. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  216. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  217. path1, ConfigConstants.CONFIG_KEY_PATH, path1);
  218. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  219. path1, ConfigConstants.CONFIG_KEY_URL, url1);
  220. modulesConfig.save();
  221. try (Git git = new Git(db)) {
  222. writeTrashFile("file.txt", "content");
  223. git.add().addFilepattern("file.txt").call();
  224. assertNotNull(git.commit().setMessage("create file").call());
  225. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  226. command.setPath(path2);
  227. String url2 = db.getDirectory().toURI().toString();
  228. command.setURI(url2);
  229. Repository r = command.call();
  230. assertNotNull(r);
  231. addRepoToClose(r);
  232. modulesConfig.load();
  233. assertEquals(path1, modulesConfig.getString(
  234. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  235. ConfigConstants.CONFIG_KEY_PATH));
  236. assertEquals(url1, modulesConfig.getString(
  237. ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
  238. ConfigConstants.CONFIG_KEY_URL));
  239. assertEquals(path2, modulesConfig.getString(
  240. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  241. ConfigConstants.CONFIG_KEY_PATH));
  242. assertEquals(url2, modulesConfig.getString(
  243. ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
  244. ConfigConstants.CONFIG_KEY_URL));
  245. }
  246. }
  247. }