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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. if (name == null) {
  135. throw new NullPointerException();
  136. }
  137. this.name = name;
  138. if (path != null)
  139. this.path = path;
  140. else
  141. this.path = name;
  142. this.revision = revision;
  143. this.remote = remote;
  144. this.groups = new HashSet<String>();
  145. if (groups != null && groups.length() > 0)
  146. this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$
  147. copyfiles = new ArrayList<CopyFile>();
  148. }
  149. /**
  150. * Set the url of the sub repo.
  151. *
  152. * @param url
  153. * @return this for chaining.
  154. */
  155. public RepoProject setUrl(String url) {
  156. this.url = url;
  157. return this;
  158. }
  159. /**
  160. * Set the default revision for the sub repo.
  161. *
  162. * @param defaultRevision
  163. * @return this for chaining.
  164. */
  165. public RepoProject setDefaultRevision(String defaultRevision) {
  166. this.defaultRevision = defaultRevision;
  167. return this;
  168. }
  169. /**
  170. * Get the name (relative path to the {@code remote}) of this sub repo.
  171. *
  172. * @return {@code name}
  173. */
  174. public String getName() {
  175. return name;
  176. }
  177. /**
  178. * Get the path (relative path to the super project) of this sub repo.
  179. *
  180. * @return {@code path}
  181. */
  182. public String getPath() {
  183. return path;
  184. }
  185. /**
  186. * Get the revision of the sub repo.
  187. *
  188. * @return {@code revision} if set, or {@code defaultRevision}.
  189. */
  190. public String getRevision() {
  191. return revision == null ? defaultRevision : revision;
  192. }
  193. /**
  194. * Getter for the copyfile configurations.
  195. *
  196. * @return Immutable copy of {@code copyfiles}
  197. */
  198. public List<CopyFile> getCopyFiles() {
  199. return Collections.unmodifiableList(copyfiles);
  200. }
  201. /**
  202. * Get the url of the sub repo.
  203. *
  204. * @return {@code url}
  205. */
  206. public String getUrl() {
  207. return url;
  208. }
  209. /**
  210. * Get the name of the remote definition of the sub repo.
  211. *
  212. * @return {@code remote}
  213. */
  214. public String getRemote() {
  215. return remote;
  216. }
  217. /**
  218. * Test whether this sub repo belongs to a specified group.
  219. *
  220. * @param group
  221. * @return true if {@code group} is present.
  222. */
  223. public boolean inGroup(String group) {
  224. return groups.contains(group);
  225. }
  226. /**
  227. * Add a copy file configuration.
  228. *
  229. * @param copyfile
  230. */
  231. public void addCopyFile(CopyFile copyfile) {
  232. copyfiles.add(copyfile);
  233. }
  234. /**
  235. * Add a bunch of copyfile configurations.
  236. *
  237. * @param copyFiles
  238. */
  239. public void addCopyFiles(Collection<CopyFile> copyFiles) {
  240. this.copyfiles.addAll(copyFiles);
  241. }
  242. /**
  243. * Clear all the copyfiles.
  244. *
  245. * @since 4.2
  246. */
  247. public void clearCopyFiles() {
  248. this.copyfiles.clear();
  249. }
  250. private String getPathWithSlash() {
  251. if (path.endsWith("/")) //$NON-NLS-1$
  252. return path;
  253. else
  254. return path + "/"; //$NON-NLS-1$
  255. }
  256. /**
  257. * Check if this sub repo is the ancestor of given sub repo.
  258. *
  259. * @param that
  260. * non null
  261. * @return true if this sub repo is the ancestor of given sub repo.
  262. */
  263. public boolean isAncestorOf(RepoProject that) {
  264. return isAncestorOf(that.getPathWithSlash());
  265. }
  266. /**
  267. * Check if this sub repo is an ancestor of the given path.
  268. *
  269. * @param thatPath
  270. * path to be checked to see if it is within this repository
  271. * @return true if this sub repo is an ancestor of the given path.
  272. * @since 4.2
  273. */
  274. public boolean isAncestorOf(String thatPath) {
  275. return thatPath.startsWith(getPathWithSlash());
  276. }
  277. @Override
  278. public boolean equals(Object o) {
  279. if (o instanceof RepoProject) {
  280. RepoProject that = (RepoProject) o;
  281. return this.getPathWithSlash().equals(that.getPathWithSlash());
  282. }
  283. return false;
  284. }
  285. @Override
  286. public int hashCode() {
  287. return this.getPathWithSlash().hashCode();
  288. }
  289. @Override
  290. public int compareTo(RepoProject that) {
  291. return this.getPathWithSlash().compareTo(that.getPathWithSlash());
  292. }
  293. }