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