1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
62
63
64
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 recommendShallow;
74 private String url;
75 private String defaultRevision;
76
77
78
79
80 public static class CopyFile {
81 final Repository repo;
82 final String path;
83 final String src;
84 final String dest;
85
86
87
88
89
90
91
92
93
94
95
96 public CopyFile(Repository repo, String path, String src, String dest) {
97 this.repo = repo;
98 this.path = path;
99 this.src = src;
100 this.dest = dest;
101 }
102
103
104
105
106
107
108 public void copy() throws IOException {
109 File srcFile = new File(repo.getWorkTree(),
110 path + "/" + src);
111 File destFile = new File(repo.getWorkTree(), dest);
112 FileInputStream input = new FileInputStream(srcFile);
113 try {
114 FileOutputStream output = new FileOutputStream(destFile);
115 try {
116 FileChannel channel = input.getChannel();
117 output.getChannel().transferFrom(
118 channel, 0, channel.size());
119 } finally {
120 output.close();
121 }
122 } finally {
123 input.close();
124 }
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public RepoProject(String name, String path, String revision,
144 String remote, Set<String> groups,
145 String recommendShallow) {
146 if (name == null) {
147 throw new NullPointerException();
148 }
149 this.name = name;
150 if (path != null)
151 this.path = path;
152 else
153 this.path = name;
154 this.revision = revision;
155 this.remote = remote;
156 this.groups = groups;
157 this.recommendShallow = recommendShallow;
158 copyfiles = new ArrayList<CopyFile>();
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public RepoProject(String name, String path, String revision,
174 String remote, String groups) {
175 this(name, path, revision, remote, new HashSet<String>(), null);
176 if (groups != null && groups.length() > 0)
177 this.setGroups(groups);
178 }
179
180
181
182
183
184
185
186 public RepoProject setUrl(String url) {
187 this.url = url;
188 return this;
189 }
190
191
192
193
194
195
196
197
198
199 public RepoProject setGroups(String groups) {
200 this.groups.clear();
201 this.groups.addAll(Arrays.asList(groups.split(",")));
202 return this;
203 }
204
205
206
207
208
209
210
211 public RepoProject setDefaultRevision(String defaultRevision) {
212 this.defaultRevision = defaultRevision;
213 return this;
214 }
215
216
217
218
219
220
221 public String getName() {
222 return name;
223 }
224
225
226
227
228
229
230 public String getPath() {
231 return path;
232 }
233
234
235
236
237
238
239 public String getRevision() {
240 return revision == null ? defaultRevision : revision;
241 }
242
243
244
245
246
247
248 public List<CopyFile> getCopyFiles() {
249 return Collections.unmodifiableList(copyfiles);
250 }
251
252
253
254
255
256
257 public String getUrl() {
258 return url;
259 }
260
261
262
263
264
265
266 public String getRemote() {
267 return remote;
268 }
269
270
271
272
273
274
275
276 public boolean inGroup(String group) {
277 return groups.contains(group);
278 }
279
280
281
282
283
284
285
286 public Set<String> getGroups() {
287 return groups;
288 }
289
290
291
292
293
294
295
296 public String getRecommendShallow() {
297 return recommendShallow;
298 }
299
300
301
302
303
304
305
306
307 public void setRecommendShallow(String recommendShallow) {
308 this.recommendShallow = recommendShallow;
309 }
310
311
312
313
314
315
316 public void addCopyFile(CopyFile copyfile) {
317 copyfiles.add(copyfile);
318 }
319
320
321
322
323
324
325 public void addCopyFiles(Collection<CopyFile> copyFiles) {
326 this.copyfiles.addAll(copyFiles);
327 }
328
329
330
331
332
333
334 public void clearCopyFiles() {
335 this.copyfiles.clear();
336 }
337
338 private String getPathWithSlash() {
339 if (path.endsWith("/"))
340 return path;
341 else
342 return path + "/";
343 }
344
345
346
347
348
349
350
351
352 public boolean isAncestorOf(RepoProject that) {
353 return isAncestorOf(that.getPathWithSlash());
354 }
355
356
357
358
359
360
361
362
363
364 public boolean isAncestorOf(String thatPath) {
365 return thatPath.startsWith(getPathWithSlash());
366 }
367
368 @Override
369 public boolean equals(Object o) {
370 if (o instanceof RepoProject) {
371 RepoProject that = (RepoProject) o;
372 return this.getPathWithSlash().equals(that.getPathWithSlash());
373 }
374 return false;
375 }
376
377 @Override
378 public int hashCode() {
379 return this.getPathWithSlash().hashCode();
380 }
381
382 @Override
383 public int compareTo(RepoProject that) {
384 return this.getPathWithSlash().compareTo(that.getPathWithSlash());
385 }
386 }
387