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