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