Skip to content

Commit 45aeac9

Browse files
authored
Fix the gpg_sign commit option (ruby-git#525)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 07a1167 commit 45aeac9

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ g.remove('file.txt', :cached => true) # git rm -f --cached -- "file.txt"
226226
g.commit('message')
227227
g.commit_all('message')
228228

229+
# Sign a commit using the gpg key configured in the user.signingkey config setting
230+
g.config('user.signingkey', '0A46826A')
231+
g.commit('message', gpg_sign: true)
232+
233+
# Sign a commit using a specified gpg key
234+
key_id = '0A46826A'
235+
g.commit('message', gpg_sign: key_id)
236+
229237
g = Git.clone(repo, 'myrepo')
230238
g.chdir do
231239
new_file('test-file', 'blahblahblah')

lib/git/lib.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,14 @@ def commit(message, opts = {})
660660
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
661661
arr_opts << '--no-verify' if opts[:no_verify]
662662
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]
663-
arr_opts << '--gpg-sign' if opts[:gpg_sign] == true || "--gpg-sign=#{opts[:gpg_sign]}" if opts[:gpg_sign]
663+
if opts[:gpg_sign]
664+
arr_opts <<
665+
if opts[:gpg_sign] == true
666+
'--gpg-sign'
667+
else
668+
"--gpg-sign=#{opts[:gpg_sign]}"
669+
end
670+
end
664671

665672
command('commit', arr_opts)
666673
end

tests/units/test_commit_with_gpg.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestCommitWithGPG < Test::Unit::TestCase
6+
def setup
7+
set_file_paths
8+
end
9+
10+
def test_with_configured_gpg_keyid
11+
Dir.mktmpdir do |dir|
12+
git = Git.init(dir)
13+
actual_cmd = nil
14+
git.lib.define_singleton_method(:run_command) do |git_cmd, &block|
15+
actual_cmd = git_cmd
16+
`true`
17+
end
18+
message = 'My commit message'
19+
git.commit(message, gpg_sign: true)
20+
assert_match(/commit.*--gpg-sign['"]/, actual_cmd)
21+
end
22+
end
23+
24+
def test_with_specific_gpg_keyid
25+
Dir.mktmpdir do |dir|
26+
git = Git.init(dir)
27+
actual_cmd = nil
28+
git.lib.define_singleton_method(:run_command) do |git_cmd, &block|
29+
actual_cmd = git_cmd
30+
`true`
31+
end
32+
message = 'My commit message'
33+
git.commit(message, gpg_sign: 'keykeykey')
34+
assert_match(/commit.*--gpg-sign=keykeykey['"]/, actual_cmd)
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)