-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-stats-ignore.rb
59 lines (54 loc) · 1.74 KB
/
git-stats-ignore.rb
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
# Parse .gitstatsignore and test if filenames match entries.
#
# Example:
#
# gitstatsignore = GitStatsIgnore.new(filename: filename)
#
class GitStatsIgnore
# Public: Returns a list of patterns found in the .gitstatsignore file.
attr_reader :patterns
# Initialize a new GitStatsIgnore object.
#
# @param content [String] The content of a .gitstatsignore file.
#
# Example:
#
# gitstatsignore = GitStatsIgnore.new(content: content)
#
def initialize(content:)
@content = content
@patterns = []
if content
@patterns = content.split(/[\r\n]+/)
.grep(/^[^#]/) # Ignore comments.
.map! { |x|
x.gsub(/(^\s+|\s+$)/, '') # Trim any whitespace.
.gsub(/\\/, '/') # Normalize folder hierarchy separator on "/".
.gsub(/\*/, '[^\/]+') # Expand * into pattern matching a file or directory.
.sub(/^\//, '^/') # Expand leading / into a match at the root of the repo.
.sub(/^(?!\^)/, '^.*/') # Insert boundary at beginning of patterns.
.sub(/(?<!\/)$/, '(?:\/|$)') # Insert boundary at the end of patterns.
}
end
end
# Determine if a relative path to the repo matches a pattern in this
# .gitstatsignore file.
#
# @param filename [String] The filename of the file to check against patterns.
#
# Example:
#
# matches = gitstatsignore.matches_filename('test.txt')
#
def matches_filename(filename:)
filename = filename
.gsub(/\\/, '/') # Normalize folder hierarchy separator on "/".
.sub(/^(?!\/)/, '/') # Indicate root of repository.
for pattern in @patterns
if filename.match(pattern)
return true
end
end
return false
end
end