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 final List<LinkFile> linkfiles;
74 private String recommendShallow;
75 private String url;
76 private String defaultRevision;
77
78
79
80
81
82
83 public static class ReferenceFile {
84 final Repository repo;
85 final String path;
86 final String src;
87 final String dest;
88
89
90
91
92
93
94
95
96
97
98
99 public ReferenceFile(Repository repo, String path, String src, String dest) {
100 this.repo = repo;
101 this.path = path;
102 this.src = src;
103 this.dest = dest;
104 }
105 }
106
107
108
109
110 public static class CopyFile extends ReferenceFile {
111
112
113
114
115
116
117
118
119
120
121 public CopyFile(Repository repo, String path, String src, String dest) {
122 super(repo, path, src, dest);
123 }
124
125
126
127
128
129
130 public void copy() throws IOException {
131 File srcFile = new File(repo.getWorkTree(),
132 path + "/" + src);
133 File destFile = new File(repo.getWorkTree(), dest);
134 FileInputStream input = new FileInputStream(srcFile);
135 try {
136 FileOutputStream output = new FileOutputStream(destFile);
137 try {
138 FileChannel channel = input.getChannel();
139 output.getChannel().transferFrom(
140 channel, 0, channel.size());
141 } finally {
142 output.close();
143 }
144 } finally {
145 input.close();
146 }
147 }
148 }
149
150
151
152
153
154
155 public static class LinkFile extends ReferenceFile {
156
157
158
159
160
161
162
163
164
165
166 public LinkFile(Repository repo, String path, String src, String dest) {
167 super(repo, path, src, dest);
168 }
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 public RepoProject(String name, String path, String revision,
189 String remote, Set<String> groups,
190 String recommendShallow) {
191 if (name == null) {
192 throw new NullPointerException();
193 }
194 this.name = name;
195 if (path != null)
196 this.path = path;
197 else
198 this.path = name;
199 this.revision = revision;
200 this.remote = remote;
201 this.groups = groups;
202 this.recommendShallow = recommendShallow;
203 copyfiles = new ArrayList<>();
204 linkfiles = new ArrayList<>();
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public RepoProject(String name, String path, String revision,
222 String remote, String groupsParam) {
223 this(name, path, revision, remote, new HashSet<String>(), null);
224 if (groupsParam != null && groupsParam.length() > 0)
225 this.setGroups(groupsParam);
226 }
227
228
229
230
231
232
233
234
235 public RepoProject setUrl(String url) {
236 this.url = url;
237 return this;
238 }
239
240
241
242
243
244
245
246
247
248 public RepoProject setGroups(String groupsParam) {
249 this.groups.clear();
250 this.groups.addAll(Arrays.asList(groupsParam.split(",")));
251 return this;
252 }
253
254
255
256
257
258
259
260
261 public RepoProject setDefaultRevision(String defaultRevision) {
262 this.defaultRevision = defaultRevision;
263 return this;
264 }
265
266
267
268
269
270
271 public String getName() {
272 return name;
273 }
274
275
276
277
278
279
280 public String getPath() {
281 return path;
282 }
283
284
285
286
287
288
289 public String getRevision() {
290 return revision == null ? defaultRevision : revision;
291 }
292
293
294
295
296
297
298 public List<CopyFile> getCopyFiles() {
299 return Collections.unmodifiableList(copyfiles);
300 }
301
302
303
304
305
306
307
308 public List<LinkFile> getLinkFiles() {
309 return Collections.unmodifiableList(linkfiles);
310 }
311
312
313
314
315
316
317 public String getUrl() {
318 return url;
319 }
320
321
322
323
324
325
326 public String getRemote() {
327 return remote;
328 }
329
330
331
332
333
334
335
336
337 public boolean inGroup(String group) {
338 return groups.contains(group);
339 }
340
341
342
343
344
345
346
347 public Set<String> getGroups() {
348 return groups;
349 }
350
351
352
353
354
355
356
357 public String getRecommendShallow() {
358 return recommendShallow;
359 }
360
361
362
363
364
365
366
367
368 public void setRecommendShallow(String recommendShallow) {
369 this.recommendShallow = recommendShallow;
370 }
371
372
373
374
375
376
377 public void addCopyFile(CopyFile copyfile) {
378 copyfiles.add(copyfile);
379 }
380
381
382
383
384
385
386
387
388 public void addCopyFiles(Collection<CopyFile> copyFiles) {
389 this.copyfiles.addAll(copyFiles);
390 }
391
392
393
394
395
396
397 public void clearCopyFiles() {
398 this.copyfiles.clear();
399 }
400
401
402
403
404
405
406
407 public void addLinkFile(LinkFile linkfile) {
408 linkfiles.add(linkfile);
409 }
410
411
412
413
414
415
416
417
418 public void addLinkFiles(Collection<LinkFile> linkFiles) {
419 this.linkfiles.addAll(linkFiles);
420 }
421
422
423
424
425
426
427 public void clearLinkFiles() {
428 this.linkfiles.clear();
429 }
430
431 private String getPathWithSlash() {
432 if (path.endsWith("/"))
433 return path;
434 else
435 return path + "/";
436 }
437
438
439
440
441
442
443
444
445 public boolean isAncestorOf(RepoProject that) {
446 return isAncestorOf(that.getPathWithSlash());
447 }
448
449
450
451
452
453
454
455
456
457 public boolean isAncestorOf(String thatPath) {
458 return thatPath.startsWith(getPathWithSlash());
459 }
460
461
462 @Override
463 public boolean equals(Object o) {
464 if (o instanceof RepoProject) {
465 RepoProject that = (RepoProject) o;
466 return this.getPathWithSlash().equals(that.getPathWithSlash());
467 }
468 return false;
469 }
470
471
472 @Override
473 public int hashCode() {
474 return this.getPathWithSlash().hashCode();
475 }
476
477
478 @Override
479 public int compareTo(RepoProject that) {
480 return this.getPathWithSlash().compareTo(that.getPathWithSlash());
481 }
482 }
483