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