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