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", "receive", and "uploadpack" configuration
66   * 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  	private final boolean fetchFsck;
96  	private final boolean receiveFsck;
97  	private final String fsckSkipList;
98  	private final EnumSet<ObjectChecker.ErrorType> ignore;
99  	private final boolean allowInvalidPersonIdent;
100 	private final boolean safeForWindows;
101 	private final boolean safeForMacOS;
102 	private final boolean allowTipSha1InWant;
103 	private final boolean allowReachableSha1InWant;
104 	final String[] hideRefs;
105 
106 	TransferConfig(final Repository db) {
107 		this(db.getConfig());
108 	}
109 
110 	TransferConfig(final Config rc) {
111 		boolean fsck = rc.getBoolean("transfer", "fsckobjects", false); //$NON-NLS-1$ //$NON-NLS-2$
112 		fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$
113 		receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck); //$NON-NLS-1$ //$NON-NLS-2$
114 		fsckSkipList = rc.getString(FSCK, null, "skipList"); //$NON-NLS-1$
115 		allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent", false); //$NON-NLS-1$
116 		safeForWindows = rc.getBoolean(FSCK, "safeForWindows", //$NON-NLS-1$
117 						SystemReader.getInstance().isWindows());
118 		safeForMacOS = rc.getBoolean(FSCK, "safeForMacOS", //$NON-NLS-1$
119 						SystemReader.getInstance().isMacOS());
120 
121 		ignore = EnumSet.noneOf(ObjectChecker.ErrorType.class);
122 		EnumSet<ObjectChecker.ErrorType> set = EnumSet
123 				.noneOf(ObjectChecker.ErrorType.class);
124 		for (String key : rc.getNames(FSCK)) {
125 			if (equalsIgnoreCase(key, "skipList") //$NON-NLS-1$
126 					|| equalsIgnoreCase(key, "allowLeadingZeroFileMode") //$NON-NLS-1$
127 					|| equalsIgnoreCase(key, "allowInvalidPersonIdent") //$NON-NLS-1$
128 					|| equalsIgnoreCase(key, "safeForWindows") //$NON-NLS-1$
129 					|| equalsIgnoreCase(key, "safeForMacOS")) { //$NON-NLS-1$
130 				continue;
131 			}
132 
133 			ObjectChecker.ErrorType id = FsckKeyNameHolder.parse(key);
134 			if (id != null) {
135 				switch (rc.getEnum(FSCK, null, key, FsckMode.ERROR)) {
136 				case ERROR:
137 					ignore.remove(id);
138 					break;
139 				case WARN:
140 				case IGNORE:
141 					ignore.add(id);
142 					break;
143 				}
144 				set.add(id);
145 			}
146 		}
147 		if (!set.contains(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE)
148 				&& rc.getBoolean(FSCK, "allowLeadingZeroFileMode", false)) { //$NON-NLS-1$
149 			ignore.add(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE);
150 		}
151 
152 		allowTipSha1InWant = rc.getBoolean(
153 				"uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
154 		allowReachableSha1InWant = rc.getBoolean(
155 				"uploadpack", "allowreachablesha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
156 		hideRefs = rc.getStringList("uploadpack", null, "hiderefs"); //$NON-NLS-1$ //$NON-NLS-2$
157 	}
158 
159 	/**
160 	 * Create checker to verify fetched objects
161 	 *
162 	 * @return checker to verify fetched objects, or null if checking is not
163 	 *         enabled in the repository configuration.
164 	 * @since 3.6
165 	 */
166 	@Nullable
167 	public ObjectChecker newObjectChecker() {
168 		return newObjectChecker(fetchFsck);
169 	}
170 
171 	/**
172 	 * Create checker to verify objects pushed into this repository
173 	 *
174 	 * @return checker to verify objects pushed into this repository, or null if
175 	 *         checking is not enabled in the repository configuration.
176 	 * @since 4.2
177 	 */
178 	@Nullable
179 	public ObjectChecker newReceiveObjectChecker() {
180 		return newObjectChecker(receiveFsck);
181 	}
182 
183 	private ObjectChecker newObjectChecker(boolean check) {
184 		if (!check) {
185 			return null;
186 		}
187 		return new ObjectChecker()
188 			.setIgnore(ignore)
189 			.setAllowInvalidPersonIdent(allowInvalidPersonIdent)
190 			.setSafeForWindows(safeForWindows)
191 			.setSafeForMacOS(safeForMacOS)
192 			.setSkipList(skipList());
193 	}
194 
195 	private ObjectIdSet skipList() {
196 		if (fsckSkipList != null && !fsckSkipList.isEmpty()) {
197 			return new LazyObjectIdSetFile(new File(fsckSkipList));
198 		}
199 		return null;
200 	}
201 
202 	/**
203 	 * Whether to allow clients to request non-advertised tip SHA-1s
204 	 *
205 	 * @return allow clients to request non-advertised tip SHA-1s?
206 	 * @since 3.1
207 	 */
208 	public boolean isAllowTipSha1InWant() {
209 		return allowTipSha1InWant;
210 	}
211 
212 	/**
213 	 * Whether to allow clients to request non-tip SHA-1s
214 	 *
215 	 * @return allow clients to request non-tip SHA-1s?
216 	 * @since 4.1
217 	 */
218 	public boolean isAllowReachableSha1InWant() {
219 		return allowReachableSha1InWant;
220 	}
221 
222 	/**
223 	 * Get {@link org.eclipse.jgit.transport.RefFilter} respecting configured
224 	 * hidden refs.
225 	 *
226 	 * @return {@link org.eclipse.jgit.transport.RefFilter} respecting
227 	 *         configured hidden refs.
228 	 * @since 3.1
229 	 */
230 	public RefFilter getRefFilter() {
231 		if (hideRefs.length == 0)
232 			return RefFilter.DEFAULT;
233 
234 		return new RefFilter() {
235 			@Override
236 			public Map<String, Ref> filter(Map<String, Ref> refs) {
237 				Map<String, Ref> result = new HashMap<>();
238 				for (Map.Entry<String, Ref> e : refs.entrySet()) {
239 					boolean add = true;
240 					for (String hide : hideRefs) {
241 						if (e.getKey().equals(hide) || prefixMatch(hide, e.getKey())) {
242 							add = false;
243 							break;
244 						}
245 					}
246 					if (add)
247 						result.put(e.getKey(), e.getValue());
248 				}
249 				return result;
250 			}
251 
252 			private boolean prefixMatch(String p, String s) {
253 				return p.charAt(p.length() - 1) == '/' && s.startsWith(p);
254 			}
255 		};
256 	}
257 
258 	static class FsckKeyNameHolder {
259 		private static final Map<String, ObjectChecker.ErrorType> errors;
260 
261 		static {
262 			errors = new HashMap<>();
263 			for (ObjectChecker.ErrorType m : ObjectChecker.ErrorType.values()) {
264 				errors.put(keyNameFor(m.name()), m);
265 			}
266 		}
267 
268 		@Nullable
269 		static ObjectChecker.ErrorType parse(String key) {
270 			return errors.get(toLowerCase(key));
271 		}
272 
273 		private static String keyNameFor(String name) {
274 			StringBuilder r = new StringBuilder(name.length());
275 			for (int i = 0; i < name.length(); i++) {
276 				char c = name.charAt(i);
277 				if (c != '_') {
278 					r.append(c);
279 				}
280 			}
281 			return toLowerCase(r.toString());
282 		}
283 
284 		private FsckKeyNameHolder() {
285 		}
286 	}
287 }