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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 final List<LinkFile> linkfiles;
  71. private String recommendShallow;
  72. private String url;
  73. private String defaultRevision;
  74. /**
  75. * The representation of a reference file configuration.
  76. *
  77. * @since 4.8
  78. */
  79. public static class ReferenceFile {
  80. final Repository repo;
  81. final String path;
  82. final String src;
  83. final String dest;
  84. /**
  85. * @param repo
  86. * the super project.
  87. * @param path
  88. * the path of the project containing this copyfile config.
  89. * @param src
  90. * the source path relative to the sub repo.
  91. * @param dest
  92. * the destination path relative to the super project.
  93. */
  94. public ReferenceFile(Repository repo, String path, String src, String dest) {
  95. this.repo = repo;
  96. this.path = path;
  97. this.src = src;
  98. this.dest = dest;
  99. }
  100. }
  101. /**
  102. * The representation of a copy file configuration.
  103. */
  104. public static class CopyFile extends ReferenceFile {
  105. /**
  106. * @param repo
  107. * the super project.
  108. * @param path
  109. * the path of the project containing this copyfile config.
  110. * @param src
  111. * the source path relative to the sub repo.
  112. * @param dest
  113. * the destination path relative to the super project.
  114. */
  115. public CopyFile(Repository repo, String path, String src, String dest) {
  116. super(repo, path, src, dest);
  117. }
  118. /**
  119. * Do the copy file action.
  120. *
  121. * @throws IOException
  122. */
  123. public void copy() throws IOException {
  124. File srcFile = new File(repo.getWorkTree(),
  125. path + "/" + src); //$NON-NLS-1$
  126. File destFile = new File(repo.getWorkTree(), dest);
  127. try (FileInputStream input = new FileInputStream(srcFile);
  128. FileOutputStream output = new FileOutputStream(destFile)) {
  129. FileChannel channel = input.getChannel();
  130. output.getChannel().transferFrom(channel, 0, channel.size());
  131. }
  132. destFile.setExecutable(srcFile.canExecute());
  133. }
  134. }
  135. /**
  136. * The representation of a link file configuration.
  137. *
  138. * @since 4.8
  139. */
  140. public static class LinkFile extends ReferenceFile {
  141. /**
  142. * @param repo
  143. * the super project.
  144. * @param path
  145. * the path of the project containing this linkfile config.
  146. * @param src
  147. * the source path relative to the sub repo.
  148. * @param dest
  149. * the destination path relative to the super project.
  150. */
  151. public LinkFile(Repository repo, String path, String src, String dest) {
  152. super(repo, path, src, dest);
  153. }
  154. }
  155. /**
  156. * Constructor for RepoProject
  157. *
  158. * @param name
  159. * the relative path to the {@code remote}
  160. * @param path
  161. * the relative path to the super project
  162. * @param revision
  163. * a SHA-1 or branch name or tag name
  164. * @param remote
  165. * name of the remote definition
  166. * @param groups
  167. * set of groups
  168. * @param recommendShallow
  169. * recommendation for shallowness
  170. * @since 4.4
  171. */
  172. public RepoProject(String name, String path, String revision,
  173. String remote, Set<String> groups,
  174. String recommendShallow) {
  175. if (name == null) {
  176. throw new NullPointerException();
  177. }
  178. this.name = name;
  179. if (path != null)
  180. this.path = path;
  181. else
  182. this.path = name;
  183. this.revision = revision;
  184. this.remote = remote;
  185. this.groups = groups;
  186. this.recommendShallow = recommendShallow;
  187. copyfiles = new ArrayList<>();
  188. linkfiles = new ArrayList<>();
  189. }
  190. /**
  191. * Constructor for RepoProject
  192. *
  193. * @param name
  194. * the relative path to the {@code remote}
  195. * @param path
  196. * the relative path to the super project
  197. * @param revision
  198. * a SHA-1 or branch name or tag name
  199. * @param remote
  200. * name of the remote definition
  201. * @param groupsParam
  202. * comma separated group list
  203. */
  204. public RepoProject(String name, String path, String revision,
  205. String remote, String groupsParam) {
  206. this(name, path, revision, remote, new HashSet<String>(), null);
  207. if (groupsParam != null && groupsParam.length() > 0)
  208. this.setGroups(groupsParam);
  209. }
  210. /**
  211. * Set the url of the sub repo.
  212. *
  213. * @param url
  214. * project url
  215. * @return this for chaining.
  216. */
  217. public RepoProject setUrl(String url) {
  218. this.url = url;
  219. return this;
  220. }
  221. /**
  222. * Set the url of the sub repo.
  223. *
  224. * @param groupsParam
  225. * comma separated group list
  226. * @return this for chaining.
  227. * @since 4.4
  228. */
  229. public RepoProject setGroups(String groupsParam) {
  230. this.groups.clear();
  231. this.groups.addAll(Arrays.asList(groupsParam.split(","))); //$NON-NLS-1$
  232. return this;
  233. }
  234. /**
  235. * Set the default revision for the sub repo.
  236. *
  237. * @param defaultRevision
  238. * the name of the default revision
  239. * @return this for chaining.
  240. */
  241. public RepoProject setDefaultRevision(String defaultRevision) {
  242. this.defaultRevision = defaultRevision;
  243. return this;
  244. }
  245. /**
  246. * Get the name (relative path to the {@code remote}) of this sub repo.
  247. *
  248. * @return {@code name}
  249. */
  250. public String getName() {
  251. return name;
  252. }
  253. /**
  254. * Get the path (relative path to the super project) of this sub repo.
  255. *
  256. * @return {@code path}
  257. */
  258. public String getPath() {
  259. return path;
  260. }
  261. /**
  262. * Get the revision of the sub repo.
  263. *
  264. * @return {@code revision} if set, or {@code defaultRevision}.
  265. */
  266. public String getRevision() {
  267. return revision == null ? defaultRevision : revision;
  268. }
  269. /**
  270. * Getter for the copyfile configurations.
  271. *
  272. * @return Immutable copy of {@code copyfiles}
  273. */
  274. public List<CopyFile> getCopyFiles() {
  275. return Collections.unmodifiableList(copyfiles);
  276. }
  277. /**
  278. * Getter for the linkfile configurations.
  279. *
  280. * @return Immutable copy of {@code linkfiles}
  281. * @since 4.8
  282. */
  283. public List<LinkFile> getLinkFiles() {
  284. return Collections.unmodifiableList(linkfiles);
  285. }
  286. /**
  287. * Get the url of the sub repo.
  288. *
  289. * @return {@code url}
  290. */
  291. public String getUrl() {
  292. return url;
  293. }
  294. /**
  295. * Get the name of the remote definition of the sub repo.
  296. *
  297. * @return {@code remote}
  298. */
  299. public String getRemote() {
  300. return remote;
  301. }
  302. /**
  303. * Test whether this sub repo belongs to a specified group.
  304. *
  305. * @param group
  306. * a group
  307. * @return true if {@code group} is present.
  308. */
  309. public boolean inGroup(String group) {
  310. return groups.contains(group);
  311. }
  312. /**
  313. * Return the set of groups.
  314. *
  315. * @return a Set of groups.
  316. * @since 4.4
  317. */
  318. public Set<String> getGroups() {
  319. return groups;
  320. }
  321. /**
  322. * Return the recommendation for shallowness.
  323. *
  324. * @return the String of "clone-depth"
  325. * @since 4.4
  326. */
  327. public String getRecommendShallow() {
  328. return recommendShallow;
  329. }
  330. /**
  331. * Sets the recommendation for shallowness.
  332. *
  333. * @param recommendShallow
  334. * recommendation for shallowness
  335. * @since 4.4
  336. */
  337. public void setRecommendShallow(String recommendShallow) {
  338. this.recommendShallow = recommendShallow;
  339. }
  340. /**
  341. * Add a copy file configuration.
  342. *
  343. * @param copyfile a {@link org.eclipse.jgit.gitrepo.RepoProject.CopyFile} object.
  344. */
  345. public void addCopyFile(CopyFile copyfile) {
  346. copyfiles.add(copyfile);
  347. }
  348. /**
  349. * Add a bunch of copyfile configurations.
  350. *
  351. * @param copyFiles
  352. * a collection of
  353. * {@link org.eclipse.jgit.gitrepo.RepoProject.CopyFile} objects
  354. */
  355. public void addCopyFiles(Collection<CopyFile> copyFiles) {
  356. this.copyfiles.addAll(copyFiles);
  357. }
  358. /**
  359. * Clear all the copyfiles.
  360. *
  361. * @since 4.2
  362. */
  363. public void clearCopyFiles() {
  364. this.copyfiles.clear();
  365. }
  366. /**
  367. * Add a link file configuration.
  368. *
  369. * @param linkfile a {@link org.eclipse.jgit.gitrepo.RepoProject.LinkFile} object.
  370. * @since 4.8
  371. */
  372. public void addLinkFile(LinkFile linkfile) {
  373. linkfiles.add(linkfile);
  374. }
  375. /**
  376. * Add a bunch of linkfile configurations.
  377. *
  378. * @param linkFiles
  379. * a collection of {@link LinkFile}s
  380. * @since 4.8
  381. */
  382. public void addLinkFiles(Collection<LinkFile> linkFiles) {
  383. this.linkfiles.addAll(linkFiles);
  384. }
  385. /**
  386. * Clear all the linkfiles.
  387. *
  388. * @since 4.8
  389. */
  390. public void clearLinkFiles() {
  391. this.linkfiles.clear();
  392. }
  393. private String getPathWithSlash() {
  394. if (path.endsWith("/")) { //$NON-NLS-1$
  395. return path;
  396. }
  397. return path + "/"; //$NON-NLS-1$
  398. }
  399. /**
  400. * Check if this sub repo is the ancestor of given sub repo.
  401. *
  402. * @param that
  403. * non null
  404. * @return true if this sub repo is the ancestor of given sub repo.
  405. */
  406. public boolean isAncestorOf(RepoProject that) {
  407. return isAncestorOf(that.getPathWithSlash());
  408. }
  409. /**
  410. * Check if this sub repo is an ancestor of the given path.
  411. *
  412. * @param thatPath
  413. * path to be checked to see if it is within this repository
  414. * @return true if this sub repo is an ancestor of the given path.
  415. * @since 4.2
  416. */
  417. public boolean isAncestorOf(String thatPath) {
  418. return thatPath.startsWith(getPathWithSlash());
  419. }
  420. /** {@inheritDoc} */
  421. @Override
  422. public boolean equals(Object o) {
  423. if (o instanceof RepoProject) {
  424. RepoProject that = (RepoProject) o;
  425. return this.getPathWithSlash().equals(that.getPathWithSlash());
  426. }
  427. return false;
  428. }
  429. /** {@inheritDoc} */
  430. @Override
  431. public int hashCode() {
  432. return this.getPathWithSlash().hashCode();
  433. }
  434. /** {@inheritDoc} */
  435. @Override
  436. public int compareTo(RepoProject that) {
  437. return this.getPathWithSlash().compareTo(that.getPathWithSlash());
  438. }
  439. }