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.

RepoCommandTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2014, Google 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.gitrepo;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.BufferedReader;
  48. import java.io.File;
  49. import java.io.FileReader;
  50. import org.eclipse.jgit.api.CloneCommand;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.junit.JGitTestUtil;
  53. import org.eclipse.jgit.junit.RepositoryTestCase;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.junit.Test;
  57. public class RepoCommandTest extends RepositoryTestCase {
  58. private Repository defaultDb;
  59. private Repository notDefaultDb;
  60. private Repository groupADb;
  61. private Repository groupBDb;
  62. private String rootUri;
  63. private String defaultUri;
  64. private String notDefaultUri;
  65. private String groupAUri;
  66. private String groupBUri;
  67. public void setUp() throws Exception {
  68. super.setUp();
  69. defaultDb = createWorkRepository();
  70. Git git = new Git(defaultDb);
  71. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world");
  72. git.add().addFilepattern("hello.txt").call();
  73. git.commit().setMessage("Initial commit").call();
  74. notDefaultDb = createWorkRepository();
  75. git = new Git(notDefaultDb);
  76. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  77. git.add().addFilepattern("world.txt").call();
  78. git.commit().setMessage("Initial commit").call();
  79. groupADb = createWorkRepository();
  80. git = new Git(groupADb);
  81. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  82. git.add().addFilepattern("a.txt").call();
  83. git.commit().setMessage("Initial commit").call();
  84. groupBDb = createWorkRepository();
  85. git = new Git(groupBDb);
  86. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  87. git.add().addFilepattern("b.txt").call();
  88. git.commit().setMessage("Initial commit").call();
  89. resolveRelativeUris();
  90. }
  91. @Test
  92. public void testAddRepoManifest() throws Exception {
  93. StringBuilder xmlContent = new StringBuilder();
  94. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  95. .append("<manifest>")
  96. .append("<remote name=\"remote1\" fetch=\".\" />")
  97. .append("<default revision=\"master\" remote=\"remote1\" />")
  98. .append("<project path=\"foo\" name=\"")
  99. .append(defaultUri)
  100. .append("\" />")
  101. .append("</manifest>");
  102. writeTrashFile("manifest.xml", xmlContent.toString());
  103. RepoCommand command = new RepoCommand(db);
  104. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  105. .setURI(rootUri)
  106. .call();
  107. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  108. assertTrue("submodule was checked out", hello.exists());
  109. BufferedReader reader = new BufferedReader(new FileReader(hello));
  110. String content = reader.readLine();
  111. reader.close();
  112. assertEquals("submodule content is as expected.", "world", content);
  113. }
  114. @Test
  115. public void testRepoManifestGroups() throws Exception {
  116. StringBuilder xmlContent = new StringBuilder();
  117. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  118. .append("<manifest>")
  119. .append("<remote name=\"remote1\" fetch=\".\" />")
  120. .append("<default revision=\"master\" remote=\"remote1\" />")
  121. .append("<project path=\"foo\" name=\"")
  122. .append(defaultUri)
  123. .append("\" groups=\"a,test\" />")
  124. .append("<project path=\"bar\" name=\"")
  125. .append(notDefaultUri)
  126. .append("\" groups=\"notdefault\" />")
  127. .append("<project path=\"a\" name=\"")
  128. .append(groupAUri)
  129. .append("\" groups=\"a\" />")
  130. .append("<project path=\"b\" name=\"")
  131. .append(groupBUri)
  132. .append("\" groups=\"b\" />")
  133. .append("</manifest>");
  134. // default should have foo, a & b
  135. Repository localDb = createWorkRepository();
  136. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  137. RepoCommand command = new RepoCommand(localDb);
  138. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  139. .setURI(rootUri)
  140. .call();
  141. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  142. assertTrue("default has foo", file.exists());
  143. file = new File(localDb.getWorkTree(), "bar/world.txt");
  144. assertFalse("default doesn't have bar", file.exists());
  145. file = new File(localDb.getWorkTree(), "a/a.txt");
  146. assertTrue("default has a", file.exists());
  147. file = new File(localDb.getWorkTree(), "b/b.txt");
  148. assertTrue("default has b", file.exists());
  149. // all,-a should have bar & b
  150. localDb = createWorkRepository();
  151. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  152. command = new RepoCommand(localDb);
  153. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  154. .setURI(rootUri)
  155. .setGroups("all,-a")
  156. .call();
  157. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  158. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  159. file = new File(localDb.getWorkTree(), "bar/world.txt");
  160. assertTrue("\"all,-a\" has bar", file.exists());
  161. file = new File(localDb.getWorkTree(), "a/a.txt");
  162. assertFalse("\"all,-a\" doesn't have a", file.exists());
  163. file = new File(localDb.getWorkTree(), "b/b.txt");
  164. assertTrue("\"all,-a\" has have b", file.exists());
  165. }
  166. @Test
  167. public void testRepoManifestCopyfile() throws Exception {
  168. Repository localDb = createWorkRepository();
  169. StringBuilder xmlContent = new StringBuilder();
  170. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  171. .append("<manifest>")
  172. .append("<remote name=\"remote1\" fetch=\".\" />")
  173. .append("<default revision=\"master\" remote=\"remote1\" />")
  174. .append("<project path=\"foo\" name=\"")
  175. .append(defaultUri)
  176. .append("\">")
  177. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  178. .append("</project>")
  179. .append("</manifest>");
  180. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  181. RepoCommand command = new RepoCommand(localDb);
  182. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  183. .setURI(rootUri)
  184. .call();
  185. // The original file should exist
  186. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  187. assertTrue("The original file exists", hello.exists());
  188. BufferedReader reader = new BufferedReader(new FileReader(hello));
  189. String content = reader.readLine();
  190. reader.close();
  191. assertEquals("The original file has expected content", "world", content);
  192. // The dest file should also exist
  193. hello = new File(localDb.getWorkTree(), "Hello");
  194. assertTrue("The destination file exists", hello.exists());
  195. reader = new BufferedReader(new FileReader(hello));
  196. content = reader.readLine();
  197. reader.close();
  198. assertEquals("The destination file has expected content", "world", content);
  199. }
  200. @Test
  201. public void testBareRepo() throws Exception {
  202. Repository remoteDb = createBareRepository();
  203. Repository tempDb = createWorkRepository();
  204. StringBuilder xmlContent = new StringBuilder();
  205. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  206. .append("<manifest>")
  207. .append("<remote name=\"remote1\" fetch=\".\" />")
  208. .append("<default revision=\"master\" remote=\"remote1\" />")
  209. .append("<project path=\"foo\" name=\"")
  210. .append(defaultUri)
  211. .append("\" />")
  212. .append("</manifest>");
  213. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml", xmlContent.toString());
  214. RepoCommand command = new RepoCommand(remoteDb);
  215. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  216. .setURI(rootUri)
  217. .call();
  218. // Clone it
  219. File directory = createTempDirectory("testBareRepo");
  220. CloneCommand clone = Git.cloneRepository();
  221. clone.setDirectory(directory);
  222. clone.setURI(remoteDb.getDirectory().toURI().toString());
  223. Repository localDb = clone.call().getRepository();
  224. // The .gitmodules file should exist
  225. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  226. assertTrue("The .gitmodules file exists", gitmodules.exists());
  227. // The first line of .gitmodules file should be expected
  228. BufferedReader reader = new BufferedReader(new FileReader(gitmodules));
  229. String content = reader.readLine();
  230. reader.close();
  231. assertEquals("The first line of .gitmodules file is as expected.",
  232. "[submodule \"foo\"]", content);
  233. // The gitlink should be the same of remote head sha1
  234. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  235. String remote = defaultDb.resolve(Constants.HEAD).name();
  236. assertEquals("The gitlink is same as remote head", remote, gitlink);
  237. }
  238. private void resolveRelativeUris() {
  239. // Find the longest common prefix ends with "/" as rootUri.
  240. defaultUri = defaultDb.getDirectory().toURI().toString();
  241. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  242. groupAUri = groupADb.getDirectory().toURI().toString();
  243. groupBUri = groupBDb.getDirectory().toURI().toString();
  244. int start = 0;
  245. while (start <= defaultUri.length()) {
  246. int newStart = defaultUri.indexOf('/', start + 1);
  247. String prefix = defaultUri.substring(0, newStart);
  248. if (!notDefaultUri.startsWith(prefix) ||
  249. !groupAUri.startsWith(prefix) ||
  250. !groupBUri.startsWith(prefix)) {
  251. start++;
  252. rootUri = defaultUri.substring(0, start);
  253. defaultUri = defaultUri.substring(start);
  254. notDefaultUri = notDefaultUri.substring(start);
  255. groupAUri = groupAUri.substring(start);
  256. groupBUri = groupBUri.substring(start);
  257. return;
  258. }
  259. start = newStart;
  260. }
  261. }
  262. }