summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorGustav Karlsson <gustav.karlsson@tieto.com>2013-03-29 00:07:49 +0100
committerGustav Karlsson <gustav.karlsson@tieto.com>2013-04-06 18:23:33 +0200
commitb3e9626743892fb33350264a86a0033d5a9756be (patch)
tree89d63dc4f799c2399c5ea953b96278d15dfc9337 /org.eclipse.jgit
parent5d446f410d7044fba165ad3deee7ac83864f0e96 (diff)
downloadjgit-b3e9626743892fb33350264a86a0033d5a9756be.tar.gz
jgit-b3e9626743892fb33350264a86a0033d5a9756be.zip
Added characters to be escaped in file name patterns
Originally, characters could not be escaped in FileNameMatcher patterns. This breaks file name matching when escaped brackets "\[" and "\]" are used in the pattern. A fix has been implemented to allow for any character to be escaped by prepending it with a '\' Bug: 340715 Change-Id: Ie46fd211931fa09ef3a6a712bd1da3d7fb64c5e3 Signed-off-by: Gustav Karlsson <gustav.karlsson@tieto.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/fnmatch/FileNameMatcher.java64
1 files changed, 44 insertions, 20 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/fnmatch/FileNameMatcher.java b/org.eclipse.jgit/src/org/eclipse/jgit/fnmatch/FileNameMatcher.java
index 22840fb71c..02e4235fd0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/fnmatch/FileNameMatcher.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/fnmatch/FileNameMatcher.java
@@ -60,9 +60,9 @@ import org.eclipse.jgit.errors.NoClosingBracketException;
* <p>
* Supported are the wildcard characters * and ? and groups with:
* <ul>
- * <li> characters e.g. [abc]</li>
- * <li> ranges e.g. [a-z]</li>
- * <li> the following character classes
+ * <li>characters e.g. [abc]</li>
+ * <li>ranges e.g. [a-z]</li>
+ * <li>the following character classes
* <ul>
* <li>[:alnum:]</li>
* <li>[:alpha:]</li>
@@ -78,9 +78,10 @@ import org.eclipse.jgit.errors.NoClosingBracketException;
* <li>[:word:]</li>
* <li>[:xdigit:]</li>
* </ul>
- * e. g. [[:xdigit:]] </li>
+ * e. g. [[:xdigit:]]</li>
* </ul>
* </p>
+ * Any character can be escaped by prepending it with a \
*/
public class FileNameMatcher {
static final List<Head> EMPTY_HEAD_LIST = Collections.emptyList();
@@ -199,7 +200,7 @@ public class FileNameMatcher {
int groupEnd = -1;
while (groupEnd == -1) {
- final int possibleGroupEnd = pattern.indexOf(']',
+ final int possibleGroupEnd = indexOfUnescaped(pattern, ']',
firstValidEndBracketIndex);
if (possibleGroupEnd == -1)
throw new NoClosingBracketException(indexOfStartBracket, "[", //$NON-NLS-1$
@@ -238,7 +239,7 @@ public class FileNameMatcher {
int currentIndex = 0;
List<AbstractHead> heads = new ArrayList<AbstractHead>();
while (currentIndex < pattern.length()) {
- final int groupStart = pattern.indexOf('[', currentIndex);
+ final int groupStart = indexOfUnescaped(pattern, '[', currentIndex);
if (groupStart == -1) {
final String patternPart = pattern.substring(currentIndex);
heads.addAll(createSimpleHeads(patternPart,
@@ -264,24 +265,35 @@ public class FileNameMatcher {
final String patternPart, final Character invalidWildgetCharacter) {
final List<AbstractHead> heads = new ArrayList<AbstractHead>(
patternPart.length());
+
+ boolean escaped = false;
for (int i = 0; i < patternPart.length(); i++) {
final char c = patternPart.charAt(i);
- switch (c) {
- case '*': {
- final AbstractHead head = createWildCardHead(
- invalidWildgetCharacter, true);
- heads.add(head);
- break;
- }
- case '?': {
- final AbstractHead head = createWildCardHead(
- invalidWildgetCharacter, false);
- heads.add(head);
- break;
- }
- default:
+ if (escaped) {
final CharacterHead head = new CharacterHead(c);
heads.add(head);
+ escaped = false;
+ } else {
+ switch (c) {
+ case '*': {
+ final AbstractHead head = createWildCardHead(
+ invalidWildgetCharacter, true);
+ heads.add(head);
+ break;
+ }
+ case '?': {
+ final AbstractHead head = createWildCardHead(
+ invalidWildgetCharacter, false);
+ heads.add(head);
+ break;
+ }
+ case '\\':
+ escaped = true;
+ break;
+ default:
+ final CharacterHead head = new CharacterHead(c);
+ heads.add(head);
+ }
}
}
return heads;
@@ -317,6 +329,18 @@ public class FileNameMatcher {
heads = newHeads;
}
+ private static int indexOfUnescaped(final String searchString,
+ final char ch, final int fromIndex) {
+ for (int i = fromIndex; i < searchString.length(); i++) {
+ char current = searchString.charAt(i);
+ if (current == ch)
+ return i;
+ if (current == '\\')
+ i++; // Skip the next char as it is escaped }
+ }
+ return -1;
+ }
+
/**
*
* @param stringToMatch