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