You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransferConfig.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. package org.eclipse.jgit.transport;
  44. import static org.eclipse.jgit.util.StringUtils.equalsIgnoreCase;
  45. import static org.eclipse.jgit.util.StringUtils.toLowerCase;
  46. import java.io.File;
  47. import java.util.EnumSet;
  48. import java.util.HashMap;
  49. import java.util.Map;
  50. import org.eclipse.jgit.annotations.Nullable;
  51. import org.eclipse.jgit.internal.storage.file.LazyObjectIdSetFile;
  52. import org.eclipse.jgit.lib.Config;
  53. import org.eclipse.jgit.lib.Config.SectionParser;
  54. import org.eclipse.jgit.lib.ObjectChecker;
  55. import org.eclipse.jgit.lib.ObjectIdSet;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.util.SystemReader;
  59. /**
  60. * The standard "transfer", "fetch", "protocol", "receive", and "uploadpack"
  61. * configuration parameters.
  62. */
  63. public class TransferConfig {
  64. private static final String FSCK = "fsck"; //$NON-NLS-1$
  65. /** Key for {@link Config#get(SectionParser)}. */
  66. public static final Config.SectionParser<TransferConfig> KEY =
  67. TransferConfig::new;
  68. /**
  69. * A git configuration value for how to handle a fsck failure of a particular kind.
  70. * Used in e.g. fsck.missingEmail.
  71. * @since 4.9
  72. */
  73. public enum FsckMode {
  74. /**
  75. * Treat it as an error (the default).
  76. */
  77. ERROR,
  78. /**
  79. * Issue a warning (in fact, jgit treats this like IGNORE, but git itself does warn).
  80. */
  81. WARN,
  82. /**
  83. * Ignore the error.
  84. */
  85. IGNORE;
  86. }
  87. /**
  88. * A git configuration variable for which versions of the Git protocol to prefer.
  89. * Used in protocol.version.
  90. */
  91. enum ProtocolVersion {
  92. V0("0"), //$NON-NLS-1$
  93. V2("2"); //$NON-NLS-1$
  94. final String name;
  95. ProtocolVersion(String name) {
  96. this.name = name;
  97. }
  98. @Nullable
  99. static ProtocolVersion parse(@Nullable String name) {
  100. if (name == null) {
  101. return null;
  102. }
  103. for (ProtocolVersion v : ProtocolVersion.values()) {
  104. if (v.name.equals(name)) {
  105. return v;
  106. }
  107. }
  108. return null;
  109. }
  110. }
  111. private final boolean fetchFsck;
  112. private final boolean receiveFsck;
  113. private final String fsckSkipList;
  114. private final EnumSet<ObjectChecker.ErrorType> ignore;
  115. private final boolean allowInvalidPersonIdent;
  116. private final boolean safeForWindows;
  117. private final boolean safeForMacOS;
  118. private final boolean allowRefInWant;
  119. private final boolean allowTipSha1InWant;
  120. private final boolean allowReachableSha1InWant;
  121. private final boolean allowFilter;
  122. final @Nullable ProtocolVersion protocolVersion;
  123. final String[] hideRefs;
  124. /**
  125. * Create a configuration honoring the repository's settings.
  126. *
  127. * @param db
  128. * the repository to read settings from. The repository is not
  129. * retained by the new configuration, instead its settings are
  130. * copied during the constructor.
  131. * @since 5.1.4
  132. */
  133. public TransferConfig(Repository db) {
  134. this(db.getConfig());
  135. }
  136. /**
  137. * Create a configuration honoring settings in a
  138. * {@link org.eclipse.jgit.lib.Config}.
  139. *
  140. * @param rc
  141. * the source to read settings from. The source is not retained
  142. * by the new configuration, instead its settings are copied
  143. * during the constructor.
  144. * @since 5.1.4
  145. */
  146. @SuppressWarnings("nls")
  147. public TransferConfig(Config rc) {
  148. boolean fsck = rc.getBoolean("transfer", "fsckobjects", false);
  149. fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck);
  150. receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck);
  151. fsckSkipList = rc.getString(FSCK, null, "skipList");
  152. allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent",
  153. false);
  154. safeForWindows = rc.getBoolean(FSCK, "safeForWindows",
  155. SystemReader.getInstance().isWindows());
  156. safeForMacOS = rc.getBoolean(FSCK, "safeForMacOS",
  157. SystemReader.getInstance().isMacOS());
  158. ignore = EnumSet.noneOf(ObjectChecker.ErrorType.class);
  159. EnumSet<ObjectChecker.ErrorType> set = EnumSet
  160. .noneOf(ObjectChecker.ErrorType.class);
  161. for (String key : rc.getNames(FSCK)) {
  162. if (equalsIgnoreCase(key, "skipList")
  163. || equalsIgnoreCase(key, "allowLeadingZeroFileMode")
  164. || equalsIgnoreCase(key, "allowInvalidPersonIdent")
  165. || equalsIgnoreCase(key, "safeForWindows")
  166. || equalsIgnoreCase(key, "safeForMacOS")) {
  167. continue;
  168. }
  169. ObjectChecker.ErrorType id = FsckKeyNameHolder.parse(key);
  170. if (id != null) {
  171. switch (rc.getEnum(FSCK, null, key, FsckMode.ERROR)) {
  172. case ERROR:
  173. ignore.remove(id);
  174. break;
  175. case WARN:
  176. case IGNORE:
  177. ignore.add(id);
  178. break;
  179. }
  180. set.add(id);
  181. }
  182. }
  183. if (!set.contains(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE)
  184. && rc.getBoolean(FSCK, "allowLeadingZeroFileMode", false)) {
  185. ignore.add(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE);
  186. }
  187. allowRefInWant = rc.getBoolean("uploadpack", "allowrefinwant", false);
  188. allowTipSha1InWant = rc.getBoolean(
  189. "uploadpack", "allowtipsha1inwant", false);
  190. allowReachableSha1InWant = rc.getBoolean(
  191. "uploadpack", "allowreachablesha1inwant", false);
  192. allowFilter = rc.getBoolean(
  193. "uploadpack", "allowfilter", false);
  194. protocolVersion = ProtocolVersion.parse(rc.getString("protocol", null, "version"));
  195. hideRefs = rc.getStringList("uploadpack", null, "hiderefs");
  196. }
  197. /**
  198. * Create checker to verify fetched objects
  199. *
  200. * @return checker to verify fetched objects, or null if checking is not
  201. * enabled in the repository configuration.
  202. * @since 3.6
  203. */
  204. @Nullable
  205. public ObjectChecker newObjectChecker() {
  206. return newObjectChecker(fetchFsck);
  207. }
  208. /**
  209. * Create checker to verify objects pushed into this repository
  210. *
  211. * @return checker to verify objects pushed into this repository, or null if
  212. * checking is not enabled in the repository configuration.
  213. * @since 4.2
  214. */
  215. @Nullable
  216. public ObjectChecker newReceiveObjectChecker() {
  217. return newObjectChecker(receiveFsck);
  218. }
  219. private ObjectChecker newObjectChecker(boolean check) {
  220. if (!check) {
  221. return null;
  222. }
  223. return new ObjectChecker()
  224. .setIgnore(ignore)
  225. .setAllowInvalidPersonIdent(allowInvalidPersonIdent)
  226. .setSafeForWindows(safeForWindows)
  227. .setSafeForMacOS(safeForMacOS)
  228. .setSkipList(skipList());
  229. }
  230. private ObjectIdSet skipList() {
  231. if (fsckSkipList != null && !fsckSkipList.isEmpty()) {
  232. return new LazyObjectIdSetFile(new File(fsckSkipList));
  233. }
  234. return null;
  235. }
  236. /**
  237. * Whether to allow clients to request non-advertised tip SHA-1s
  238. *
  239. * @return allow clients to request non-advertised tip SHA-1s?
  240. * @since 3.1
  241. */
  242. public boolean isAllowTipSha1InWant() {
  243. return allowTipSha1InWant;
  244. }
  245. /**
  246. * Whether to allow clients to request non-tip SHA-1s
  247. *
  248. * @return allow clients to request non-tip SHA-1s?
  249. * @since 4.1
  250. */
  251. public boolean isAllowReachableSha1InWant() {
  252. return allowReachableSha1InWant;
  253. }
  254. /**
  255. * @return true if clients are allowed to specify a "filter" line
  256. * @since 5.0
  257. */
  258. public boolean isAllowFilter() {
  259. return allowFilter;
  260. }
  261. /**
  262. * @return true if clients are allowed to specify a "want-ref" line
  263. * @since 5.1
  264. */
  265. public boolean isAllowRefInWant() {
  266. return allowRefInWant;
  267. }
  268. /**
  269. * Get {@link org.eclipse.jgit.transport.RefFilter} respecting configured
  270. * hidden refs.
  271. *
  272. * @return {@link org.eclipse.jgit.transport.RefFilter} respecting
  273. * configured hidden refs.
  274. * @since 3.1
  275. */
  276. public RefFilter getRefFilter() {
  277. if (hideRefs.length == 0)
  278. return RefFilter.DEFAULT;
  279. return new RefFilter() {
  280. @Override
  281. public Map<String, Ref> filter(Map<String, Ref> refs) {
  282. Map<String, Ref> result = new HashMap<>();
  283. for (Map.Entry<String, Ref> e : refs.entrySet()) {
  284. boolean add = true;
  285. for (String hide : hideRefs) {
  286. if (e.getKey().equals(hide) || prefixMatch(hide, e.getKey())) {
  287. add = false;
  288. break;
  289. }
  290. }
  291. if (add)
  292. result.put(e.getKey(), e.getValue());
  293. }
  294. return result;
  295. }
  296. private boolean prefixMatch(String p, String s) {
  297. return p.charAt(p.length() - 1) == '/' && s.startsWith(p);
  298. }
  299. };
  300. }
  301. /**
  302. * Like {@code getRefFilter() == RefFilter.DEFAULT}, but faster.
  303. *
  304. * @return {@code true} if no ref filtering is needed because there
  305. * are no configured hidden refs.
  306. */
  307. boolean hasDefaultRefFilter() {
  308. return hideRefs.length == 0;
  309. }
  310. static class FsckKeyNameHolder {
  311. private static final Map<String, ObjectChecker.ErrorType> errors;
  312. static {
  313. errors = new HashMap<>();
  314. for (ObjectChecker.ErrorType m : ObjectChecker.ErrorType.values()) {
  315. errors.put(keyNameFor(m.name()), m);
  316. }
  317. }
  318. @Nullable
  319. static ObjectChecker.ErrorType parse(String key) {
  320. return errors.get(toLowerCase(key));
  321. }
  322. private static String keyNameFor(String name) {
  323. StringBuilder r = new StringBuilder(name.length());
  324. for (int i = 0; i < name.length(); i++) {
  325. char c = name.charAt(i);
  326. if (c != '_') {
  327. r.append(c);
  328. }
  329. }
  330. return toLowerCase(r.toString());
  331. }
  332. private FsckKeyNameHolder() {
  333. }
  334. }
  335. }