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.

RepoTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.pgm;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import java.io.File;
  48. import java.util.Arrays;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.junit.JGitTestUtil;
  51. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.junit.Before;
  54. import org.junit.Test;
  55. public class RepoTest extends CLIRepositoryTestCase {
  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. @Override
  66. @Before
  67. public void setUp() throws Exception {
  68. super.setUp();
  69. defaultDb = createWorkRepository();
  70. try (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. }
  75. notDefaultDb = createWorkRepository();
  76. try (Git git = new Git(notDefaultDb)) {
  77. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  78. git.add().addFilepattern("world.txt").call();
  79. git.commit().setMessage("Initial commit").call();
  80. }
  81. groupADb = createWorkRepository();
  82. try (Git git = new Git(groupADb)) {
  83. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  84. git.add().addFilepattern("a.txt").call();
  85. git.commit().setMessage("Initial commit").call();
  86. }
  87. groupBDb = createWorkRepository();
  88. try (Git git = new Git(groupBDb)) {
  89. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  90. git.add().addFilepattern("b.txt").call();
  91. git.commit().setMessage("Initial commit").call();
  92. }
  93. resolveRelativeUris();
  94. }
  95. @Test
  96. public void testMissingPath() throws Exception {
  97. try {
  98. execute("git repo");
  99. fail("Must die");
  100. } catch (Die e) {
  101. // expected, requires argument
  102. }
  103. }
  104. /**
  105. * See bug 484951: "git repo -h" should not print unexpected values
  106. *
  107. * @throws Exception
  108. */
  109. @Test
  110. public void testZombieHelpArgument() throws Exception {
  111. String[] output = execute("git repo -h");
  112. String all = Arrays.toString(output);
  113. assertTrue("Unexpected help output: " + all,
  114. all.contains("jgit repo"));
  115. assertFalse("Unexpected help output: " + all,
  116. all.contains("jgit repo VAL"));
  117. }
  118. @Test
  119. public void testAddRepoManifest() throws Exception {
  120. StringBuilder xmlContent = new StringBuilder();
  121. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  122. .append("<manifest>")
  123. .append("<remote name=\"remote1\" fetch=\".\" />")
  124. .append("<default revision=\"master\" remote=\"remote1\" />")
  125. .append("<project path=\"foo\" name=\"")
  126. .append(defaultUri)
  127. .append("\" groups=\"a,test\" />")
  128. .append("<project path=\"bar\" name=\"")
  129. .append(notDefaultUri)
  130. .append("\" groups=\"notdefault\" />")
  131. .append("<project path=\"a\" name=\"")
  132. .append(groupAUri)
  133. .append("\" groups=\"a\" />")
  134. .append("<project path=\"b\" name=\"")
  135. .append(groupBUri)
  136. .append("\" groups=\"b\" />")
  137. .append("</manifest>");
  138. writeTrashFile("manifest.xml", xmlContent.toString());
  139. StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
  140. .append(rootUri)
  141. .append("\" --groups=\"all,-a\" \"")
  142. .append(db.getWorkTree().getAbsolutePath())
  143. .append("/manifest.xml\"");
  144. execute(cmd.toString());
  145. File file = new File(db.getWorkTree(), "foo/hello.txt");
  146. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  147. file = new File(db.getWorkTree(), "bar/world.txt");
  148. assertTrue("\"all,-a\" has bar", file.exists());
  149. file = new File(db.getWorkTree(), "a/a.txt");
  150. assertFalse("\"all,-a\" doesn't have a", file.exists());
  151. file = new File(db.getWorkTree(), "b/b.txt");
  152. assertTrue("\"all,-a\" has have b", file.exists());
  153. }
  154. private void resolveRelativeUris() {
  155. // Find the longest common prefix ends with "/" as rootUri.
  156. defaultUri = defaultDb.getDirectory().toURI().toString();
  157. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  158. groupAUri = groupADb.getDirectory().toURI().toString();
  159. groupBUri = groupBDb.getDirectory().toURI().toString();
  160. int start = 0;
  161. while (start <= defaultUri.length()) {
  162. int newStart = defaultUri.indexOf('/', start + 1);
  163. String prefix = defaultUri.substring(0, newStart);
  164. if (!notDefaultUri.startsWith(prefix) ||
  165. !groupAUri.startsWith(prefix) ||
  166. !groupBUri.startsWith(prefix)) {
  167. start++;
  168. rootUri = defaultUri.substring(0, start) + "manifest";
  169. defaultUri = defaultUri.substring(start);
  170. notDefaultUri = notDefaultUri.substring(start);
  171. groupAUri = groupAUri.substring(start);
  172. groupBUri = groupBUri.substring(start);
  173. return;
  174. }
  175. start = newStart;
  176. }
  177. }
  178. }