1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.gitrepo;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.nio.channels.FileChannel;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.eclipse.jgit.lib.Repository;
26
27
28
29
30
31
32
33 public class RepoProject implements Comparable<RepoProject> {
34 private final String name;
35 private final String path;
36 private final String revision;
37 private final String remote;
38 private final Set<String> groups;
39 private final List<CopyFile> copyfiles;
40 private final List<LinkFile> linkfiles;
41 private String recommendShallow;
42 private String url;
43 private String defaultRevision;
44
45
46
47
48
49
50 public static class ReferenceFile {
51 final Repository repo;
52 final String path;
53 final String src;
54 final String dest;
55
56
57
58
59
60
61
62
63
64
65
66 public ReferenceFile(Repository repo, String path, String src, String dest) {
67 this.repo = repo;
68 this.path = path;
69 this.src = src;
70 this.dest = dest;
71 }
72 }
73
74
75
76
77 public static class CopyFile extends ReferenceFile {
78
79
80
81
82
83
84
85
86
87
88 public CopyFile(Repository repo, String path, String src, String dest) {
89 super(repo, path, src, dest);
90 }
91
92
93
94
95
96
97 public void copy() throws IOException {
98 File srcFile = new File(repo.getWorkTree(),
99 path + "/" + src);
100 File destFile = new File(repo.getWorkTree(), dest);
101 try (FileInputStream input = new FileInputStream(srcFile);
102 FileOutputStream output = new FileOutputStream(destFile)) {
103 FileChannel channel = input.getChannel();
104 output.getChannel().transferFrom(channel, 0, channel.size());
105 }
106 destFile.setExecutable(srcFile.canExecute());
107 }
108 }
109
110
111
112
113
114
115 public static class LinkFile extends ReferenceFile {
116
117
118
119
120
121
122
123
124
125
126 public LinkFile(Repository repo, String path, String src, String dest) {
127 super(repo, path, src, dest);
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public RepoProject(String name, String path, String revision,
149 String remote, Set<String> groups,
150 String recommendShallow) {
151 if (name == null) {
152 throw new NullPointerException();
153 }
154 this.name = name;
155 if (path != null)
156 this.path = path;
157 else
158 this.path = name;
159 this.revision = revision;
160 this.remote = remote;
161 this.groups = groups;
162 this.recommendShallow = recommendShallow;
163 copyfiles = new ArrayList<>();
164 linkfiles = new ArrayList<>();
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, String groupsParam) {
183 this(name, path, revision, remote, new HashSet<String>(), null);
184 if (groupsParam != null && groupsParam.length() > 0)
185 this.setGroups(groupsParam);
186 }
187
188
189
190
191
192
193
194
195 public RepoProject setUrl(String url) {
196 this.url = url;
197 return this;
198 }
199
200
201
202
203
204
205
206
207
208 public RepoProject setGroups(String groupsParam) {
209 this.groups.clear();
210 this.groups.addAll(Arrays.asList(groupsParam.split(",")));
211 return this;
212 }
213
214
215
216
217
218
219
220
221 public RepoProject setDefaultRevision(String defaultRevision) {
222 this.defaultRevision = defaultRevision;
223 return this;
224 }
225
226
227
228
229
230
231 public String getName() {
232 return name;
233 }
234
235
236
237
238
239
240 public String getPath() {
241 return path;
242 }
243
244
245
246
247
248
249 public String getRevision() {
250 return revision == null ? defaultRevision : revision;
251 }
252
253
254
255
256
257
258 public List<CopyFile> getCopyFiles() {
259 return Collections.unmodifiableList(copyfiles);
260 }
261
262
263
264
265
266
267
268 public List<LinkFile> getLinkFiles() {
269 return Collections.unmodifiableList(linkfiles);
270 }
271
272
273
274
275
276
277 public String getUrl() {
278 return url;
279 }
280
281
282
283
284
285
286 public String getRemote() {
287 return remote;
288 }
289
290
291
292
293
294
295
296
297 public boolean inGroup(String group) {
298 return groups.contains(group);
299 }
300
301
302
303
304
305
306
307 public Set<String> getGroups() {
308 return groups;
309 }
310
311
312
313
314
315
316
317 public String getRecommendShallow() {
318 return recommendShallow;
319 }
320
321
322
323
324
325
326
327
328 public void setRecommendShallow(String recommendShallow) {
329 this.recommendShallow = recommendShallow;
330 }
331
332
333
334
335
336
337 public void addCopyFile(CopyFile copyfile) {
338 copyfiles.add(copyfile);
339 }
340
341
342
343
344
345
346
347
348 public void addCopyFiles(Collection<CopyFile> copyFiles) {
349 this.copyfiles.addAll(copyFiles);
350 }
351
352
353
354
355
356
357 public void clearCopyFiles() {
358 this.copyfiles.clear();
359 }
360
361
362
363
364
365
366
367 public void addLinkFile(LinkFile linkfile) {
368 linkfiles.add(linkfile);
369 }
370
371
372
373
374
375
376
377
378 public void addLinkFiles(Collection<LinkFile> linkFiles) {
379 this.linkfiles.addAll(linkFiles);
380 }
381
382
383
384
385
386
387 public void clearLinkFiles() {
388 this.linkfiles.clear();
389 }
390
391 private String getPathWithSlash() {
392 if (path.endsWith("/")) {
393 return path;
394 }
395 return path + "/";
396 }
397
398
399
400
401
402
403
404
405 public boolean isAncestorOf(RepoProject that) {
406 return isAncestorOf(that.getPathWithSlash());
407 }
408
409
410
411
412
413
414
415
416
417 public boolean isAncestorOf(String thatPath) {
418 return thatPath.startsWith(getPathWithSlash());
419 }
420
421
422 @Override
423 public boolean equals(Object o) {
424 if (o instanceof RepoProject) {
425 RepoProject that = (RepoProject) o;
426 return this.getPathWithSlash().equals(that.getPathWithSlash());
427 }
428 return false;
429 }
430
431
432 @Override
433 public int hashCode() {
434 return this.getPathWithSlash().hashCode();
435 }
436
437
438 @Override
439 public int compareTo(RepoProject that) {
440 return this.getPathWithSlash().compareTo(that.getPathWithSlash());
441 }
442 }
443