-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathIgnoreEntry.cs
84 lines (68 loc) · 2.19 KB
/
IgnoreEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//-----------------------------------------------------------------------
// <copyright file="IgnoreEntry.cs" company="(none)">
// Copyright © 2013 John Gietzen and the WebGit .NET Authors. All rights reserved.
// </copyright>
// <author>John Gietzen</author>
//-----------------------------------------------------------------------
namespace WebGitNet
{
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
public class IgnoreEntry
{
private string pathGlobs;
private ReadOnlyCollection<Regex> pathRegexes;
public string CommitHash { get; set; }
public bool Negated { get; set; }
public string PathGlobs
{
get { return this.pathGlobs; }
set { this.pathGlobs = value; this.pathRegexes = null; }
}
public ReadOnlyCollection<Regex> PathRegexes
{
get
{
if (this.pathRegexes == null && this.pathGlobs != null)
{
this.pathRegexes = (from glob in this.pathGlobs.Split('/')
select PathUtilities.GlobToRegex(glob)).ToList().AsReadOnly();
}
return this.pathRegexes;
}
}
public bool Rooted { get; set; }
public bool IsMatch(string path)
{
var parts = path.Split('/');
var regexes = this.PathRegexes;
var maxPart = parts.Length - regexes.Count;
if (maxPart < 0)
{
return false;
}
if (this.Rooted)
{
maxPart = 0;
}
for (int part = 0; part <= maxPart; part++)
{
bool match = true;
for (int i = 0; i < regexes.Count; i++)
{
if (!regexes[i].IsMatch(parts[part + i]))
{
match = false;
break;
}
}
if (match)
{
return true;
}
}
return false;
}
}
}