Skip to content

Commit 93c8210

Browse files
committed
Add Git::Log#max_count
1 parent d84097b commit 93c8210

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,28 @@ directory, in the index and in the repository. Similar to running 'git status'
101101

102102
**Git::Remote**- A reference to a remote repository that is tracked by this repository.
103103

104-
**Git::Log** - An Enumerable object that references all the `Git::Object::Commit` objects that encompass your log query, which can be constructed through methods on the `Git::Log object`,
105-
like:
104+
**Git::Log** - An Enumerable object that references all the `Git::Object::Commit`
105+
objects that encompass your log query, which can be constructed through methods on
106+
the `Git::Log object`, like:
106107

107-
`@git.log(20).object("some_file").since("2 weeks ago").between('v2.6', 'v2.7').each { |commit| [block] }`
108+
```ruby
109+
git.log
110+
.max_count(:all)
111+
.object('README.md')
112+
.since('10 years ago')
113+
.between('v1.0.7', 'HEAD')
114+
.map { |commit| commit.sha }
115+
```
108116

109-
Pass the `--all` option to `git log` as follows:
117+
A maximum of 30 commits are returned if `max_count` is not called. To get all commits
118+
that match the log query, call `max_count(:all)`.
110119

111-
`@git.log.all.each { |commit| [block] }`
120+
Note that `git.log.all` adds the `--all` option to the underlying `git log` command.
121+
This asks for the logs of all refs (basically all commits reachable by HEAD,
122+
branches, and tags). This does not control the maximum number of commits returned. To
123+
control how many commits are returned, you should call `max_count`.
112124

113-
**Git::Worktrees** - Enumerable object that holds `Git::Worktree objects`.
125+
**Git::Worktrees** - Enumerable object that holds `Git::Worktree objects`.
114126

115127
## Errors Raised By This Gem
116128

lib/git/log.rb

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,76 @@
11
module Git
22

3-
# object that holds the last X commits on given branch
3+
# Return the last n commits that match the specified criteria
4+
#
5+
# @example The last (default number) of commits
6+
# git = Git.open('.')
7+
# Git::Log.new(git) #=> Enumerable of the last 30 commits
8+
#
9+
# @example The last n commits
10+
# Git::Log.new(git).max_commits(50) #=> Enumerable of last 50 commits
11+
#
12+
# @example All commits returned by `git log`
13+
# Git::Log.new(git).max_count(:all) #=> Enumerable of all commits
14+
#
15+
# @example All commits that match complex criteria
16+
# Git::Log.new(git)
17+
# .max_count(:all)
18+
# .object('README.md')
19+
# .since('10 years ago')
20+
# .between('v1.0.7', 'HEAD')
21+
#
22+
# @api public
23+
#
424
class Log
525
include Enumerable
626

7-
def initialize(base, count = 30)
27+
# Create a new Git::Log object
28+
#
29+
# @example
30+
# git = Git.open('.')
31+
# Git::Log.new(git)
32+
#
33+
# @param base [Git::Base] the git repository object
34+
# @param max_count [Integer, Symbol, nil] the number of commits to return, or
35+
# `:all` or `nil` to return all
36+
#
37+
# Passing max_count to {#initialize} is equivalent to calling {#max_count} on the object.
38+
#
39+
def initialize(base, max_count = 30)
840
dirty_log
941
@base = base
10-
@count = count
42+
max_count(max_count)
43+
end
44+
45+
# The maximum number of commits to return
46+
#
47+
# @example All commits returned by `git log`
48+
# git = Git.open('.')
49+
# Git::Log.new(git).max_count(:all)
50+
#
51+
# @param num_or_all [Integer, Symbol, nil] the number of commits to return, or
52+
# `:all` or `nil` to return all
53+
#
54+
# @return [self]
55+
#
56+
def max_count(num_or_all)
57+
dirty_log
58+
@max_count = (num_or_all == :all) ? nil : num_or_all
59+
self
1160
end
1261

62+
# Adds the --all flag to the git log command
63+
#
64+
# This asks for the logs of all refs (basically all commits reachable by HEAD,
65+
# branches, and tags). This does not control the maximum number of commits
66+
# returned. To control how many commits are returned, call {#max_count}.
67+
#
68+
# @example Return the last 50 commits reachable by all refs
69+
# git = Git.open('.')
70+
# Git::Log.new(git).max_count(50).all
71+
#
72+
# @return [self]
73+
#
1374
def all
1475
dirty_log
1576
@all = true
@@ -119,7 +180,7 @@ def check_log
119180
# actually run the 'git log' command
120181
def run_log
121182
log = @base.lib.full_log_commits(
122-
count: @count, all: @all, object: @object, path_limiter: @path, since: @since,
183+
count: @max_count, all: @all, object: @object, path_limiter: @path, since: @since,
123184
author: @author, grep: @grep, skip: @skip, until: @until, between: @between,
124185
cherry: @cherry
125186
)

tests/units/test_log.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ def setup
99
@git = Git.open(@wdir)
1010
end
1111

12+
def test_log_max_count_default
13+
assert_equal(30, @git.log.size)
14+
end
15+
16+
# In these tests, note that @git.log(n) is equivalent to @git.log.max_count(n)
17+
def test_log_max_count_20
18+
assert_equal(20, @git.log(20).size)
19+
assert_equal(20, @git.log.max_count(20).size)
20+
end
21+
22+
def test_log_max_count_nil
23+
assert_equal(72, @git.log(nil).size)
24+
assert_equal(72, @git.log.max_count(nil).size)
25+
end
26+
27+
def test_log_max_count_all
28+
assert_equal(72, @git.log(:all).size)
29+
assert_equal(72, @git.log.max_count(:all).size)
30+
end
31+
32+
# Note that @git.log.all does not control the number of commits returned. For that,
33+
# use @git.log.max_count(n)
1234
def test_log_all
1335
assert_equal(72, @git.log(100).size)
1436
assert_equal(76, @git.log(100).all.size)

0 commit comments

Comments
 (0)