/* * Copyright (C) 2014, 2021 Andrey Loskutov and others * * 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. * * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.ignore; import static org.eclipse.jgit.ignore.IMatcher.NO_MATCH; import static org.eclipse.jgit.ignore.internal.Strings.isDirectoryPattern; import static org.eclipse.jgit.ignore.internal.Strings.stripTrailing; import static org.eclipse.jgit.ignore.internal.Strings.stripTrailingWhitespace; import java.text.MessageFormat; import org.eclipse.jgit.errors.InvalidPatternException; import org.eclipse.jgit.ignore.internal.PathMatcher; import org.eclipse.jgit.internal.JGitText; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * "Fast" (compared with IgnoreRule) git ignore rule implementation supporting * also double star {@code **} pattern. *

* This class is immutable and thread safe. * * @since 3.6 */ public class FastIgnoreRule { private static final Logger LOG = LoggerFactory .getLogger(FastIgnoreRule.class); /** * Character used as default path separator for ignore entries */ public static final char PATH_SEPARATOR = '/'; private IMatcher matcher; private boolean inverse; private boolean dirOnly; /** * Constructor for FastIgnoreRule * * @param pattern * ignore pattern as described in git manual. If pattern is invalid or is not a pattern * (comment), this rule doesn't match anything. */ public FastIgnoreRule(String pattern) { this(); try { parse(pattern); } catch (InvalidPatternException e) { LOG.error(MessageFormat.format(JGitText.get().badIgnorePattern, e.getPattern()), e); } } FastIgnoreRule() { matcher = IMatcher.NO_MATCH; } void parse(String pattern) throws InvalidPatternException { if (pattern == null) { throw new IllegalArgumentException("Pattern must not be null!"); //$NON-NLS-1$ } if (pattern.length() == 0) { dirOnly = false; inverse = false; this.matcher = NO_MATCH; return; } inverse = pattern.charAt(0) == '!'; if (inverse) { pattern = pattern.substring(1); if (pattern.length() == 0) { dirOnly = false; this.matcher = NO_MATCH; return; } } if (pattern.charAt(0) == '#') { this.matcher = NO_MATCH; dirOnly = false; return; } if (pattern.charAt(0) == '\\' && pattern.length() > 1) { char next = pattern.charAt(1); if (next == '!' || next == '#') { // remove backslash escaping first special characters pattern = pattern.substring(1); } } dirOnly = isDirectoryPattern(pattern); if (dirOnly) { pattern = stripTrailingWhitespace(pattern); pattern = stripTrailing(pattern, PATH_SEPARATOR); if (pattern.length() == 0) { this.matcher = NO_MATCH; return; } } this.matcher = PathMatcher.createPathMatcher(pattern, Character.valueOf(PATH_SEPARATOR), dirOnly); } /** * Returns true if a match was made.
* This function does NOT return the actual ignore status of the target! * Please consult {@link #getResult()} for the negation status. The actual * ignore status may be true or false depending on whether this rule is an * ignore rule or a negation rule. * * @param path * Name pattern of the file, relative to the base directory of * this rule * @param directory * Whether the target file is a directory or not * @return True if a match was made. This does not necessarily mean that the * target is ignored. Call {@link #getResult() getResult()} for the * result. */ public boolean isMatch(String path, boolean directory) { return isMatch(path, directory, false); } /** * Returns true if a match was made.
* This function does NOT return the actual ignore status of the target! * Please consult {@link #getResult()} for the negation status. The actual * ignore status may be true or false depending on whether this rule is an * ignore rule or a negation rule. * * @param path * Name pattern of the file, relative to the base directory of * this rule * @param directory * Whether the target file is a directory or not * @param pathMatch * {@code true} if the match is for the full path: see * {@link IMatcher#matches(String, int, int)} * @return True if a match was made. This does not necessarily mean that the * target is ignored. Call {@link #getResult() getResult()} for the * result. * @since 4.11 */ public boolean isMatch(String path, boolean directory, boolean pathMatch) { if (path == null) return false; if (path.length() == 0) return false; boolean match = matcher.matches(path, directory, pathMatch); return match; } /** * Whether the pattern is just a file name and not a path * * @return {@code true} if the pattern is just a file name and not a path */ public boolean getNameOnly() { return !(matcher instanceof PathMatcher); } /** * Whether the pattern should match directories only * * @return {@code true} if the pattern should match directories only */ public boolean dirOnly() { return dirOnly; } /** * Indicates whether the rule is non-negation or negation. * * @return True if the pattern had a "!" in front of it */ public boolean getNegation() { return inverse; } /** * Indicates whether the rule is non-negation or negation. * * @return True if the target is to be ignored, false otherwise. */ public boolean getResult() { return !inverse; } /** * Whether the rule never matches * * @return {@code true} if the rule never matches (comment line or broken * pattern) * @since 4.1 */ public boolean isEmpty() { return matcher == NO_MATCH; } @Override public String toString() { StringBuilder sb = new StringBuilder(); if (inverse) sb.append('!'); sb.append(matcher); if (dirOnly) sb.append(PATH_SEPARATOR); return sb.toString(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (inverse ? 1231 : 1237); result = prime * result + (dirOnly ? 1231 : 1237); result = prime * result + ((matcher == null) ? 0 : matcher.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof FastIgnoreRule)) return false; FastIgnoreRule other = (FastIgnoreRule) obj; if (inverse != other.inverse) return false; if (dirOnly != other.dirOnly) return false; return matcher.equals(other.matcher); } } X-Content-Type-Options: nosniff Content-Security-Policy: default-src 'none' Content-Type: text/plain; charset=UTF-8 Content-Length: 2478 Content-Disposition: inline; filename="IMatcher.java" Last-Modified: Wed, 09 Jul 2025 23:49:35 GMT Expires: Wed, 09 Jul 2025 23:54:35 GMT ETag: "3cbb069ff9874ffbc43e1f9cd6f9c984d3eeb7e1" /* * Copyright (C) 2014, 2020 Andrey Loskutov and others * * 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. * * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.ignore; import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.errors.InvalidPatternException; import org.eclipse.jgit.ignore.internal.PathMatcher; /** * Generic path matcher. * * @since 5.7 */ public interface IMatcher { /** * Matcher that does not match any pattern. */ public static final IMatcher NO_MATCH = new IMatcher() { @Override public boolean matches(String path, boolean assumeDirectory, boolean pathMatch) { return false; } @Override public boolean matches(String segment, int startIncl, int endExcl) { return false; } }; /** * Creates a path matcher for the given pattern. A pattern may contain the * wildcards "?", "*", and "**". The directory separator is '/'. * * @param pattern * to match * @param dirOnly * whether to match only directories * @return a matcher for the given pattern * @throws InvalidPatternException * if the pattern is invalid */ @NonNull public static IMatcher createPathMatcher(@NonNull String pattern, boolean dirOnly) throws InvalidPatternException { return PathMatcher.createPathMatcher(pattern, Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly); } /** * Matches entire given string * * @param path * string which is not null, but might be empty * @param assumeDirectory * true to assume this path as directory (even if it doesn't end * with a slash) * @param pathMatch * {@code true} if the match is for the full path: prefix-only * matches are not allowed * @return true if this matcher pattern matches given string */ boolean matches(String path, boolean assumeDirectory, boolean pathMatch); /** * Matches only part of given string * * @param segment * string which is not null, but might be empty * @param startIncl * start index, inclusive * @param endExcl * end index, exclusive * @return true if this matcher pattern matches given string */ boolean matches(String segment, int startIncl, int endExcl); } X-Content-Type-Options: nosniff Content-Security-Policy: default-src 'none' Content-Type: text/plain; charset=UTF-8 Content-Length: 5649 Content-Disposition: inline; filename="IgnoreNode.java" Last-Modified: Wed, 09 Jul 2025 23:49:35 GMT Expires: Wed, 09 Jul 2025 23:54:35 GMT ETag: "33dceb0717702053d84622c1713ed48cc487c186" /* * Copyright (C) 2010, 2021 Red Hat Inc. and others * * 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. * * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.ignore; import static java.nio.charset.StandardCharsets.UTF_8; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.errors.InvalidPatternException; import org.eclipse.jgit.internal.JGitText; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Represents a bundle of ignore rules inherited from a base directory. * * This class is not thread safe, it maintains state about the last match. */ public class IgnoreNode { private static final Logger LOG = LoggerFactory.getLogger(IgnoreNode.class); /** Result from {@link IgnoreNode#isIgnored(String, boolean)}. */ public enum MatchResult { /** The file is not ignored, due to a rule saying its not ignored. */ NOT_IGNORED, /** The file is ignored due to a rule in this node. */ IGNORED, /** The ignore status is unknown, check inherited rules. */ CHECK_PARENT, /** * The first previous (parent) ignore rule match (if any) should be * negated, and then inherited rules applied. * * @since 3.6 */ CHECK_PARENT_NEGATE_FIRST_MATCH; } /** The rules that have been parsed into this node. */ private final List rules; /** * Create an empty ignore node with no rules. */ public IgnoreNode() { this(new ArrayList<>()); } /** * Create an ignore node with given rules. * * @param rules * list of rules. */ public IgnoreNode(List rules) { this.rules = rules; } /** * Parse files according to gitignore standards. * * @param in * input stream holding the standard ignore format. The caller is * responsible for closing the stream. * @throws java.io.IOException * Error thrown when reading an ignore file. */ public void parse(InputStream in) throws IOException { parse(null, in); } /** * Parse files according to gitignore standards. * * @param sourceName * identifying the source of the stream * @param in * input stream holding the standard ignore format. The caller is * responsible for closing the stream. * @throws java.io.IOException * Error thrown when reading an ignore file. * @since 5.11 */ public void parse(String sourceName, InputStream in) throws IOException { BufferedReader br = asReader(in); String txt; int lineNumber = 1; while ((txt = br.readLine()) != null) { if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) { //$NON-NLS-1$ //$NON-NLS-2$ FastIgnoreRule rule = new FastIgnoreRule(); try { rule.parse(txt); } catch (InvalidPatternException e) { if (sourceName != null) { LOG.error(MessageFormat.format( JGitText.get().badIgnorePatternFull, sourceName, Integer.toString(lineNumber), e.getPattern(), e.getLocalizedMessage()), e); } else { LOG.error(MessageFormat.format( JGitText.get().badIgnorePattern, e.getPattern()), e); } } if (!rule.isEmpty()) { rules.add(rule); } } lineNumber++; } } private static BufferedReader asReader(InputStream in) { return new BufferedReader(new InputStreamReader(in, UTF_8)); } /** * Get list of all ignore rules held by this node * * @return list of all ignore rules held by this node */ public List getRules() { return Collections.unmodifiableList(rules); } /** * Determine if an entry path matches an ignore rule. * * @param entryPath * the path to test. The path must be relative to this ignore * node's own repository path, and in repository path format * (uses '/' and not '\'). * @param isDirectory * true if the target item is a directory. * @return status of the path. */ public MatchResult isIgnored(String entryPath, boolean isDirectory) { final Boolean result = checkIgnored(entryPath, isDirectory); if (result == null) { return MatchResult.CHECK_PARENT; } return result.booleanValue() ? MatchResult.IGNORED : MatchResult.NOT_IGNORED; } /** * Determine if an entry path matches an ignore rule. * * @param entryPath * the path to test. The path must be relative to this ignore * node's own repository path, and in repository path format * (uses '/' and not '\'). * @param isDirectory * true if the target item is a directory. * @return Boolean.TRUE, if the entry is ignored; Boolean.FALSE, if the * entry is forced to be not ignored (negated match); or null, if * undetermined * @since 4.11 */ public @Nullable Boolean checkIgnored(String entryPath, boolean isDirectory) { // Parse rules in the reverse order that they were read because later // rules have higher priority for (int i = rules.size() - 1; i > -1; i--) { FastIgnoreRule rule = rules.get(i); if (rule.isMatch(entryPath, isDirectory, true)) { return Boolean.valueOf(rule.getResult()); } } return null; } @Override public String toString() { return rules.toString(); } } Content-Type: text/plain; charset=UTF-8 Content-Length: 5649 Content-Disposition: inline; filename="IgnoreNode.java" Last-Modified: Wed, 09 Jul 2025 23:49:35 GMT Expires: Wed, 09 Jul 2025 23:54:35 GMT ETag: "18f54d1c1c0f9bc41321ee87cf55d2c40547105a" /org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/

/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/