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
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 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<Attribute>();
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 * @return True if the pattern should match directories only
166 * @since 4.3
167 */
168 public boolean isDirOnly() {
169 return dirOnly;
170 }
171
172 /**
173 * Returns the attributes.
174 *
175 * @return an unmodifiable list of attributes (never returns
176 * <code>null</code>)
177 */
178 public List<Attribute> getAttributes() {
179 return Collections.unmodifiableList(attributes);
180 }
181
182 /**
183 * @return <code>true</code> if the pattern is just a file name and not a
184 * path
185 */
186 public boolean isNameOnly() {
187 return nameOnly;
188 }
189
190 /**
191 * @return The blob pattern to be used as a matcher (never returns
192 * <code>null</code>)
193 */
194 public String getPattern() {
195 return pattern;
196 }
197
198 /**
199 * Returns <code>true</code> if a match was made.
200 *
201 * @param relativeTarget
202 * Name pattern of the file, relative to the base directory of
203 * this rule
204 * @param isDirectory
205 * Whether the target file is a directory or not
206 * @return True if a match was made.
207 */
208 public boolean isMatch(String relativeTarget, boolean isDirectory) {
209 if (relativeTarget == null)
210 return false;
211 if (relativeTarget.length() == 0)
212 return false;
213 boolean match = matcher.matches(relativeTarget, isDirectory);
214 return match;
215 }
216
217 @Override
218 public String toString() {
219 StringBuilder sb = new StringBuilder();
220 sb.append(pattern);
221 for (Attribute a : attributes) {
222 sb.append(" "); //$NON-NLS-1$
223 sb.append(a);
224 }
225 return sb.toString();
226
227 }
228 }