-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcommits.rb
More file actions
97 lines (92 loc) · 2.48 KB
/
commits.rb
File metadata and controls
97 lines (92 loc) · 2.48 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'delegate'
module GitDiffParser
# The array of commit
class Commits < DelegateClass(Array)
# @return [Commits<Commit>]
def self.[](*ary)
new(ary)
end
# @param contents [String] `git diff` result
#
# @return [Commits<Commit>] parsed object
def self.parse(contents)
body = false
consume = false
author = ''
date = ''
chash = ''
mhash = ''
file = ''
files = []
comments = []
commit = []
lines = contents.lines
line_count = lines.count
parsed = new
lines.each_with_index do |line, count|
case line.chomp
when %r{^commit (?<hash>.*)}
if body == true
parsed << Commit.new(hash: chash, mhash: mhash, author: author, date: date, comment: comments, files: files)
comments.clear
files.clear
mhash.clear
body = false
end
chash = Regexp.last_match[:hash]
consume = true
when %r{^Author:\s+(?<author>.*)}
if consume
author = Regexp.last_match[:author]
body = true
end
when %r{^Date: (?<date>.*)}
if consume == true
date = Regexp.last_match[:date]
body = true
end
when %r{^Merge: (?<mhash>.*)}
if consume == true
mhash = Regexp.last_match[:mhash]
body = true
end
when %r{^[ACDMRTUXB]\s(?<file>.*)}
if consume == true
file = Regexp.last_match[:file]
files << file
body = true
if line_count == count + 1
parsed << Commit.new(hash: chash, mergeh: mhash, author: author, date: date, comment: comments, files: files)
end
end
else
if consume == true
comments << line
body = true
end
end
end
parsed
end
# @return [Commits<Commit>]
def initialize(*args)
super Array.new(*args)
end
# @return [Array<String>] file path
def hashes
map(&:chash)
end
# @param file [String] file path
#
# @return [Commit, nil]
def find_commit_by_file(file)
find { |commit| commit.file == file }
end
# @param secure_hash [String] target sha1 hash
#
# @return [Commit, nil]
def find_commit_by_secure_hash(secure_hash)
find { |commit| commit.secure_hash == secure_hash }
end
end
end