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.

AttributesRule.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2010, Red Hat 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.attributes;
  44. import static org.eclipse.jgit.ignore.internal.IMatcher.NO_MATCH;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.List;
  48. import org.eclipse.jgit.attributes.Attribute.State;
  49. import org.eclipse.jgit.errors.InvalidPatternException;
  50. import org.eclipse.jgit.ignore.FastIgnoreRule;
  51. import org.eclipse.jgit.ignore.internal.IMatcher;
  52. import org.eclipse.jgit.ignore.internal.PathMatcher;
  53. /**
  54. * A single attributes rule corresponding to one line in a .gitattributes file.
  55. *
  56. * Inspiration from: {@link FastIgnoreRule}
  57. *
  58. * @since 3.7
  59. */
  60. public class AttributesRule {
  61. /**
  62. * regular expression for splitting attributes - space, tab and \r (the C
  63. * implementation oddly enough allows \r between attributes)
  64. * */
  65. private static final String ATTRIBUTES_SPLIT_REGEX = "[ \t\r]"; //$NON-NLS-1$
  66. private static List<Attribute> parseAttributes(String attributesLine) {
  67. // the C implementation oddly enough allows \r between attributes too.
  68. ArrayList<Attribute> result = new ArrayList<>();
  69. for (String attribute : attributesLine.split(ATTRIBUTES_SPLIT_REGEX)) {
  70. attribute = attribute.trim();
  71. if (attribute.length() == 0)
  72. continue;
  73. if (attribute.startsWith("-")) {//$NON-NLS-1$
  74. if (attribute.length() > 1)
  75. result.add(new Attribute(attribute.substring(1),
  76. State.UNSET));
  77. continue;
  78. }
  79. if (attribute.startsWith("!")) {//$NON-NLS-1$
  80. if (attribute.length() > 1)
  81. result.add(new Attribute(attribute.substring(1),
  82. State.UNSPECIFIED));
  83. continue;
  84. }
  85. final int equalsIndex = attribute.indexOf("="); //$NON-NLS-1$
  86. if (equalsIndex == -1)
  87. result.add(new Attribute(attribute, State.SET));
  88. else {
  89. String attributeKey = attribute.substring(0, equalsIndex);
  90. if (attributeKey.length() > 0) {
  91. String attributeValue = attribute
  92. .substring(equalsIndex + 1);
  93. result.add(new Attribute(attributeKey, attributeValue));
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99. private final String pattern;
  100. private final List<Attribute> attributes;
  101. private final boolean nameOnly;
  102. private final boolean dirOnly;
  103. private final IMatcher matcher;
  104. /**
  105. * Create a new attribute rule with the given pattern. Assumes that the
  106. * pattern is already trimmed.
  107. *
  108. * @param pattern
  109. * Base pattern for the attributes rule. This pattern will be
  110. * parsed to generate rule parameters. It can not be
  111. * <code>null</code>.
  112. * @param attributes
  113. * the rule attributes. This string will be parsed to read the
  114. * attributes.
  115. */
  116. public AttributesRule(String pattern, String attributes) {
  117. this.attributes = parseAttributes(attributes);
  118. if (pattern.endsWith("/")) { //$NON-NLS-1$
  119. pattern = pattern.substring(0, pattern.length() - 1);
  120. dirOnly = true;
  121. } else {
  122. dirOnly = false;
  123. }
  124. int slashIndex = pattern.indexOf('/');
  125. if (slashIndex < 0) {
  126. nameOnly = true;
  127. } else if (slashIndex == 0) {
  128. nameOnly = false;
  129. } else {
  130. nameOnly = false;
  131. // Contains "/" but does not start with one
  132. // Adding / to the start should not interfere with matching
  133. pattern = "/" + pattern; //$NON-NLS-1$
  134. }
  135. IMatcher candidateMatcher = NO_MATCH;
  136. try {
  137. candidateMatcher = PathMatcher.createPathMatcher(pattern,
  138. Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly);
  139. } catch (InvalidPatternException e) {
  140. // ignore: invalid patterns are silently ignored
  141. }
  142. this.matcher = candidateMatcher;
  143. this.pattern = pattern;
  144. }
  145. /**
  146. * @return True if the pattern should match directories only
  147. * @since 4.3
  148. */
  149. public boolean isDirOnly() {
  150. return dirOnly;
  151. }
  152. /**
  153. * Returns the attributes.
  154. *
  155. * @return an unmodifiable list of attributes (never returns
  156. * <code>null</code>)
  157. */
  158. public List<Attribute> getAttributes() {
  159. return Collections.unmodifiableList(attributes);
  160. }
  161. /**
  162. * @return <code>true</code> if the pattern is just a file name and not a
  163. * path
  164. */
  165. public boolean isNameOnly() {
  166. return nameOnly;
  167. }
  168. /**
  169. * @return The blob pattern to be used as a matcher (never returns
  170. * <code>null</code>)
  171. */
  172. public String getPattern() {
  173. return pattern;
  174. }
  175. /**
  176. * Returns <code>true</code> if a match was made.
  177. *
  178. * @param relativeTarget
  179. * Name pattern of the file, relative to the base directory of
  180. * this rule
  181. * @param isDirectory
  182. * Whether the target file is a directory or not
  183. * @return True if a match was made.
  184. */
  185. public boolean isMatch(String relativeTarget, boolean isDirectory) {
  186. if (relativeTarget == null)
  187. return false;
  188. if (relativeTarget.length() == 0)
  189. return false;
  190. boolean match = matcher.matches(relativeTarget, isDirectory);
  191. return match;
  192. }
  193. @Override
  194. public String toString() {
  195. StringBuilder sb = new StringBuilder();
  196. sb.append(pattern);
  197. for (Attribute a : attributes) {
  198. sb.append(" "); //$NON-NLS-1$
  199. sb.append(a);
  200. }
  201. return sb.toString();
  202. }
  203. }