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 url;
74 private String defaultRevision;
75
76
77
78
79 public static class CopyFile {
80 final Repository repo;
81 final String path;
82 final String src;
83 final String dest;
84
85
86
87
88
89
90
91
92
93
94
95 public CopyFile(Repository repo, String path, String src, String dest) {
96 this.repo = repo;
97 this.path = path;
98 this.src = src;
99 this.dest = dest;
100 }
101
102
103
104
105
106
107 public void copy() throws IOException {
108 File srcFile = new File(repo.getWorkTree(),
109 path + "/" + src);
110 File destFile = new File(repo.getWorkTree(), dest);
111 FileInputStream input = new FileInputStream(srcFile);
112 try {
113 FileOutputStream output = new FileOutputStream(destFile);
114 try {
115 FileChannel channel = input.getChannel();
116 output.getChannel().transferFrom(
117 channel, 0, channel.size());
118 } finally {
119 output.close();
120 }
121 } finally {
122 input.close();
123 }
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public RepoProject(String name, String path, String revision,
140 String remote, String groups) {
141 if (name == null) {
142 throw new NullPointerException();
143 }
144 this.name = name;
145 if (path != null)
146 this.path = path;
147 else
148 this.path = name;
149 this.revision = revision;
150 this.remote = remote;
151 this.groups = new HashSet<String>();
152 if (groups != null && groups.length() > 0)
153 this.groups.addAll(Arrays.asList(groups.split(",")));
154 copyfiles = new ArrayList<CopyFile>();
155 }
156
157
158
159
160
161
162
163 public RepoProject setUrl(String url) {
164 this.url = url;
165 return this;
166 }
167
168
169
170
171
172
173
174 public RepoProject setDefaultRevision(String defaultRevision) {
175 this.defaultRevision = defaultRevision;
176 return this;
177 }
178
179
180
181
182
183
184 public String getName() {
185 return name;
186 }
187
188
189
190
191
192
193 public String getPath() {
194 return path;
195 }
196
197
198
199
200
201
202 public String getRevision() {
203 return revision == null ? defaultRevision : revision;
204 }
205
206
207
208
209
210
211 public List<CopyFile> getCopyFiles() {
212 return Collections.unmodifiableList(copyfiles);
213 }
214
215
216
217
218
219
220 public String getUrl() {
221 return url;
222 }
223
224
225
226
227
228
229 public String getRemote() {
230 return remote;
231 }
232
233
234
235
236
237
238
239 public boolean inGroup(String group) {
240 return groups.contains(group);
241 }
242
243
244
245
246
247
248 public void addCopyFile(CopyFile copyfile) {
249 copyfiles.add(copyfile);
250 }
251
252
253
254
255
256
257 public void addCopyFiles(Collection<CopyFile> copyFiles) {
258 this.copyfiles.addAll(copyFiles);
259 }
260
261
262
263
264
265
266 public void clearCopyFiles() {
267 this.copyfiles.clear();
268 }
269
270 private String getPathWithSlash() {
271 if (path.endsWith("/"))
272 return path;
273 else
274 return path + "/";
275 }
276
277
278
279
280
281
282
283
284 public boolean isAncestorOf(RepoProject that) {
285 return isAncestorOf(that.getPathWithSlash());
286 }
287
288
289
290
291
292
293
294
295
296 public boolean isAncestorOf(String thatPath) {
297 return thatPath.startsWith(getPathWithSlash());
298 }
299
300 @Override
301 public boolean equals(Object o) {
302 if (o instanceof RepoProject) {
303 RepoProject that = (RepoProject) o;
304 return this.getPathWithSlash().equals(that.getPathWithSlash());
305 }
306 return false;
307 }
308
309 @Override
310 public int hashCode() {
311 return this.getPathWithSlash().hashCode();
312 }
313
314 @Override
315 public int compareTo(RepoProject that) {
316 return this.getPathWithSlash().compareTo(that.getPathWithSlash());
317 }
318 }
319