diff --git a/lib/git/lib.rb b/lib/git/lib.rb index ba9a4da1..92d603bf 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -550,6 +550,18 @@ def remove(path = '.', opts = {}) command('rm', arr_opts) end + # Takes the commit message with the options and executes the commit command + # + # accepts options: + # :amend + # :all + # :allow_empty + # :author + # :date + # :no_verify + # + # @param [String] message the commit message to be used + # @param [Array] opts the commit options to be used def commit(message, opts = {}) arr_opts = [] arr_opts << "--message=#{message}" if message @@ -558,6 +570,7 @@ def commit(message, opts = {}) arr_opts << '--allow-empty' if opts[:allow_empty] 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] command('commit', arr_opts) end diff --git a/tests/test_helper.rb b/tests/test_helper.rb index 565ee1fe..617d8c83 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -69,6 +69,10 @@ def update_file(path, content) def delete_file(path) File.delete(path) end + + def move_file(source_path, target_path) + File.rename source_path, target_path + end def new_file(name, contents) create_file(name,contents) diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 8796dc88..c007d168 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -44,6 +44,37 @@ def test_commit_with_date assert_equal("Scott Chacon #{author_date.strftime("%s %z")}", data['author']) end + def test_commit_with_no_verify + # Backup current pre-commit hook + pre_commit_path = "#{@wdir}/.git/hooks/pre-commit" + pre_commit_path_bak = "#{pre_commit_path}-bak" + move_file(pre_commit_path, pre_commit_path_bak) + + # Adds a pre-commit file that should throw an error + create_file(pre_commit_path, 'echo Pre-commit file. Shoud not execute; exit 1') # Error when executed + File.chmod(0111, pre_commit_path) + + create_file("#{@wdir}/test_file_2", 'content test_file_2') + @lib.add('test_file_2') + + # Error raised because of pre-commit hook and no use of no_verify option + assert_raise Git::GitExecuteError do + @lib.commit('commit without no verify and pre-commit file') + end + + # Error is not raised when no_verify is passed + assert_nothing_raised do + @lib.commit('commit with no verify and pre-commit file', no_verify: true ) + end + + # Restore pre-commit hook + move_file(pre_commit_path_bak, pre_commit_path) + + # Verify the commit was created + data = @lib.commit_data('HEAD') + assert_equal("commit with no verify and pre-commit file\n", data['message']) + end + def test_checkout assert(@lib.checkout('test_checkout_b',{:new_branch=>true})) assert(@lib.checkout('master'))