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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Git;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.Repository;
  54. import org.junit.Test;
  55. public class RepoCommandTest extends RepositoryTestCase {
  56. private Repository defaultDb;
  57. private Repository notDefaultDb;
  58. private Repository groupADb;
  59. private Repository groupBDb;
  60. private String rootUri;
  61. private String defaultUri;
  62. private String notDefaultUri;
  63. private String groupAUri;
  64. private String groupBUri;
  65. public void setUp() throws Exception {
  66. super.setUp();
  67. defaultDb = createWorkRepository();
  68. Git git = new Git(defaultDb);
  69. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world");
  70. git.add().addFilepattern("hello.txt").call();
  71. git.commit().setMessage("Initial commit").call();
  72. notDefaultDb = createWorkRepository();
  73. git = new Git(notDefaultDb);
  74. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  75. git.add().addFilepattern("world.txt").call();
  76. git.commit().setMessage("Initial commit").call();
  77. groupADb = createWorkRepository();
  78. git = new Git(groupADb);
  79. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  80. git.add().addFilepattern("a.txt").call();
  81. git.commit().setMessage("Initial commit").call();
  82. groupBDb = createWorkRepository();
  83. git = new Git(groupBDb);
  84. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  85. git.add().addFilepattern("b.txt").call();
  86. git.commit().setMessage("Initial commit").call();
  87. resolveRelativeUris();
  88. }
  89. @Test
  90. public void testAddRepoManifest() throws Exception {
  91. StringBuilder xmlContent = new StringBuilder();
  92. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  93. .append("<manifest>")
  94. .append("<remote name=\"remote1\" fetch=\".\" />")
  95. .append("<default revision=\"master\" remote=\"remote1\" />")
  96. .append("<project path=\"foo\" name=\"")
  97. .append(defaultUri)
  98. .append("\" />")
  99. .append("</manifest>");
  100. writeTrashFile("manifest.xml", xmlContent.toString());
  101. RepoCommand command = new RepoCommand(db);
  102. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  103. .setURI(rootUri)
  104. .call();
  105. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  106. assertTrue("submodule was checked out", hello.exists());
  107. BufferedReader reader = new BufferedReader(new FileReader(hello));
  108. String content = reader.readLine();
  109. reader.close();
  110. assertEquals("submodule content is as expected.", "world", content);
  111. }
  112. @Test
  113. public void testRepoManifestGroups() throws Exception {
  114. StringBuilder xmlContent = new StringBuilder();
  115. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  116. .append("<manifest>")
  117. .append("<remote name=\"remote1\" fetch=\".\" />")
  118. .append("<default revision=\"master\" remote=\"remote1\" />")
  119. .append("<project path=\"foo\" name=\"")
  120. .append(defaultUri)
  121. .append("\" groups=\"a,test\" />")
  122. .append("<project path=\"bar\" name=\"")
  123. .append(notDefaultUri)
  124. .append("\" groups=\"notdefault\" />")
  125. .append("<project path=\"a\" name=\"")
  126. .append(groupAUri)
  127. .append("\" groups=\"a\" />")
  128. .append("<project path=\"b\" name=\"")
  129. .append(groupBUri)
  130. .append("\" groups=\"b\" />")
  131. .append("</manifest>");
  132. // default should have foo, a & b
  133. Repository localDb = createWorkRepository();
  134. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  135. RepoCommand command = new RepoCommand(localDb);
  136. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  137. .setURI(rootUri)
  138. .call();
  139. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  140. assertTrue("default has foo", file.exists());
  141. file = new File(localDb.getWorkTree(), "bar/world.txt");
  142. assertFalse("default doesn't have bar", file.exists());
  143. file = new File(localDb.getWorkTree(), "a/a.txt");
  144. assertTrue("default has a", file.exists());
  145. file = new File(localDb.getWorkTree(), "b/b.txt");
  146. assertTrue("default has b", file.exists());
  147. // all,-a should have bar & b
  148. localDb = createWorkRepository();
  149. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  150. command = new RepoCommand(localDb);
  151. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  152. .setURI(rootUri)
  153. .setGroups("all,-a")
  154. .call();
  155. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  156. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  157. file = new File(localDb.getWorkTree(), "bar/world.txt");
  158. assertTrue("\"all,-a\" has bar", file.exists());
  159. file = new File(localDb.getWorkTree(), "a/a.txt");
  160. assertFalse("\"all,-a\" doesn't have a", file.exists());
  161. file = new File(localDb.getWorkTree(), "b/b.txt");
  162. assertTrue("\"all,-a\" has have b", file.exists());
  163. }
  164. @Test
  165. public void testRepoManifestCopyfile() throws Exception {
  166. Repository localDb = createWorkRepository();
  167. StringBuilder xmlContent = new StringBuilder();
  168. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  169. .append("<manifest>")
  170. .append("<remote name=\"remote1\" fetch=\".\" />")
  171. .append("<default revision=\"master\" remote=\"remote1\" />")
  172. .append("<project path=\"foo\" name=\"")
  173. .append(defaultUri)
  174. .append("\">")
  175. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  176. .append("</project>")
  177. .append("</manifest>");
  178. JGitTestUtil.writeTrashFile(localDb, "manifest.xml", xmlContent.toString());
  179. RepoCommand command = new RepoCommand(localDb);
  180. command.setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  181. .setURI(rootUri)
  182. .call();
  183. // The original file should exist
  184. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  185. assertTrue("The original file exists", hello.exists());
  186. BufferedReader reader = new BufferedReader(new FileReader(hello));
  187. String content = reader.readLine();
  188. reader.close();
  189. assertEquals("The original file has expected content", "world", content);
  190. // The dest file should also exist
  191. hello = new File(localDb.getWorkTree(), "Hello");
  192. assertTrue("The destination file exists", hello.exists());
  193. reader = new BufferedReader(new FileReader(hello));
  194. content = reader.readLine();
  195. reader.close();
  196. assertEquals("The destination file has expected content", "world", content);
  197. }
  198. private void resolveRelativeUris() {
  199. // Find the longest common prefix ends with "/" as rootUri.
  200. defaultUri = defaultDb.getDirectory().toURI().toString();
  201. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  202. groupAUri = groupADb.getDirectory().toURI().toString();
  203. groupBUri = groupBDb.getDirectory().toURI().toString();
  204. int start = 0;
  205. while (start <= defaultUri.length()) {
  206. int newStart = defaultUri.indexOf('/', start + 1);
  207. String prefix = defaultUri.substring(0, newStart);
  208. if (!notDefaultUri.startsWith(prefix) ||
  209. !groupAUri.startsWith(prefix) ||
  210. !groupBUri.startsWith(prefix)) {
  211. start++;
  212. rootUri = defaultUri.substring(0, start);
  213. defaultUri = defaultUri.substring(start);
  214. notDefaultUri = notDefaultUri.substring(start);
  215. groupAUri = groupAUri.substring(start);
  216. groupBUri = groupBUri.substring(start);
  217. return;
  218. }
  219. start = newStart;
  220. }
  221. }
  222. }