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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. private final boolean allowSidebandAll;
  123. final @Nullable ProtocolVersion protocolVersion;
  124. final String[] hideRefs;
  125. /**
  126. * Create a configuration honoring the repository's settings.
  127. *
  128. * @param db
  129. * the repository to read settings from. The repository is not
  130. * retained by the new configuration, instead its settings are
  131. * copied during the constructor.
  132. * @since 5.1.4
  133. */
  134. public TransferConfig(Repository db) {
  135. this(db.getConfig());
  136. }
  137. /**
  138. * Create a configuration honoring settings in a
  139. * {@link org.eclipse.jgit.lib.Config}.
  140. *
  141. * @param rc
  142. * the source to read settings from. The source is not retained
  143. * by the new configuration, instead its settings are copied
  144. * during the constructor.
  145. * @since 5.1.4
  146. */
  147. @SuppressWarnings("nls")
  148. public TransferConfig(Config rc) {
  149. boolean fsck = rc.getBoolean("transfer", "fsckobjects", false);
  150. fetchFsck = rc.getBoolean("fetch", "fsckobjects", fsck);
  151. receiveFsck = rc.getBoolean("receive", "fsckobjects", fsck);
  152. fsckSkipList = rc.getString(FSCK, null, "skipList");
  153. allowInvalidPersonIdent = rc.getBoolean(FSCK, "allowInvalidPersonIdent",
  154. false);
  155. safeForWindows = rc.getBoolean(FSCK, "safeForWindows",
  156. SystemReader.getInstance().isWindows());
  157. safeForMacOS = rc.getBoolean(FSCK, "safeForMacOS",
  158. SystemReader.getInstance().isMacOS());
  159. ignore = EnumSet.noneOf(ObjectChecker.ErrorType.class);
  160. EnumSet<ObjectChecker.ErrorType> set = EnumSet
  161. .noneOf(ObjectChecker.ErrorType.class);
  162. for (String key : rc.getNames(FSCK)) {
  163. if (equalsIgnoreCase(key, "skipList")
  164. || equalsIgnoreCase(key, "allowLeadingZeroFileMode")
  165. || equalsIgnoreCase(key, "allowInvalidPersonIdent")
  166. || equalsIgnoreCase(key, "safeForWindows")
  167. || equalsIgnoreCase(key, "safeForMacOS")) {
  168. continue;
  169. }
  170. ObjectChecker.ErrorType id = FsckKeyNameHolder.parse(key);
  171. if (id != null) {
  172. switch (rc.getEnum(FSCK, null, key, FsckMode.ERROR)) {
  173. case ERROR:
  174. ignore.remove(id);
  175. break;
  176. case WARN:
  177. case IGNORE:
  178. ignore.add(id);
  179. break;
  180. }
  181. set.add(id);
  182. }
  183. }
  184. if (!set.contains(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE)
  185. && rc.getBoolean(FSCK, "allowLeadingZeroFileMode", false)) {
  186. ignore.add(ObjectChecker.ErrorType.ZERO_PADDED_FILEMODE);
  187. }
  188. allowRefInWant = rc.getBoolean("uploadpack", "allowrefinwant", false);
  189. allowTipSha1InWant = rc.getBoolean(
  190. "uploadpack", "allowtipsha1inwant", false);
  191. allowReachableSha1InWant = rc.getBoolean(
  192. "uploadpack", "allowreachablesha1inwant", false);
  193. allowFilter = rc.getBoolean(
  194. "uploadpack", "allowfilter", false);
  195. protocolVersion = ProtocolVersion.parse(rc.getString("protocol", null, "version"));
  196. hideRefs = rc.getStringList("uploadpack", null, "hiderefs");
  197. allowSidebandAll = rc.getBoolean(
  198. "uploadpack", "allowsidebandall", false);
  199. }
  200. /**
  201. * Create checker to verify fetched objects
  202. *
  203. * @return checker to verify fetched objects, or null if checking is not
  204. * enabled in the repository configuration.
  205. * @since 3.6
  206. */
  207. @Nullable
  208. public ObjectChecker newObjectChecker() {
  209. return newObjectChecker(fetchFsck);
  210. }
  211. /**
  212. * Create checker to verify objects pushed into this repository
  213. *
  214. * @return checker to verify objects pushed into this repository, or null if
  215. * checking is not enabled in the repository configuration.
  216. * @since 4.2
  217. */
  218. @Nullable
  219. public ObjectChecker newReceiveObjectChecker() {
  220. return newObjectChecker(receiveFsck);
  221. }
  222. private ObjectChecker newObjectChecker(boolean check) {
  223. if (!check) {
  224. return null;
  225. }
  226. return new ObjectChecker()
  227. .setIgnore(ignore)
  228. .setAllowInvalidPersonIdent(allowInvalidPersonIdent)
  229. .setSafeForWindows(safeForWindows)
  230. .setSafeForMacOS(safeForMacOS)
  231. .setSkipList(skipList());
  232. }
  233. private ObjectIdSet skipList() {
  234. if (fsckSkipList != null && !fsckSkipList.isEmpty()) {
  235. return new LazyObjectIdSetFile(new File(fsckSkipList));
  236. }
  237. return null;
  238. }
  239. /**
  240. * Whether to allow clients to request non-advertised tip SHA-1s
  241. *
  242. * @return allow clients to request non-advertised tip SHA-1s?
  243. * @since 3.1
  244. */
  245. public boolean isAllowTipSha1InWant() {
  246. return allowTipSha1InWant;
  247. }
  248. /**
  249. * Whether to allow clients to request non-tip SHA-1s
  250. *
  251. * @return allow clients to request non-tip SHA-1s?
  252. * @since 4.1
  253. */
  254. public boolean isAllowReachableSha1InWant() {
  255. return allowReachableSha1InWant;
  256. }
  257. /**
  258. * @return true if clients are allowed to specify a "filter" line
  259. * @since 5.0
  260. */
  261. public boolean isAllowFilter() {
  262. return allowFilter;
  263. }
  264. /**
  265. * @return true if clients are allowed to specify a "want-ref" line
  266. * @since 5.1
  267. */
  268. public boolean isAllowRefInWant() {
  269. return allowRefInWant;
  270. }
  271. /**
  272. * @return true if clients are allowed to specify a "sideband-all" line
  273. * @since 5.5
  274. */
  275. public boolean isAllowSidebandAll() {
  276. return allowSidebandAll;
  277. }
  278. /**
  279. * Get {@link org.eclipse.jgit.transport.RefFilter} respecting configured
  280. * hidden refs.
  281. *
  282. * @return {@link org.eclipse.jgit.transport.RefFilter} respecting
  283. * configured hidden refs.
  284. * @since 3.1
  285. */
  286. public RefFilter getRefFilter() {
  287. if (hideRefs.length == 0)
  288. return RefFilter.DEFAULT;
  289. return new RefFilter() {
  290. @Override
  291. public Map<String, Ref> filter(Map<String, Ref> refs) {
  292. Map<String, Ref> result = new HashMap<>();
  293. for (Map.Entry<String, Ref> e : refs.entrySet()) {
  294. boolean add = true;
  295. for (String hide : hideRefs) {
  296. if (e.getKey().equals(hide) || prefixMatch(hide, e.getKey())) {
  297. add = false;
  298. break;
  299. }
  300. }
  301. if (add)
  302. result.put(e.getKey(), e.getValue());
  303. }
  304. return result;
  305. }
  306. private boolean prefixMatch(String p, String s) {
  307. return p.charAt(p.length() - 1) == '/' && s.startsWith(p);
  308. }
  309. };
  310. }
  311. /**
  312. * Like {@code getRefFilter() == RefFilter.DEFAULT}, but faster.
  313. *
  314. * @return {@code true} if no ref filtering is needed because there
  315. * are no configured hidden refs.
  316. */
  317. boolean hasDefaultRefFilter() {
  318. return hideRefs.length == 0;
  319. }
  320. static class FsckKeyNameHolder {
  321. private static final Map<String, ObjectChecker.ErrorType> errors;
  322. static {
  323. errors = new HashMap<>();
  324. for (ObjectChecker.ErrorType m : ObjectChecker.ErrorType.values()) {
  325. errors.put(keyNameFor(m.name()), m);
  326. }
  327. }
  328. @Nullable
  329. static ObjectChecker.ErrorType parse(String key) {
  330. return errors.get(toLowerCase(key));
  331. }
  332. private static String keyNameFor(String name) {
  333. StringBuilder r = new StringBuilder(name.length());
  334. for (int i = 0; i < name.length(); i++) {
  335. char c = name.charAt(i);
  336. if (c != '_') {
  337. r.append(c);
  338. }
  339. }
  340. return toLowerCase(r.toString());
  341. }
  342. private FsckKeyNameHolder() {
  343. }
  344. }
  345. }