View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * The standard "transfer", "fetch", "protocol", "receive", and "uploadpack"
66   * configuration parameters.
67   */
68  public class TransferConfig {
69  	private static final String FSCK = "fsck"; //$NON-NLS-1$
70  
71  	/** Key for {@link Config#get(SectionParser)}. */
72  	public static final Config.SectionParser<TransferConfig> KEY =
73  			TransferConfig::new;
74  
75  	/**
76  	 * A git configuration value for how to handle a fsck failure of a particular kind.
77  	 * Used in e.g. fsck.missingEmail.
78  	 * @since 4.9
79  	 */
80  	public enum FsckMode {
81  		/**
82  		 * Treat it as an error (the default).
83  		 */
84  		ERROR,
85  		/**
86  		 * Issue a warning (in fact, jgit treats this like IGNORE, but git itself does warn).
87  		 */
88  		WARN,
89  		/**
90  		 * Ignore the error.
91  		 */
92  		IGNORE;
93  	}
94  
95  	/**
96  	 * A git configuration variable for which versions of the Git protocol to prefer.
97  	 * Used in protocol.version.
98  	 */
99  	enum ProtocolVersion {
100 		V0("0"), //$NON-NLS-1$
101 		V2("2"); //$NON-NLS-1$
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 	 * Create a configuration honoring the repository's settings.
139 	 *
140 	 * @param db
141 	 *            the repository to read settings from. The repository is not
142 	 *            retained by the new configuration, instead its settings are
143 	 *            copied during the constructor.
144 	 * @since 5.1.4
145 	 */
146 	public TransferConfig(Repository db) {
147 		this(db.getConfig());
148 	}
149 
150 	/**
151 	 * Create a configuration honoring settings in a
152 	 * {@link org.eclipse.jgit.lib.Config}.
153 	 *
154 	 * @param rc
155 	 *            the source to read settings from. The source is not retained
156 	 *            by the new configuration, instead its settings are copied
157 	 *            during the constructor.
158 	 * @since 5.1.4
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 	 * Create checker to verify fetched objects
217 	 *
218 	 * @return checker to verify fetched objects, or null if checking is not
219 	 *         enabled in the repository configuration.
220 	 * @since 3.6
221 	 */
222 	@Nullable
223 	public ObjectChecker newObjectChecker() {
224 		return newObjectChecker(fetchFsck);
225 	}
226 
227 	/**
228 	 * Create checker to verify objects pushed into this repository
229 	 *
230 	 * @return checker to verify objects pushed into this repository, or null if
231 	 *         checking is not enabled in the repository configuration.
232 	 * @since 4.2
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 	 * Whether to allow clients to request non-advertised tip SHA-1s
260 	 *
261 	 * @return allow clients to request non-advertised tip SHA-1s?
262 	 * @since 3.1
263 	 */
264 	public boolean isAllowTipSha1InWant() {
265 		return allowTipSha1InWant;
266 	}
267 
268 	/**
269 	 * Whether to allow clients to request non-tip SHA-1s
270 	 *
271 	 * @return allow clients to request non-tip SHA-1s?
272 	 * @since 4.1
273 	 */
274 	public boolean isAllowReachableSha1InWant() {
275 		return allowReachableSha1InWant;
276 	}
277 
278 	/**
279 	 * @return true if clients are allowed to specify a "filter" line
280 	 * @since 5.0
281 	 */
282 	public boolean isAllowFilter() {
283 		return allowFilter;
284 	}
285 
286 	/**
287 	 * @return true if clients are allowed to specify a "want-ref" line
288 	 * @since 5.1
289 	 */
290 	public boolean isAllowRefInWant() {
291 		return allowRefInWant;
292 	}
293 
294 	/**
295 	 * Get {@link org.eclipse.jgit.transport.RefFilter} respecting configured
296 	 * hidden refs.
297 	 *
298 	 * @return {@link org.eclipse.jgit.transport.RefFilter} respecting
299 	 *         configured hidden refs.
300 	 * @since 3.1
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 	 * Like {@code getRefFilter() == RefFilter.DEFAULT}, but faster.
332 	 *
333 	 * @return {@code true} if no ref filtering is needed because there
334 	 *         are no configured hidden refs.
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 }