Skip to content

Add Git::Log#all option #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ like:

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

Pass the `--all` option to `git log` as follows:

`@git.log.all.each { |commit| [block] }`

**Git::Worktrees** - Enumerable object that holds `Git::Worktree objects`.

## Examples
Expand Down
7 changes: 6 additions & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,12 @@ def diff_as_hash(diff_command, opts=[])
def log_common_options(opts)
arr_opts = []

arr_opts << "-#{opts[:count]}" if opts[:count]
if opts[:count] && !opts[:count].is_a?(Integer)
raise ArgumentError, "The log count option must be an Integer but was #{opts[:count].inspect}"
end

arr_opts << "--max-count=#{opts[:count]}" if opts[:count]
arr_opts << "--all" if opts[:all]
arr_opts << "--no-color"
arr_opts << "--cherry" if opts[:cherry]
arr_opts << "--since=#{opts[:since]}" if opts[:since].is_a? String
Expand Down
68 changes: 32 additions & 36 deletions lib/git/log.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
module Git

# object that holds the last X commits on given branch
class Log
include Enumerable

def initialize(base, count = 30)
dirty_log
@base = base
@count = count

@commits = nil
@author = nil
@grep = nil
@object = nil
@path = nil
@since = nil
@skip = nil
@until = nil
@between = nil
@cherry = nil
end

def all
dirty_log
@all = true
self
end

def object(objectish)
Expand All @@ -32,37 +27,37 @@ def author(regex)
@author = regex
return self
end

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

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

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

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

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

def between(sha1, sha2 = nil)
dirty_log
@between = [sha1, sha2]
Expand All @@ -74,24 +69,24 @@ def cherry
@cherry = true
return self
end

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


# forces git log to run

def size
check_log
@commits.size rescue nil
end

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

def first
check_log
@commits.first rescue nil
Expand All @@ -107,29 +102,30 @@ def [](index)
@commits[index] rescue nil
end

private

private

def dirty_log
@dirty_flag = true
end

def check_log
if @dirty_flag
run_log
@dirty_flag = false
end
end

# actually run the 'git log' command
def run_log
log = @base.lib.full_log_commits(:count => @count, :object => @object,
:path_limiter => @path, :since => @since,
:author => @author, :grep => @grep, :skip => @skip,
:until => @until, :between => @between, :cherry => @cherry)
def run_log
log = @base.lib.full_log_commits(
count: @count, all: @all, object: @object, path_limiter: @path, since: @since,
author: @author, grep: @grep, skip: @skip, until: @until, between: @between,
cherry: @cherry
)
@commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
end

end

end
12 changes: 10 additions & 2 deletions tests/units/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ def setup
@git = Git.open(@wdir)
end

def test_get_fisrt_and_last_entries
def test_log_all
assert_equal(72, @git.log(100).size)
assert_equal(76, @git.log(100).all.size)
end

def test_log_non_integer_count
assert_raises(ArgumentError) { @git.log('foo').size }
end

def test_get_first_and_last_entries
log = @git.log
assert(log.first.is_a?(Git::Object::Commit))
assert_equal('46abbf07e3c564c723c7c039a43ab3a39e5d02dd', log.first.objectish)
Expand Down Expand Up @@ -96,5 +105,4 @@ def test_log_cherry
l = @git.log.between( 'master', 'cherry').cherry
assert_equal( 1, l.size )
end

end