Skip to content

Commit 806e1af

Browse files
authored
Add Git::Log#all option (ruby-git#630)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent d905314 commit 806e1af

File tree

4 files changed

+52
-39
lines changed

4 files changed

+52
-39
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ like:
6666

6767
`@git.log(20).object("some_file").since("2 weeks ago").between('v2.6', 'v2.7').each { |commit| [block] }`
6868

69+
Pass the `--all` option to `git log` as follows:
70+
71+
`@git.log.all.each { |commit| [block] }`
72+
6973
**Git::Worktrees** - Enumerable object that holds `Git::Worktree objects`.
7074

7175
## Examples

lib/git/lib.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,12 @@ def diff_as_hash(diff_command, opts=[])
11821182
def log_common_options(opts)
11831183
arr_opts = []
11841184

1185-
arr_opts << "-#{opts[:count]}" if opts[:count]
1185+
if opts[:count] && !opts[:count].is_a?(Integer)
1186+
raise ArgumentError, "The log count option must be an Integer but was #{opts[:count].inspect}"
1187+
end
1188+
1189+
arr_opts << "--max-count=#{opts[:count]}" if opts[:count]
1190+
arr_opts << "--all" if opts[:all]
11861191
arr_opts << "--no-color"
11871192
arr_opts << "--cherry" if opts[:cherry]
11881193
arr_opts << "--since=#{opts[:since]}" if opts[:since].is_a? String

lib/git/log.rb

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
module Git
2-
2+
33
# object that holds the last X commits on given branch
44
class Log
55
include Enumerable
6-
6+
77
def initialize(base, count = 30)
88
dirty_log
99
@base = base
1010
@count = count
11-
12-
@commits = nil
13-
@author = nil
14-
@grep = nil
15-
@object = nil
16-
@path = nil
17-
@since = nil
18-
@skip = nil
19-
@until = nil
20-
@between = nil
21-
@cherry = nil
11+
end
12+
13+
def all
14+
dirty_log
15+
@all = true
16+
self
2217
end
2318

2419
def object(objectish)
@@ -32,37 +27,37 @@ def author(regex)
3227
@author = regex
3328
return self
3429
end
35-
30+
3631
def grep(regex)
3732
dirty_log
3833
@grep = regex
3934
return self
4035
end
41-
36+
4237
def path(path)
4338
dirty_log
4439
@path = path
4540
return self
4641
end
47-
42+
4843
def skip(num)
4944
dirty_log
5045
@skip = num
5146
return self
5247
end
53-
48+
5449
def since(date)
5550
dirty_log
5651
@since = date
5752
return self
5853
end
59-
54+
6055
def until(date)
6156
dirty_log
6257
@until = date
6358
return self
6459
end
65-
60+
6661
def between(sha1, sha2 = nil)
6762
dirty_log
6863
@between = [sha1, sha2]
@@ -74,24 +69,24 @@ def cherry
7469
@cherry = true
7570
return self
7671
end
77-
72+
7873
def to_s
7974
self.map { |c| c.to_s }.join("\n")
8075
end
81-
76+
8277

8378
# forces git log to run
84-
79+
8580
def size
8681
check_log
8782
@commits.size rescue nil
8883
end
89-
84+
9085
def each(&block)
9186
check_log
9287
@commits.each(&block)
9388
end
94-
89+
9590
def first
9691
check_log
9792
@commits.first rescue nil
@@ -107,29 +102,30 @@ def [](index)
107102
@commits[index] rescue nil
108103
end
109104

110-
111-
private
112-
105+
106+
private
107+
113108
def dirty_log
114109
@dirty_flag = true
115110
end
116-
111+
117112
def check_log
118113
if @dirty_flag
119114
run_log
120115
@dirty_flag = false
121116
end
122117
end
123-
118+
124119
# actually run the 'git log' command
125-
def run_log
126-
log = @base.lib.full_log_commits(:count => @count, :object => @object,
127-
:path_limiter => @path, :since => @since,
128-
:author => @author, :grep => @grep, :skip => @skip,
129-
:until => @until, :between => @between, :cherry => @cherry)
120+
def run_log
121+
log = @base.lib.full_log_commits(
122+
count: @count, all: @all, object: @object, path_limiter: @path, since: @since,
123+
author: @author, grep: @grep, skip: @skip, until: @until, between: @between,
124+
cherry: @cherry
125+
)
130126
@commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
131127
end
132-
128+
133129
end
134-
130+
135131
end

tests/units/test_log.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ def setup
99
@git = Git.open(@wdir)
1010
end
1111

12-
def test_get_fisrt_and_last_entries
12+
def test_log_all
13+
assert_equal(72, @git.log(100).size)
14+
assert_equal(76, @git.log(100).all.size)
15+
end
16+
17+
def test_log_non_integer_count
18+
assert_raises(ArgumentError) { @git.log('foo').size }
19+
end
20+
21+
def test_get_first_and_last_entries
1322
log = @git.log
1423
assert(log.first.is_a?(Git::Object::Commit))
1524
assert_equal('46abbf07e3c564c723c7c039a43ab3a39e5d02dd', log.first.objectish)
@@ -96,5 +105,4 @@ def test_log_cherry
96105
l = @git.log.between( 'master', 'cherry').cherry
97106
assert_equal( 1, l.size )
98107
end
99-
100108
end

0 commit comments

Comments
 (0)