class Git::Log
Return the last n commits that match the specified criteria
@example The last (default number) of commits
git = Git.open('.') Git::Log.new(git).execute #=> Enumerable of the last 30 commits
@example The last n commits
Git::Log.new(git).max_commits(50).execute #=> Enumerable of last 50 commits
@example All commits returned by ‘git log`
Git::Log.new(git).max_count(:all).execute #=> 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') .execute
@api public
Public Class Methods
Source
# File lib/git/log.rb, line 91 def initialize(base, max_count = 30) dirty_log @base = base max_count(max_count) end
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.
Public Instance Methods
Source
# File lib/git/log.rb, line 243 def [](index) deprecate_method(__method__) check_log @commits[index] rescue nil end
Source
# File lib/git/log.rb, line 145 def all dirty_log @all = true 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]
Source
# File lib/git/log.rb, line 193 def between(sha1, sha2 = nil) dirty_log @between = [sha1, sha2] self end
Source
# File lib/git/log.rb, line 225 def each(&block) deprecate_method(__method__) check_log @commits.each(&block) end
Source
# File lib/git/log.rb, line 111 def execute run_log Result.new(@commits) end
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.
@example
query = g.log.since('2 weeks ago').author('Scott') results = query.execute puts "Found #{results.size} commits" results.each do |commit| # ... end
@return [Git::Log::Result] an object containing the log results
Source
# File lib/git/log.rb, line 231 def first deprecate_method(__method__) check_log @commits.first rescue nil end
Source
# File lib/git/log.rb, line 163 def grep(regex) dirty_log @grep = regex self end
Source
# File lib/git/log.rb, line 237 def last deprecate_method(__method__) check_log @commits.last rescue nil end
Source
# 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
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]
Source
# File lib/git/log.rb, line 151 def object(objectish) dirty_log @object = objectish self end
Source
# File lib/git/log.rb, line 181 def since(date) dirty_log @since = date self end
Source
# File lib/git/log.rb, line 219 def size deprecate_method(__method__) check_log @commits.size rescue nil end
forces git log to run
Source
# File lib/git/log.rb, line 211 def to_s deprecate_method(__method__) check_log @commits.map { |c| c.to_s }.join("\n") end
Source
# File lib/git/log.rb, line 187 def until(date) dirty_log @until = date self end
Private Instance Methods
Source
# File lib/git/log.rb, line 260 def check_log if @dirty_flag run_log @dirty_flag = false end end
Source
# File lib/git/log.rb, line 252 def deprecate_method(method_name) Git::Deprecation.warn("Calling Git::Log##{method_name} is deprecated and will be removed in a future version. Call #execute and then ##{method_name} on the result object.") end
Source
# File lib/git/log.rb, line 268 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, merges: @merges ) @commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) } end
actually run the ‘git log’ command