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