-
Notifications
You must be signed in to change notification settings - Fork 527
/
log.rb
192 lines (164 loc) · 3.95 KB
/
log.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module Git
# Return the last n commits that match the specified criteria
#
# @example The last (default number) of commits
# git = Git.open('.')
# Git::Log.new(git) #=> Enumerable of the last 30 commits
#
# @example The last n commits
# Git::Log.new(git).max_commits(50) #=> Enumerable of last 50 commits
#
# @example All commits returned by `git log`
# Git::Log.new(git).max_count(:all) #=> Enumerable of all commits
#
# @example All commits that match complex criteria
# Git::Log.new(git)
# .max_count(:all)
# .object('README.md')
# .since('10 years ago')
# .between('v1.0.7', 'HEAD')
#
# @api public
#
class Log
include Enumerable
# Create a new Git::Log object
#
# @example
# git = Git.open('.')
# Git::Log.new(git)
#
# @param base [Git::Base] the git repository object
# @param max_count [Integer, Symbol, nil] the number of commits to return, or
# `:all` or `nil` to return all
#
# Passing max_count to {#initialize} is equivalent to calling {#max_count} on the object.
#
def initialize(base, max_count = 30)
dirty_log
@base = base
max_count(max_count)
end
# The maximum number of commits to return
#
# @example All commits returned by `git log`
# git = Git.open('.')
# Git::Log.new(git).max_count(:all)
#
# @param num_or_all [Integer, Symbol, nil] the number of commits to return, or
# `:all` or `nil` to return all
#
# @return [self]
#
def max_count(num_or_all)
dirty_log
@max_count = (num_or_all == :all) ? nil : num_or_all
self
end
# Adds the --all flag to the git log command
#
# This asks for the logs of all refs (basically all commits reachable by HEAD,
# branches, and tags). This does not control the maximum number of commits
# returned. To control how many commits are returned, call {#max_count}.
#
# @example Return the last 50 commits reachable by all refs
# git = Git.open('.')
# Git::Log.new(git).max_count(50).all
#
# @return [self]
#
def all
dirty_log
@all = true
self
end
def object(objectish)
dirty_log
@object = objectish
return self
end
def author(regex)
dirty_log
@author = regex
return self
end
def grep(regex)
dirty_log
@grep = regex
return self
end
def path(path)
dirty_log
@path = path
return self
end
def skip(num)
dirty_log
@skip = num
return self
end
def since(date)
dirty_log
@since = date
return self
end
def until(date)
dirty_log
@until = date
return self
end
def between(sha1, sha2 = nil)
dirty_log
@between = [sha1, sha2]
return self
end
def cherry
dirty_log
@cherry = true
return self
end
def to_s
self.map { |c| c.to_s }.join("\n")
end
# forces git log to run
def size
check_log
@commits.size rescue nil
end
def each(&block)
check_log
@commits.each(&block)
end
def first
check_log
@commits.first rescue nil
end
def last
check_log
@commits.last rescue nil
end
def [](index)
check_log
@commits[index] rescue nil
end
private
def dirty_log
@dirty_flag = true
end
def check_log
if @dirty_flag
run_log
@dirty_flag = false
end
end
# actually run the 'git log' command
def run_log
log = @base.lib.full_log_commits(
count: @max_count, all: @all, object: @object, path_limiter: @path, since: @since,
author: @author, grep: @grep, skip: @skip, until: @until, between: @between,
cherry: @cherry
)
@commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
end
end
end