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) #=> Enumerable of the last 30 commits

The last n commits

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

All commits returned by git log

Git::Log.new(git).max_count(:all) #=> 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')

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.



39
40
41
42
43
# File 'lib/git/log.rb', line 39

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

Instance Method Details

#[](index)



161
162
163
164
# File 'lib/git/log.rb', line 161

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

#allself

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)


74
75
76
77
78
# File 'lib/git/log.rb', line 74

def all
  dirty_log
  @all = true
  self
end

#author(regex)



86
87
88
89
90
# File 'lib/git/log.rb', line 86

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

#between(sha1, sha2 = nil)



122
123
124
125
126
# File 'lib/git/log.rb', line 122

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

#cherry



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

def cherry
  dirty_log
  @cherry = true
  return self
end

#each(&block)



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

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

#first



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

def first
  check_log
  @commits.first rescue nil
end

#grep(regex)



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

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

#last



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

def last
  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)


56
57
58
59
60
# File 'lib/git/log.rb', line 56

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

#object(objectish)



80
81
82
83
84
# File 'lib/git/log.rb', line 80

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

#path(path)



98
99
100
101
102
# File 'lib/git/log.rb', line 98

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

#since(date)



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

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

#size

forces git log to run



141
142
143
144
# File 'lib/git/log.rb', line 141

def size
  check_log
  @commits.size rescue nil
end

#skip(num)



104
105
106
107
108
# File 'lib/git/log.rb', line 104

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

#to_s



134
135
136
# File 'lib/git/log.rb', line 134

def to_s
  self.map { |c| c.to_s }.join("\n")
end

#until(date)



116
117
118
119
120
# File 'lib/git/log.rb', line 116

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