您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2014 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import static org.junit.Assert.assertFalse;
  12. import static org.junit.Assert.assertTrue;
  13. import static org.junit.Assert.fail;
  14. import java.io.File;
  15. import java.util.Arrays;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.junit.JGitTestUtil;
  18. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. public class RepoTest extends CLIRepositoryTestCase {
  23. private Repository defaultDb;
  24. private Repository notDefaultDb;
  25. private Repository groupADb;
  26. private Repository groupBDb;
  27. private String rootUri;
  28. private String defaultUri;
  29. private String notDefaultUri;
  30. private String groupAUri;
  31. private String groupBUri;
  32. @Override
  33. @Before
  34. public void setUp() throws Exception {
  35. super.setUp();
  36. defaultDb = createWorkRepository();
  37. try (Git git = new Git(defaultDb)) {
  38. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "world");
  39. git.add().addFilepattern("hello.txt").call();
  40. git.commit().setMessage("Initial commit").call();
  41. }
  42. notDefaultDb = createWorkRepository();
  43. try (Git git = new Git(notDefaultDb)) {
  44. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  45. git.add().addFilepattern("world.txt").call();
  46. git.commit().setMessage("Initial commit").call();
  47. }
  48. groupADb = createWorkRepository();
  49. try (Git git = new Git(groupADb)) {
  50. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  51. git.add().addFilepattern("a.txt").call();
  52. git.commit().setMessage("Initial commit").call();
  53. }
  54. groupBDb = createWorkRepository();
  55. try (Git git = new Git(groupBDb)) {
  56. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  57. git.add().addFilepattern("b.txt").call();
  58. git.commit().setMessage("Initial commit").call();
  59. }
  60. resolveRelativeUris();
  61. }
  62. @Test
  63. public void testMissingPath() throws Exception {
  64. try {
  65. execute("git repo");
  66. fail("Must die");
  67. } catch (Die e) {
  68. // expected, requires argument
  69. }
  70. }
  71. /**
  72. * See bug 484951: "git repo -h" should not print unexpected values
  73. *
  74. * @throws Exception
  75. */
  76. @Test
  77. public void testZombieHelpArgument() throws Exception {
  78. String[] output = execute("git repo -h");
  79. String all = Arrays.toString(output);
  80. assertTrue("Unexpected help output: " + all,
  81. all.contains("jgit repo"));
  82. assertFalse("Unexpected help output: " + all,
  83. all.contains("jgit repo VAL"));
  84. }
  85. @Test
  86. public void testAddRepoManifest() throws Exception {
  87. StringBuilder xmlContent = new StringBuilder();
  88. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  89. .append("<manifest>")
  90. .append("<remote name=\"remote1\" fetch=\".\" />")
  91. .append("<default revision=\"master\" remote=\"remote1\" />")
  92. .append("<project path=\"foo\" name=\"")
  93. .append(defaultUri)
  94. .append("\" groups=\"a,test\" />")
  95. .append("<project path=\"bar\" name=\"")
  96. .append(notDefaultUri)
  97. .append("\" groups=\"notdefault\" />")
  98. .append("<project path=\"a\" name=\"")
  99. .append(groupAUri)
  100. .append("\" groups=\"a\" />")
  101. .append("<project path=\"b\" name=\"")
  102. .append(groupBUri)
  103. .append("\" groups=\"b\" />")
  104. .append("</manifest>");
  105. writeTrashFile("manifest.xml", xmlContent.toString());
  106. StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
  107. .append(rootUri)
  108. .append("\" --groups=\"all,-a\" \"")
  109. .append(db.getWorkTree().getAbsolutePath())
  110. .append("/manifest.xml\"");
  111. execute(cmd.toString());
  112. File file = new File(db.getWorkTree(), "foo/hello.txt");
  113. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  114. file = new File(db.getWorkTree(), "bar/world.txt");
  115. assertTrue("\"all,-a\" has bar", file.exists());
  116. file = new File(db.getWorkTree(), "a/a.txt");
  117. assertFalse("\"all,-a\" doesn't have a", file.exists());
  118. file = new File(db.getWorkTree(), "b/b.txt");
  119. assertTrue("\"all,-a\" has have b", file.exists());
  120. }
  121. private void resolveRelativeUris() {
  122. // Find the longest common prefix ends with "/" as rootUri.
  123. defaultUri = defaultDb.getDirectory().toURI().toString();
  124. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  125. groupAUri = groupADb.getDirectory().toURI().toString();
  126. groupBUri = groupBDb.getDirectory().toURI().toString();
  127. int start = 0;
  128. while (start <= defaultUri.length()) {
  129. int newStart = defaultUri.indexOf('/', start + 1);
  130. String prefix = defaultUri.substring(0, newStart);
  131. if (!notDefaultUri.startsWith(prefix) ||
  132. !groupAUri.startsWith(prefix) ||
  133. !groupBUri.startsWith(prefix)) {
  134. start++;
  135. rootUri = defaultUri.substring(0, start) + "manifest";
  136. defaultUri = defaultUri.substring(start);
  137. notDefaultUri = notDefaultUri.substring(start);
  138. groupAUri = groupAUri.substring(start);
  139. groupBUri = groupBUri.substring(start);
  140. return;
  141. }
  142. start = newStart;
  143. }
  144. }
  145. }