Skip to content

Commit e58cd29

Browse files
authored
Support the commit --no-gpg-sign flag (#589)
Support the commit --no-gpg-sign flag Signed-off-by: Bradley Buda <bradleybuda@gmail.com>
1 parent 323383b commit e58cd29

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ g.commit('message', gpg_sign: true)
244244
key_id = '0A46826A'
245245
g.commit('message', gpg_sign: key_id)
246246

247+
# Skip signing a commit (overriding any global gpgsign setting)
248+
g.commit('message', no_gpg_sign: true)
249+
247250
g = Git.clone(repo, 'myrepo')
248251
g.chdir do
249252
new_file('test-file', 'blahblahblah')

lib/git/lib.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ def remove(path = '.', opts = {})
647647
# :date
648648
# :no_verify
649649
# :allow_empty_message
650-
# :gpg_sign
650+
# :gpg_sign (accepts true or a gpg key ID as a String)
651+
# :no_gpg_sign (conflicts with :gpg_sign)
651652
#
652653
# @param [String] message the commit message to be used
653654
# @param [Hash] opts the commit options to be used
@@ -661,13 +662,18 @@ def commit(message, opts = {})
661662
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
662663
arr_opts << '--no-verify' if opts[:no_verify]
663664
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]
664-
if opts[:gpg_sign]
665+
666+
if opts[:gpg_sign] && opts[:no_gpg_sign]
667+
raise ArgumentError, 'cannot specify :gpg_sign and :no_gpg_sign'
668+
elsif opts[:gpg_sign]
665669
arr_opts <<
666670
if opts[:gpg_sign] == true
667671
'--gpg-sign'
668672
else
669673
"--gpg-sign=#{opts[:gpg_sign]}"
670674
end
675+
elsif opts[:no_gpg_sign]
676+
arr_opts << '--no-gpg-sign'
671677
end
672678

673679
command('commit', arr_opts)

tests/units/test_commit_with_gpg.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,29 @@ def test_with_specific_gpg_keyid
3434
assert_match(/commit.*--gpg-sign=keykeykey['"]/, actual_cmd)
3535
end
3636
end
37+
38+
def test_disabling_gpg_sign
39+
Dir.mktmpdir do |dir|
40+
git = Git.init(dir)
41+
actual_cmd = nil
42+
git.lib.define_singleton_method(:run_command) do |git_cmd, &block|
43+
actual_cmd = git_cmd
44+
`true`
45+
end
46+
message = 'My commit message'
47+
git.commit(message, no_gpg_sign: true)
48+
assert_match(/commit.*--no-gpg-sign['"]/, actual_cmd)
49+
end
50+
end
51+
52+
def test_conflicting_gpg_sign_options
53+
Dir.mktmpdir do |dir|
54+
git = Git.init(dir)
55+
message = 'My commit message'
56+
57+
assert_raises ArgumentError do
58+
git.commit(message, gpg_sign: true, no_gpg_sign: true)
59+
end
60+
end
61+
end
3762
end

0 commit comments

Comments
 (0)