Class: Git::Log

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/log.rb

Overview

Return the last n commits that match the specified criteria

Examples:

The last (default number) of commits

git = Git.open('.')
Git::Log.new(git).execute #=> Enumerable of the last 30 commits

The last n commits

Git::Log.new(git).max_commits(50).execute #=> Enumerable of last 50 commits

All commits returned by git log

Git::Log.new(git).max_count(:all).execute #=> Enumerable of all commits

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')
  .execute

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(base, max_count = 30) ⇒ Log

Create a new Git::Log object

Examples:

git = Git.open('.')
Git::Log.new(git)

Parameters:

  • base (Git::Base) —

    the git repository object

  • max_count (Integer, Symbol, nil) (defaults to: 30) —

    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.



91
92
93
94
95
# File 'lib/git/log.rb', line 91

def initialize(base, max_count = 30)
  dirty_log
  @base = base
  max_count(max_count)
end

Instance Method Details

#[](index)



243
244
245
246
247
# File 'lib/git/log.rb', line 243

def [](index)
  deprecate_method(__method__)
  check_log
  @commits[index] rescue nil
end

#all ⇒ self

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.

Examples:

Return the last 50 commits reachable by all refs

git = Git.open('.')
Git::Log.new(git).max_count(50).all

Returns:

  • (self)


145
146
147
148
149
# File 'lib/git/log.rb', line 145

def all
  dirty_log
  @all = true
  self
end

#author(regex)



157
158
159
160
161
# File 'lib/git/log.rb', line 157

def author(regex)
  dirty_log
  @author = regex
  self
end

#between(sha1, sha2 = nil)



193
194
195
196
197
# File 'lib/git/log.rb', line 193

def between(sha1, sha2 = nil)
  dirty_log
  @between = [sha1, sha2]
  self
end

#cherry



199
200
201
202
203
# File 'lib/git/log.rb', line 199

def cherry
  dirty_log
  @cherry = true
  self
end

#each(&block)



225
226
227
228
229
# File 'lib/git/log.rb', line 225

def each(&block)
  deprecate_method(__method__)
  check_log
  @commits.each(&block)
end

#execute ⇒ Git::Log::Result

Executes the git log command and returns an immutable result object.

This is the preferred way to get log data. It separates the query building from the execution, making the API more predictable.

Examples:

query = g.log.since('2 weeks ago').author('Scott')
results = query.execute
puts "Found #{results.size} commits"
results.each do |commit|
  # ...
end

Returns:



111
112
113
114
# File 'lib/git/log.rb', line 111

def execute
  run_log
  Result.new(@commits)
end

#first



231
232
233
234
235
# File 'lib/git/log.rb', line 231

def first
  deprecate_method(__method__)
  check_log
  @commits.first rescue nil
end

#grep(regex)



163
164
165
166
167
# File 'lib/git/log.rb', line 163

def grep(regex)
  dirty_log
  @grep = regex
  self
end

#last



237
238
239
240
241
# File 'lib/git/log.rb', line 237

def last
  deprecate_method(__method__)
  check_log
  @commits.last rescue nil
end

#max_count(num_or_all) ⇒ self

The maximum number of commits to return

Examples:

All commits returned by git log

git = Git.open('.')
Git::Log.new(git).max_count(:all)

Parameters:

  • num_or_all (Integer, Symbol, nil) —

    the number of commits to return, or :all or nil to return all

Returns:

  • (self)


127
128
129
130
131
# File 'lib/git/log.rb', line 127

def max_count(num_or_all)
  dirty_log
  @max_count = (num_or_all == :all) ? nil : num_or_all
  self
end

#merges



205
206
207
208
209
# File 'lib/git/log.rb', line 205

def merges
  dirty_log
  @merges = true
  self
end

#object(objectish)



151
152
153
154
155
# File 'lib/git/log.rb', line 151

def object(objectish)
  dirty_log
  @object = objectish
  self
end

#path(path)



169
170
171
172
173
# File 'lib/git/log.rb', line 169

def path(path)
  dirty_log
  @path = path
  self
end

#since(date)



181
182
183
184
185
# File 'lib/git/log.rb', line 181

def since(date)
  dirty_log
  @since = date
  self
end

#size

forces git log to run



219
220
221
222
223
# File 'lib/git/log.rb', line 219

def size
  deprecate_method(__method__)
  check_log
  @commits.size rescue nil
end

#skip(num)



175
176
177
178
179
# File 'lib/git/log.rb', line 175

def skip(num)
  dirty_log
  @skip = num
  self
end

#to_s



211
212
213
214
215
# File 'lib/git/log.rb', line 211

def to_s
  deprecate_method(__method__)
  check_log
  @commits.map { |c| c.to_s }.join("\n")
end

#until(date)



187
188
189
190
191
# File 'lib/git/log.rb', line 187

def until(date)
  dirty_log
  @until = date
  self
end