aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java116
1 files changed, 57 insertions, 59 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java b/org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java
index bcac14b5ff..2039191b8c 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/attributes/AttributesRule.java
@@ -1,48 +1,15 @@
/*
- * Copyright (C) 2010, Red Hat Inc.
- * and other copyright owners as documented in the project's IP log.
+ * Copyright (C) 2010, 2017 Red Hat Inc. and others
*
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
*
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.attributes;
-import static org.eclipse.jgit.ignore.internal.IMatcher.NO_MATCH;
+import static org.eclipse.jgit.ignore.IMatcher.NO_MATCH;
import java.util.ArrayList;
import java.util.Collections;
@@ -51,13 +18,13 @@ import java.util.List;
import org.eclipse.jgit.attributes.Attribute.State;
import org.eclipse.jgit.errors.InvalidPatternException;
import org.eclipse.jgit.ignore.FastIgnoreRule;
-import org.eclipse.jgit.ignore.internal.IMatcher;
+import org.eclipse.jgit.ignore.IMatcher;
import org.eclipse.jgit.ignore.internal.PathMatcher;
/**
* A single attributes rule corresponding to one line in a .gitattributes file.
*
- * Inspiration from: {@link FastIgnoreRule}
+ * Inspiration from: {@link org.eclipse.jgit.ignore.FastIgnoreRule}
*
* @since 3.7
*/
@@ -71,7 +38,7 @@ public class AttributesRule {
private static List<Attribute> parseAttributes(String attributesLine) {
// the C implementation oddly enough allows \r between attributes too.
- ArrayList<Attribute> result = new ArrayList<Attribute>();
+ ArrayList<Attribute> result = new ArrayList<>();
for (String attribute : attributesLine.split(ATTRIBUTES_SPLIT_REGEX)) {
attribute = attribute.trim();
if (attribute.length() == 0)
@@ -84,7 +51,14 @@ public class AttributesRule {
continue;
}
- final int equalsIndex = attribute.indexOf("="); //$NON-NLS-1$
+ if (attribute.startsWith("!")) {//$NON-NLS-1$
+ if (attribute.length() > 1)
+ result.add(new Attribute(attribute.substring(1),
+ State.UNSPECIFIED));
+ continue;
+ }
+
+ final int equalsIndex = attribute.indexOf('=');
if (equalsIndex == -1)
result.add(new Attribute(attribute, State.SET));
else {
@@ -102,10 +76,11 @@ public class AttributesRule {
private final String pattern;
private final List<Attribute> attributes;
- private boolean nameOnly;
- private boolean dirOnly;
+ private final boolean nameOnly;
- private IMatcher matcher;
+ private final boolean dirOnly;
+
+ private final IMatcher matcher;
/**
* Create a new attribute rule with the given pattern. Assumes that the
@@ -121,43 +96,50 @@ public class AttributesRule {
*/
public AttributesRule(String pattern, String attributes) {
this.attributes = parseAttributes(attributes);
- nameOnly = false;
- dirOnly = false;
if (pattern.endsWith("/")) { //$NON-NLS-1$
pattern = pattern.substring(0, pattern.length() - 1);
dirOnly = true;
+ } else {
+ dirOnly = false;
}
- boolean hasSlash = pattern.contains("/"); //$NON-NLS-1$
+ int slashIndex = pattern.indexOf('/');
- if (!hasSlash)
+ if (slashIndex < 0) {
nameOnly = true;
- else if (!pattern.startsWith("/")) { //$NON-NLS-1$
+ } else if (slashIndex == 0) {
+ nameOnly = false;
+ } else {
+ nameOnly = false;
// Contains "/" but does not start with one
// Adding / to the start should not interfere with matching
pattern = "/" + pattern; //$NON-NLS-1$
}
+ IMatcher candidateMatcher = NO_MATCH;
try {
- matcher = PathMatcher.createPathMatcher(pattern,
+ candidateMatcher = PathMatcher.createPathMatcher(pattern,
Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly);
} catch (InvalidPatternException e) {
- matcher = NO_MATCH;
+ // ignore: invalid patterns are silently ignored
}
-
+ this.matcher = candidateMatcher;
this.pattern = pattern;
}
/**
- * @return True if the pattern should match directories only
+ * Whether to match directories only
+ *
+ * @return {@code true} if the pattern should match directories only
+ * @since 4.3
*/
- public boolean dirOnly() {
+ public boolean isDirOnly() {
return dirOnly;
}
/**
- * Returns the attributes.
+ * Return the attributes.
*
* @return an unmodifiable list of attributes (never returns
* <code>null</code>)
@@ -167,6 +149,8 @@ public class AttributesRule {
}
/**
+ * Whether the pattern is only a file name and not a path
+ *
* @return <code>true</code> if the pattern is just a file name and not a
* path
*/
@@ -175,6 +159,8 @@ public class AttributesRule {
}
/**
+ * Get the pattern
+ *
* @return The blob pattern to be used as a matcher (never returns
* <code>null</code>)
*/
@@ -197,7 +183,19 @@ public class AttributesRule {
return false;
if (relativeTarget.length() == 0)
return false;
- boolean match = matcher.matches(relativeTarget, isDirectory);
+ boolean match = matcher.matches(relativeTarget, isDirectory, true);
return match;
}
-} \ No newline at end of file
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(pattern);
+ for (Attribute a : attributes) {
+ sb.append(" "); //$NON-NLS-1$
+ sb.append(a);
+ }
+ return sb.toString();
+
+ }
+}