Skip to content

Add commit --allow-empty-message option and fix empty message parsing in process_commit_log_data #482

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
Sep 5, 2020
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
6 changes: 5 additions & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ def process_commit_log_data(data)
next
end

in_message = false if in_message && line[0..3] != " "

if in_message
hsh['message'] << "#{line[4..-1]}\n"
next
Expand Down Expand Up @@ -559,9 +561,10 @@ def remove(path = '.', opts = {})
# :author
# :date
# :no_verify
# :allow_empty_message
#
# @param [String] message the commit message to be used
# @param [Array] opts the commit options to be used
# @param [Hash] opts the commit options to be used
def commit(message, opts = {})
arr_opts = []
arr_opts << "--message=#{message}" if message
Expand All @@ -571,6 +574,7 @@ def commit(message, opts = {})
arr_opts << "--author=#{opts[:author]}" if opts[:author]
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
arr_opts << '--no-verify' if opts[:no_verify]
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]

command('commit', arr_opts)
end
Expand Down
25 changes: 25 additions & 0 deletions tests/units/test_commit_with_empty_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'

class TestCommitWithEmptyMessage < Test::Unit::TestCase
def setup
set_file_paths
end

def test_without_allow_empty_message_option
Dir.mktmpdir do |dir|
git = Git.init(dir)
assert_raises Git::GitExecuteError do
git.commit('', { allow_empty: true })
end
end
end

def test_with_allow_empty_message_option
Dir.mktmpdir do |dir|
git = Git.init(dir)
git.commit('', { allow_empty: true, allow_empty_message: true})
assert_equal(1, git.log.to_a.size)
end
end
end
14 changes: 13 additions & 1 deletion tests/units/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,17 @@ def test_log_file_noexist
@git.log.object('no-exist.txt').size
end
end


def test_log_with_empty_commit_message
Dir.mktmpdir do |dir|
git = Git.init(dir)
expected_message = 'message'
git.commit(expected_message, { allow_empty: true })
git.commit('', { allow_empty: true, allow_empty_message: true })
log = git.log
assert_equal(2, log.to_a.size)
assert_equal('', log[0].message)
assert_equal(expected_message, log[1].message)
end
end
end