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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 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. try {
  94. execute("git repo");
  95. fail("Must die");
  96. } catch (Die e) {
  97. // expected, requires argument
  98. }
  99. }
  100. /**
  101. * See bug 484951: "git repo -h" should not print unexpected values
  102. *
  103. * @throws Exception
  104. */
  105. @Test
  106. public void testZombieHelpArgument() throws Exception {
  107. String[] output = execute("git repo -h");
  108. String all = Arrays.toString(output);
  109. assertTrue("Unexpected help output: " + all,
  110. all.contains("jgit repo"));
  111. assertFalse("Unexpected help output: " + all,
  112. all.contains("jgit repo VAL"));
  113. }
  114. @Test
  115. public void testAddRepoManifest() 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. writeTrashFile("manifest.xml", xmlContent.toString());
  135. StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
  136. .append(rootUri)
  137. .append("\" --groups=\"all,-a\" \"")
  138. .append(db.getWorkTree().getAbsolutePath())
  139. .append("/manifest.xml\"");
  140. execute(cmd.toString());
  141. File file = new File(db.getWorkTree(), "foo/hello.txt");
  142. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  143. file = new File(db.getWorkTree(), "bar/world.txt");
  144. assertTrue("\"all,-a\" has bar", file.exists());
  145. file = new File(db.getWorkTree(), "a/a.txt");
  146. assertFalse("\"all,-a\" doesn't have a", file.exists());
  147. file = new File(db.getWorkTree(), "b/b.txt");
  148. assertTrue("\"all,-a\" has have b", file.exists());
  149. }
  150. private void resolveRelativeUris() {
  151. // Find the longest common prefix ends with "/" as rootUri.
  152. defaultUri = defaultDb.getDirectory().toURI().toString();
  153. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  154. groupAUri = groupADb.getDirectory().toURI().toString();
  155. groupBUri = groupBDb.getDirectory().toURI().toString();
  156. int start = 0;
  157. while (start <= defaultUri.length()) {
  158. int newStart = defaultUri.indexOf('/', start + 1);
  159. String prefix = defaultUri.substring(0, newStart);
  160. if (!notDefaultUri.startsWith(prefix) ||
  161. !groupAUri.startsWith(prefix) ||
  162. !groupBUri.startsWith(prefix)) {
  163. start++;
  164. rootUri = defaultUri.substring(0, start) + "manifest";
  165. defaultUri = defaultUri.substring(start);
  166. notDefaultUri = notDefaultUri.substring(start);
  167. groupAUri = groupAUri.substring(start);
  168. groupBUri = groupBUri.substring(start);
  169. return;
  170. }
  171. start = newStart;
  172. }
  173. }
  174. }