-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #911
- Loading branch information
Showing
13 changed files
with
435 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
namespace Ignore | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public class Ignore | ||
{ | ||
private readonly List<IgnoreRule> rules; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Ignore"/> class. | ||
/// </summary> | ||
public Ignore() | ||
{ | ||
rules = new List<IgnoreRule>(); | ||
OriginalRules = new List<string>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the list of the original rules passed in to the class ctor. | ||
/// </summary> | ||
public List<string> OriginalRules { get; } | ||
|
||
/// <summary> | ||
/// Adds the given pattern to this <see cref="Ignore"/> instance. | ||
/// </summary> | ||
/// <param name="rule">Gitignore style pattern string.</param> | ||
/// <returns>Current instance of <see cref="Ignore"/>.</returns> | ||
public Ignore Add(string rule) | ||
{ | ||
OriginalRules.Add(rule); | ||
rules.Add(new IgnoreRule(rule)); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the given pattern list to this <see cref="Ignore"/> instance. | ||
/// </summary> | ||
/// <param name="patterns">List of gitignore style pattern strings.</param> | ||
/// <returns>Current instance of <see cref="Ignore"/>.</returns> | ||
public Ignore Add(IEnumerable<string> patterns) | ||
{ | ||
var patternList = patterns.ToList(); | ||
OriginalRules.AddRange(patternList); | ||
patternList.ForEach(pattern => rules.Add(new IgnoreRule(pattern))); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Test whether the input path is ignored as per the rules | ||
/// specified in the class ctor. | ||
/// </summary> | ||
/// <param name="path">File path to consider.</param> | ||
/// <returns>A boolean indicating if the path is ignored.</returns> | ||
public bool IsIgnored(string path) | ||
{ | ||
var ignore = IsPathIgnored(path); | ||
|
||
return ignore; | ||
} | ||
|
||
|
||
public IEnumerable<string> Filter(IEnumerable<string> paths) | ||
{ | ||
var filteredPaths = new List<string>(); | ||
foreach (var path in paths) | ||
{ | ||
var ignore = IsPathIgnored(path); | ||
if (ignore == false) | ||
{ | ||
filteredPaths.Add(path); | ||
} | ||
} | ||
|
||
return filteredPaths; | ||
} | ||
|
||
private bool IsPathIgnored(string path) | ||
{ | ||
var ignore = false; | ||
foreach (var rule in rules) | ||
{ | ||
if (rule.Negate) | ||
{ | ||
if (ignore && rule.IsMatch(path)) | ||
{ | ||
ignore = false; | ||
} | ||
} | ||
else if (!ignore && rule.IsMatch(path)) | ||
{ | ||
ignore = true; | ||
} | ||
} | ||
|
||
return ignore; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
namespace Ignore | ||
{ | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
public class IgnoreRule | ||
{ | ||
private static readonly List<Replacer> Replacers = new List<Replacer> | ||
{ | ||
ReplacerStash.TrailingSpaces, | ||
ReplacerStash.EscapedSpaces, | ||
ReplacerStash.LiteralDot, | ||
ReplacerStash.LiteralPlus, | ||
// probably not needed | ||
// ReplacerStash.Metacharacters, | ||
ReplacerStash.QuestionMark, | ||
ReplacerStash.NonLeadingSingleStar, | ||
ReplacerStash.LeadingSingleStar, | ||
ReplacerStash.LeadingDoubleStar, | ||
// probably not needed | ||
// ReplacerStash.MetacharacterSlashAfterLeadingSlash, | ||
ReplacerStash.MiddleDoubleStar, | ||
ReplacerStash.LeadingSlash, | ||
ReplacerStash.TrailingDoubleStar, | ||
ReplacerStash.OtherDoubleStar, | ||
ReplacerStash.MiddleSlash, | ||
ReplacerStash.TrailingSlash, | ||
ReplacerStash.NoSlash, | ||
ReplacerStash.NoTrailingSlash, | ||
ReplacerStash.Ending | ||
}; | ||
|
||
private readonly Regex? parsedRegex; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="IgnoreRule"/> class. | ||
/// Parses the given pattern as per .gitignore spec. | ||
/// https://git-scm.com/docs/gitignore#_pattern_format. | ||
/// </summary> | ||
/// <param name="pattern">Pattern to parse.</param> | ||
public IgnoreRule(string pattern) | ||
{ | ||
// A blank line matches no files, so it can serve as a separator for readability. | ||
if (string.IsNullOrEmpty(pattern.Trim())) | ||
{ | ||
return; | ||
} | ||
|
||
// A line starting with # serves as a comment. Put a backslash ("\") in front of the first hash for patterns that begin with a hash. | ||
if (pattern.StartsWith("#")) | ||
{ | ||
return; | ||
} | ||
|
||
// Account for escaped # and !, remove the leading backslash. | ||
// Also either a pattern will start with \ or with ! | ||
if (pattern.StartsWith("\\!") || pattern.StartsWith("\\#")) | ||
{ | ||
pattern = pattern.Substring(1); | ||
} | ||
else if (pattern.StartsWith("!")) | ||
{ | ||
Negate = true; | ||
pattern = pattern.Substring(1); | ||
} | ||
|
||
foreach (var replacer in Replacers) | ||
{ | ||
pattern = replacer.Invoke(pattern); | ||
} | ||
|
||
parsedRegex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
} | ||
|
||
public bool Negate { get; } | ||
|
||
public bool IsMatch(string input) | ||
{ | ||
return parsedRegex != null && parsedRegex.IsMatch(input); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
From https://github.com/goelhardik/ignore because it was not strong name signed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Ignore | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
/// <summary> | ||
/// A holder for a regex and replacer function. | ||
/// The function is invoked if the input matches the regex. | ||
/// </summary> | ||
public class Replacer | ||
{ | ||
private readonly string name; | ||
|
||
private readonly Regex regex; | ||
|
||
private readonly MatchEvaluator matchEvaluator; | ||
|
||
public Replacer(string name, Regex regex, Func<Match, string> replacer) | ||
{ | ||
this.name = name; | ||
this.regex = regex; | ||
matchEvaluator = new MatchEvaluator(replacer); | ||
} | ||
|
||
public string Invoke(string pattern) | ||
{ | ||
return regex.Replace(pattern, matchEvaluator); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return name; | ||
} | ||
} | ||
} |
Oops, something went wrong.