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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  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. 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 testMissingPath() throws Exception {
  93. assertEquals("fatal: Argument \"path\" is required",
  94. executeUnchecked("git repo")[0]);
  95. }
  96. /**
  97. * See bug 484951: "git repo -h" should not print unexpected values
  98. *
  99. * @throws Exception
  100. */
  101. @Test
  102. public void testZombieHelpArgument() throws Exception {
  103. String[] output = execute("git repo -h");
  104. String all = Arrays.toString(output);
  105. assertTrue("Unexpected help output: " + all,
  106. all.contains("jgit repo"));
  107. assertFalse("Unexpected help output: " + all,
  108. all.contains("jgit repo VAL"));
  109. }
  110. @Test
  111. public void testAddRepoManifest() throws Exception {
  112. StringBuilder xmlContent = new StringBuilder();
  113. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  114. .append("<manifest>")
  115. .append("<remote name=\"remote1\" fetch=\".\" />")
  116. .append("<default revision=\"master\" remote=\"remote1\" />")
  117. .append("<project path=\"foo\" name=\"")
  118. .append(defaultUri)
  119. .append("\" groups=\"a,test\" />")
  120. .append("<project path=\"bar\" name=\"")
  121. .append(notDefaultUri)
  122. .append("\" groups=\"notdefault\" />")
  123. .append("<project path=\"a\" name=\"")
  124. .append(groupAUri)
  125. .append("\" groups=\"a\" />")
  126. .append("<project path=\"b\" name=\"")
  127. .append(groupBUri)
  128. .append("\" groups=\"b\" />")
  129. .append("</manifest>");
  130. writeTrashFile("manifest.xml", xmlContent.toString());
  131. StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
  132. .append(rootUri)
  133. .append("\" --groups=\"all,-a\" \"")
  134. .append(db.getWorkTree().getAbsolutePath())
  135. .append("/manifest.xml\"");
  136. execute(cmd.toString());
  137. File file = new File(db.getWorkTree(), "foo/hello.txt");
  138. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  139. file = new File(db.getWorkTree(), "bar/world.txt");
  140. assertTrue("\"all,-a\" has bar", file.exists());
  141. file = new File(db.getWorkTree(), "a/a.txt");
  142. assertFalse("\"all,-a\" doesn't have a", file.exists());
  143. file = new File(db.getWorkTree(), "b/b.txt");
  144. assertTrue("\"all,-a\" has have b", file.exists());
  145. }
  146. private void resolveRelativeUris() {
  147. // Find the longest common prefix ends with "/" as rootUri.
  148. defaultUri = defaultDb.getDirectory().toURI().toString();
  149. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  150. groupAUri = groupADb.getDirectory().toURI().toString();
  151. groupBUri = groupBDb.getDirectory().toURI().toString();
  152. int start = 0;
  153. while (start <= defaultUri.length()) {
  154. int newStart = defaultUri.indexOf('/', start + 1);
  155. String prefix = defaultUri.substring(0, newStart);
  156. if (!notDefaultUri.startsWith(prefix) ||
  157. !groupAUri.startsWith(prefix) ||
  158. !groupBUri.startsWith(prefix)) {
  159. start++;
  160. rootUri = defaultUri.substring(0, start) + "manifest";
  161. defaultUri = defaultUri.substring(start);
  162. notDefaultUri = notDefaultUri.substring(start);
  163. groupAUri = groupAUri.substring(start);
  164. groupBUri = groupBUri.substring(start);
  165. return;
  166. }
  167. start = newStart;
  168. }
  169. }
  170. }