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
44 package org.eclipse.jgit.transport;
45
46 import static org.eclipse.jgit.util.StringUtils.equalsIgnoreCase;
47 import static org.eclipse.jgit.util.StringUtils.toLowerCase;
48
49 import java.io.File;
50 import java.util.EnumSet;
51 import java.util.HashMap;
52 import java.util.Map;
53
54 import org.eclipse.jgit.annotations.Nullable;
55 import org.eclipse.jgit.internal.storage.file.LazyObjectIdSetFile;
56 import org.eclipse.jgit.lib.Config;
57 import org.eclipse.jgit.lib.Config.SectionParser;
58 import org.eclipse.jgit.lib.ObjectChecker;
59 import org.eclipse.jgit.lib.ObjectIdSet;
60 import org.eclipse.jgit.lib.Ref;
61 import org.eclipse.jgit.lib.Repository;
62 import org.eclipse.jgit.util.SystemReader;
63
64
65
66
67
68 public class TransferConfig {
69 private static final String FSCK = "fsck";
70
71
72 public static final Config.SectionParser<TransferConfig> KEY =
73 TransferConfig::new;
74
75
76
77
78
79
80 public enum FsckMode {
81
82
83
84 ERROR,
85
86
87
88 WARN,
89
90
91
92 IGNORE;
93 }
94
95
96
97
98
99 enum ProtocolVersion {
100 V0("0"),
101 V2("2");
102
103 final String name;
104
105 ProtocolVersion(String name) {
106 this.name = name;
107 }
108
109 static @Nullable ProtocolVersion parse(@Nullable String name) {
110 if (name == null) {
111 return null;
112 }
113 for (ProtocolVersion v : ProtocolVersion.values()) {
114 if (v.name.equals(name)) {
115 return v;
116 }
117 }
118 return null;
119 }
120 }
121
122 private final boolean fetchFsck;
123 private final boolean receiveFsck;
124 private final String fsckSkipList;
125 private final EnumSet<ObjectChecker.ErrorType> ignore;
126 private final boolean allowInvalidPersonIdent;
127 private final boolean safeForWindows;
128 private final boolean safeForMacOS;
129 private final boolean allowRefInWant;
130 private final boolean allowTipSha1InWant;
131 private final boolean allowReachableSha1InWant;
132 private final boolean allowFilter;
133 final @Nullable ProtocolVersion protocolVersion;
134 final String[] hideRefs;
135
136
137
138
139
140
141
142
143
144
145 public TransferConfig(Repository db) {
146 this(db.getConfig());
147 }
148
149
150
151
152
153
154
155
156
157
158
159 @SuppressWarnings("nls")
160 public TransferConfig(Config rc) {
161 boolean fsck = rc.getBoolean("transfer", "fsckobjects", false);
162 fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck);
163 receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck);
164 fsckSkipList = rc.getString(FSCK, null, "skipList");
165 allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent",
166 false);
167 safeForWindows = rc.getBoolean(FSCK, "safeForWindows",
168 SystemReader.getInstance().isWindows());
169 safeForMacOS = rc.getBoolean(FSCK, "safeForMacOS",
170 SystemReader.getInstance().isMacOS());
171
172 ignore = EnumSet.noneOf(ObjectChecker.ErrorType.class);
173 EnumSet<ObjectChecker.ErrorType> set = EnumSet
174 .noneOf(ObjectChecker.ErrorType.class);
175 for (String key : rc.getNames(FSCK)) {
176 if (equalsIgnoreCase(key, "skipList")
177 || equalsIgnoreCase(key, "allowLeadingZeroFileMode")
178 || equalsIgnoreCase(key, "allowInvalidPersonIdent")
179 || equalsIgnoreCase(key, "safeForWindows")
180 || equalsIgnoreCase(key, "safeForMacOS")) {
181 continue;
182 }
183
184 ObjectChecker.ErrorType id = FsckKeyNameHolder.parse(key);
185 if (id != null) {
186 switch (rc.getEnum(FSCK, null, key, FsckMode.ERROR)) {
187 case ERROR:
188 ignore.remove(id);
189 break;
190 case WARN:
191 case IGNORE:
192 ignore.add(id);
193 break;
194 }
195 set.add(id);
196 }
197 }
198 if (!set.contains(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE)
199 && rc.getBoolean(FSCK, "allowLeadingZeroFileMode", false)) {
200 ignore.add(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE);
201 }
202
203 allowRefInWant = rc.getBoolean("uploadpack", "allowrefinwant", false);
204 allowTipSha1InWant = rc.getBoolean(
205 "uploadpack", "allowtipsha1inwant", false);
206 allowReachableSha1InWant = rc.getBoolean(
207 "uploadpack", "allowreachablesha1inwant", false);
208 allowFilter = rc.getBoolean(
209 "uploadpack", "allowfilter", false);
210 protocolVersion = ProtocolVersion.parse(rc.getString("protocol", null, "version"));
211 hideRefs = rc.getStringList("uploadpack", null, "hiderefs");
212 }
213
214
215
216
217
218
219
220
221 @Nullable
222 public ObjectChecker newObjectChecker() {
223 return newObjectChecker(fetchFsck);
224 }
225
226
227
228
229
230
231
232
233 @Nullable
234 public ObjectChecker newReceiveObjectChecker() {
235 return newObjectChecker(receiveFsck);
236 }
237
238 private ObjectChecker newObjectChecker(boolean check) {
239 if (!check) {
240 return null;
241 }
242 return new ObjectChecker()
243 .setIgnore(ignore)
244 .setAllowInvalidPersonIdent(allowInvalidPersonIdent)
245 .setSafeForWindows(safeForWindows)
246 .setSafeForMacOS(safeForMacOS)
247 .setSkipList(skipList());
248 }
249
250 private ObjectIdSet skipList() {
251 if (fsckSkipList != null && !fsckSkipList.isEmpty()) {
252 return new LazyObjectIdSetFile(new File(fsckSkipList));
253 }
254 return null;
255 }
256
257
258
259
260
261
262
263 public boolean isAllowTipSha1InWant() {
264 return allowTipSha1InWant;
265 }
266
267
268
269
270
271
272
273 public boolean isAllowReachableSha1InWant() {
274 return allowReachableSha1InWant;
275 }
276
277
278
279
280
281 public boolean isAllowFilter() {
282 return allowFilter;
283 }
284
285
286
287
288
289 public boolean isAllowRefInWant() {
290 return allowRefInWant;
291 }
292
293
294
295
296
297
298
299
300
301 public RefFilter getRefFilter() {
302 if (hideRefs.length == 0)
303 return RefFilter.DEFAULT;
304
305 return new RefFilter() {
306 @Override
307 public Map<String, Ref> filter(Map<String, Ref> refs) {
308 Map<String, Ref> result = new HashMap<>();
309 for (Map.Entry<String, Ref> e : refs.entrySet()) {
310 boolean add = true;
311 for (String hide : hideRefs) {
312 if (e.getKey().equals(hide) || prefixMatch(hide, e.getKey())) {
313 add = false;
314 break;
315 }
316 }
317 if (add)
318 result.put(e.getKey(), e.getValue());
319 }
320 return result;
321 }
322
323 private boolean prefixMatch(String p, String s) {
324 return p.charAt(p.length() - 1) == '/' && s.startsWith(p);
325 }
326 };
327 }
328
329
330
331
332
333
334
335 boolean hasDefaultRefFilter() {
336 return hideRefs.length == 0;
337 }
338
339 static class FsckKeyNameHolder {
340 private static final Map<String, ObjectChecker.ErrorType> errors;
341
342 static {
343 errors = new HashMap<>();
344 for (ObjectChecker.ErrorType m : ObjectChecker.ErrorType.values()) {
345 errors.put(keyNameFor(m.name()), m);
346 }
347 }
348
349 @Nullable
350 static ObjectChecker.ErrorType parse(String key) {
351 return errors.get(toLowerCase(key));
352 }
353
354 private static String keyNameFor(String name) {
355 StringBuilder r = new StringBuilder(name.length());
356 for (int i = 0; i < name.length(); i++) {
357 char c = name.charAt(i);
358 if (c != '_') {
359 r.append(c);
360 }
361 }
362 return toLowerCase(r.toString());
363 }
364
365 private FsckKeyNameHolder() {
366 }
367 }
368 }