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 8.5KB

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