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.8KB

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