Skip to content

Commit b79eac0

Browse files
committed
Fix detecting of commit message in Git::Lib::process_commit_log_data
If the commit doesn't contain a message, parsing the output of "git log" results in an error, which occures because the end of the commit message can't be detected. Signed-off-by: Pavel Kuznetsov <kpd200081@yandex.ru>
1 parent 4bef5ab commit b79eac0

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/git/lib.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ def process_commit_log_data(data)
243243
next
244244
end
245245

246+
in_message = false if in_message && line[0..3] != " "
247+
246248
if in_message
247249
hsh['message'] << "#{line[4..-1]}\n"
248250
next
@@ -559,9 +561,10 @@ def remove(path = '.', opts = {})
559561
# :author
560562
# :date
561563
# :no_verify
564+
# :allow_empty_message
562565
#
563566
# @param [String] message the commit message to be used
564-
# @param [Array] opts the commit options to be used
567+
# @param [Hash] opts the commit options to be used
565568
def commit(message, opts = {})
566569
arr_opts = []
567570
arr_opts << "--message=#{message}" if message
@@ -571,6 +574,7 @@ def commit(message, opts = {})
571574
arr_opts << "--author=#{opts[:author]}" if opts[:author]
572575
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
573576
arr_opts << '--no-verify' if opts[:no_verify]
577+
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]
574578

575579
command('commit', arr_opts)
576580
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env ruby
2+
require File.dirname(__FILE__) + '/../test_helper'
3+
4+
class TestCommitWithEmptyMessage < Test::Unit::TestCase
5+
def setup
6+
set_file_paths
7+
end
8+
9+
def test_without_allow_empty_message_option
10+
Dir.mktmpdir do |dir|
11+
git = Git.init(dir)
12+
assert_raises Git::GitExecuteError do
13+
git.commit('', { allow_empty: true })
14+
end
15+
end
16+
end
17+
18+
def test_with_allow_empty_message_option
19+
Dir.mktmpdir do |dir|
20+
git = Git.init(dir)
21+
git.commit('', { allow_empty: true, allow_empty_message: true})
22+
assert_equal(1, git.log.to_a.size)
23+
end
24+
end
25+
end

tests/units/test_log.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,17 @@ def test_log_file_noexist
7878
@git.log.object('no-exist.txt').size
7979
end
8080
end
81-
81+
82+
def test_log_with_empty_commit_message
83+
Dir.mktmpdir do |dir|
84+
git = Git.init(dir)
85+
expected_message = 'message'
86+
git.commit(expected_message, { allow_empty: true })
87+
git.commit('', { allow_empty: true, allow_empty_message: true })
88+
log = git.log
89+
assert_equal(2, log.to_a.size)
90+
assert_equal('', log[0].message)
91+
assert_equal(expected_message, log[1].message)
92+
end
93+
end
8294
end

0 commit comments

Comments
 (0)