Skip to content

Commit 861eb71

Browse files
Add no verify for commit with documentation (#454)
* Add yard doc for changes in #commit Signed-off-by: Agora Security <github@agora-security.com> * Add unit testing for commit with option no-verify Signed-off-by: Agora Security <github@agora-security.com> * Add function to move files Signed-off-by: Agora Security <github@agora-security.com> * Add no verify for commit In git commit, add: -n, --no-verify This option bypasses the pre-commit and commit-msg hooks. See also githooks(5). Signed-off-by: Agora@Ubuntu-dev <filter@agora-security.com> Signed-off-by: Agora Security <github@agora-security.com> Co-authored-by: James Couball <jcouball@yahoo.com>
1 parent f3b439d commit 861eb71

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lib/git/lib.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ def remove(path = '.', opts = {})
550550
command('rm', arr_opts)
551551
end
552552

553+
# Takes the commit message with the options and executes the commit command
554+
#
555+
# accepts options:
556+
# :amend
557+
# :all
558+
# :allow_empty
559+
# :author
560+
# :date
561+
# :no_verify
562+
#
563+
# @param [String] message the commit message to be used
564+
# @param [Array] opts the commit options to be used
553565
def commit(message, opts = {})
554566
arr_opts = []
555567
arr_opts << "--message=#{message}" if message
@@ -558,6 +570,7 @@ def commit(message, opts = {})
558570
arr_opts << '--allow-empty' if opts[:allow_empty]
559571
arr_opts << "--author=#{opts[:author]}" if opts[:author]
560572
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
573+
arr_opts << '--no-verify' if opts[:no_verify]
561574

562575
command('commit', arr_opts)
563576
end

tests/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def update_file(path, content)
6969
def delete_file(path)
7070
File.delete(path)
7171
end
72+
73+
def move_file(source_path, target_path)
74+
File.rename source_path, target_path
75+
end
7276

7377
def new_file(name, contents)
7478
create_file(name,contents)

tests/units/test_lib.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ def test_commit_with_date
4444
assert_equal("Scott Chacon <schacon@gmail.com> #{author_date.strftime("%s %z")}", data['author'])
4545
end
4646

47+
def test_commit_with_no_verify
48+
# Backup current pre-commit hook
49+
pre_commit_path = "#{@wdir}/.git/hooks/pre-commit"
50+
pre_commit_path_bak = "#{pre_commit_path}-bak"
51+
move_file(pre_commit_path, pre_commit_path_bak)
52+
53+
# Adds a pre-commit file that should throw an error
54+
create_file(pre_commit_path, 'echo Pre-commit file. Shoud not execute; exit 1') # Error when executed
55+
File.chmod(0111, pre_commit_path)
56+
57+
create_file("#{@wdir}/test_file_2", 'content test_file_2')
58+
@lib.add('test_file_2')
59+
60+
# Error raised because of pre-commit hook and no use of no_verify option
61+
assert_raise Git::GitExecuteError do
62+
@lib.commit('commit without no verify and pre-commit file')
63+
end
64+
65+
# Error is not raised when no_verify is passed
66+
assert_nothing_raised do
67+
@lib.commit('commit with no verify and pre-commit file', no_verify: true )
68+
end
69+
70+
# Restore pre-commit hook
71+
move_file(pre_commit_path_bak, pre_commit_path)
72+
73+
# Verify the commit was created
74+
data = @lib.commit_data('HEAD')
75+
assert_equal("commit with no verify and pre-commit file\n", data['message'])
76+
end
77+
4778
def test_checkout
4879
assert(@lib.checkout('test_checkout_b',{:new_branch=>true}))
4980
assert(@lib.checkout('master'))

0 commit comments

Comments
 (0)