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.

RepoProject.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (C) 2015, 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.gitrepo;
  44. import java.io.File;
  45. import java.io.FileInputStream;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.nio.channels.FileChannel;
  49. import java.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.HashSet;
  54. import java.util.List;
  55. import java.util.Set;
  56. import org.eclipse.jgit.lib.Repository;
  57. /**
  58. * The representation of a repo sub project.
  59. *
  60. * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
  61. * @since 4.0
  62. */
  63. public class RepoProject implements Comparable<RepoProject> {
  64. private final String name;
  65. private final String path;
  66. private final String revision;
  67. private final String remote;
  68. private final Set<String> groups;
  69. private final List<CopyFile> copyfiles;
  70. private String url;
  71. private String defaultRevision;
  72. /**
  73. * The representation of a copy file configuration.
  74. */
  75. public static class CopyFile {
  76. final Repository repo;
  77. final String path;
  78. final String src;
  79. final String dest;
  80. /**
  81. * @param repo
  82. * the super project.
  83. * @param path
  84. * the path of the project containing this copyfile config.
  85. * @param src
  86. * the source path relative to the sub repo.
  87. * @param dest
  88. * the destination path relative to the super project.
  89. */
  90. public CopyFile(Repository repo, String path, String src, String dest) {
  91. this.repo = repo;
  92. this.path = path;
  93. this.src = src;
  94. this.dest = dest;
  95. }
  96. /**
  97. * Do the copy file action.
  98. *
  99. * @throws IOException
  100. */
  101. public void copy() throws IOException {
  102. File srcFile = new File(repo.getWorkTree(),
  103. path + "/" + src); //$NON-NLS-1$
  104. File destFile = new File(repo.getWorkTree(), dest);
  105. FileInputStream input = new FileInputStream(srcFile);
  106. try {
  107. FileOutputStream output = new FileOutputStream(destFile);
  108. try {
  109. FileChannel channel = input.getChannel();
  110. output.getChannel().transferFrom(
  111. channel, 0, channel.size());
  112. } finally {
  113. output.close();
  114. }
  115. } finally {
  116. input.close();
  117. }
  118. }
  119. }
  120. /**
  121. * @param name
  122. * the relative path to the {@code remote}
  123. * @param path
  124. * the relative path to the super project
  125. * @param revision
  126. * a SHA-1 or branch name or tag name
  127. * @param remote
  128. * name of the remote definition
  129. * @param groups
  130. * comma separated group list
  131. */
  132. public RepoProject(String name, String path, String revision,
  133. String remote, String groups) {
  134. this.name = name;
  135. if (path != null)
  136. this.path = path;
  137. else
  138. this.path = name;
  139. this.revision = revision;
  140. this.remote = remote;
  141. this.groups = new HashSet<String>();
  142. if (groups != null && groups.length() > 0)
  143. this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$
  144. copyfiles = new ArrayList<CopyFile>();
  145. }
  146. /**
  147. * Set the url of the sub repo.
  148. *
  149. * @param url
  150. * @return this for chaining.
  151. */
  152. public RepoProject setUrl(String url) {
  153. this.url = url;
  154. return this;
  155. }
  156. /**
  157. * Set the default revision for the sub repo.
  158. *
  159. * @param defaultRevision
  160. * @return this for chaining.
  161. */
  162. public RepoProject setDefaultRevision(String defaultRevision) {
  163. this.defaultRevision = defaultRevision;
  164. return this;
  165. }
  166. /**
  167. * Get the name (relative path to the {@code remote}) of this sub repo.
  168. *
  169. * @return {@code name}
  170. */
  171. public String getName() {
  172. return name;
  173. }
  174. /**
  175. * Get the path (relative path to the super project) of this sub repo.
  176. *
  177. * @return {@code path}
  178. */
  179. public String getPath() {
  180. return path;
  181. }
  182. /**
  183. * Get the revision of the sub repo.
  184. *
  185. * @return {@code revision} if set, or {@code defaultRevision}.
  186. */
  187. public String getRevision() {
  188. return revision == null ? defaultRevision : revision;
  189. }
  190. /**
  191. * Getter for the copyfile configurations.
  192. *
  193. * @return Immutable copy of {@code copyfiles}
  194. */
  195. public List<CopyFile> getCopyFiles() {
  196. return Collections.unmodifiableList(copyfiles);
  197. }
  198. /**
  199. * Get the url of the sub repo.
  200. *
  201. * @return {@code url}
  202. */
  203. public String getUrl() {
  204. return url;
  205. }
  206. /**
  207. * Get the name of the remote definition of the sub repo.
  208. *
  209. * @return {@code remote}
  210. */
  211. public String getRemote() {
  212. return remote;
  213. }
  214. /**
  215. * Test whether this sub repo belongs to a specified group.
  216. *
  217. * @param group
  218. * @return true if {@code group} is present.
  219. */
  220. public boolean inGroup(String group) {
  221. return groups.contains(group);
  222. }
  223. /**
  224. * Add a copy file configuration.
  225. *
  226. * @param copyfile
  227. */
  228. public void addCopyFile(CopyFile copyfile) {
  229. copyfiles.add(copyfile);
  230. }
  231. /**
  232. * Add a bunch of copyfile configurations.
  233. *
  234. * @param copyfiles
  235. */
  236. public void addCopyFiles(Collection<CopyFile> copyfiles) {
  237. this.copyfiles.addAll(copyfiles);
  238. }
  239. private String getPathWithSlash() {
  240. if (path.endsWith("/")) //$NON-NLS-1$
  241. return path;
  242. else
  243. return path + "/"; //$NON-NLS-1$
  244. }
  245. /**
  246. * Check if this sub repo is the ancestor of given sub repo.
  247. *
  248. * @param that
  249. * non null
  250. * @return true if this sub repo is the ancestor of given sub repo.
  251. */
  252. public boolean isAncestorOf(RepoProject that) {
  253. return that.getPathWithSlash().startsWith(this.getPathWithSlash());
  254. }
  255. @Override
  256. public boolean equals(Object o) {
  257. if (o instanceof RepoProject) {
  258. RepoProject that = (RepoProject) o;
  259. return this.getPathWithSlash().equals(that.getPathWithSlash());
  260. }
  261. return false;
  262. }
  263. @Override
  264. public int hashCode() {
  265. return this.getPathWithSlash().hashCode();
  266. }
  267. @Override
  268. public int compareTo(RepoProject that) {
  269. return this.getPathWithSlash().compareTo(that.getPathWithSlash());
  270. }
  271. }