Skip to content

Support the git log --merges option #799

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
May 15, 2025
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
2 changes: 2 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def log_commits(opts = {})
# * 'tree' [String] the tree sha
# * 'author' [String] the author of the commit and timestamp of when the changes were created
# * 'committer' [String] the committer of the commit and timestamp of when the commit was applied
# * 'merges' [Boolean] if truthy, only include merge commits (aka commits with 2 or more parents)
#
# @raise [ArgumentError] if the revision range (specified with :between or :object) is a string starting with a hyphen
#
Expand All @@ -305,6 +306,7 @@ def full_log_commits(opts = {})

arr_opts << '--pretty=raw'
arr_opts << "--skip=#{opts[:skip]}" if opts[:skip]
arr_opts << '--merges' if opts[:merges]

arr_opts += log_path_options(opts)

Expand Down
9 changes: 7 additions & 2 deletions lib/git/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@ def cherry
return self
end

def merges
dirty_log
@merges = true
return self
end

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


# forces git log to run

def size
Expand Down Expand Up @@ -184,7 +189,7 @@ 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
cherry: @cherry, merges: @merges
)
@commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) }
end
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def append_file(name, contents)
#
# @return [void]
#
def assert_command_line_eq(expected_command_line, method: :command, mocked_output: nil, include_env: false)
def assert_command_line_eq(expected_command_line, method: :command, mocked_output: '', include_env: false)
actual_command_line = nil

command_output = ''
Expand Down
5 changes: 5 additions & 0 deletions tests/units/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ def test_log_cherry
l = @git.log.between( 'master', 'cherry').cherry
assert_equal( 1, l.size )
end

def test_log_merges
expected_command_line = ['log', '--max-count=30', '--no-color', '--pretty=raw', '--merges', {:chdir=>nil}]
assert_command_line_eq(expected_command_line) { |git| git.log.merges.size }
end
end