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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 java.io.File;
  47. import org.eclipse.jgit.api.Git;
  48. import org.eclipse.jgit.junit.JGitTestUtil;
  49. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  50. import org.eclipse.jgit.lib.Repository;
  51. import org.junit.Before;
  52. import org.junit.Test;
  53. public class RepoTest extends CLIRepositoryTestCase {
  54. private Repository defaultDb;
  55. private Repository notDefaultDb;
  56. private Repository groupADb;
  57. private Repository groupBDb;
  58. private String rootUri;
  59. private String defaultUri;
  60. private String notDefaultUri;
  61. private String groupAUri;
  62. private String groupBUri;
  63. @Override
  64. @Before
  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("\" groups=\"a,test\" />")
  99. .append("<project path=\"bar\" name=\"")
  100. .append(notDefaultUri)
  101. .append("\" groups=\"notdefault\" />")
  102. .append("<project path=\"a\" name=\"")
  103. .append(groupAUri)
  104. .append("\" groups=\"a\" />")
  105. .append("<project path=\"b\" name=\"")
  106. .append(groupBUri)
  107. .append("\" groups=\"b\" />")
  108. .append("</manifest>");
  109. writeTrashFile("manifest.xml", xmlContent.toString());
  110. StringBuilder cmd = new StringBuilder("git repo --base-uri=\"")
  111. .append(rootUri)
  112. .append("\" --groups=\"all,-a\" \"")
  113. .append(db.getWorkTree().getAbsolutePath())
  114. .append("/manifest.xml\"");
  115. execute(cmd.toString());
  116. File file = new File(db.getWorkTree(), "foo/hello.txt");
  117. assertFalse("\"all,-a\" doesn't have foo", file.exists());
  118. file = new File(db.getWorkTree(), "bar/world.txt");
  119. assertTrue("\"all,-a\" has bar", file.exists());
  120. file = new File(db.getWorkTree(), "a/a.txt");
  121. assertFalse("\"all,-a\" doesn't have a", file.exists());
  122. file = new File(db.getWorkTree(), "b/b.txt");
  123. assertTrue("\"all,-a\" has have b", file.exists());
  124. }
  125. private void resolveRelativeUris() {
  126. // Find the longest common prefix ends with "/" as rootUri.
  127. defaultUri = defaultDb.getDirectory().toURI().toString();
  128. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  129. groupAUri = groupADb.getDirectory().toURI().toString();
  130. groupBUri = groupBDb.getDirectory().toURI().toString();
  131. int start = 0;
  132. while (start <= defaultUri.length()) {
  133. int newStart = defaultUri.indexOf('/', start + 1);
  134. String prefix = defaultUri.substring(0, newStart);
  135. if (!notDefaultUri.startsWith(prefix) ||
  136. !groupAUri.startsWith(prefix) ||
  137. !groupBUri.startsWith(prefix)) {
  138. start++;
  139. rootUri = defaultUri.substring(0, start) + "manifest";
  140. defaultUri = defaultUri.substring(start);
  141. notDefaultUri = notDefaultUri.substring(start);
  142. groupAUri = groupAUri.substring(start);
  143. groupBUri = groupBUri.substring(start);
  144. return;
  145. }
  146. start = newStart;
  147. }
  148. }
  149. }