Skip to content

Commit

Permalink
Fix the gpg_sign commit option (#525)
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Jul 7, 2021
1 parent 07a1167 commit 45aeac9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -226,6 +226,14 @@ g.remove('file.txt', :cached => true) # git rm -f --cached -- "file.txt"
g.commit('message')
g.commit_all('message')

# Sign a commit using the gpg key configured in the user.signingkey config setting
g.config('user.signingkey', '0A46826A')
g.commit('message', gpg_sign: true)

# Sign a commit using a specified gpg key
key_id = '0A46826A'
g.commit('message', gpg_sign: key_id)

g = Git.clone(repo, 'myrepo')
g.chdir do
new_file('test-file', 'blahblahblah')
Expand Down
9 changes: 8 additions & 1 deletion lib/git/lib.rb
Expand Up @@ -660,7 +660,14 @@ def commit(message, opts = {})
arr_opts << "--date=#{opts[:date]}" if opts[:date].is_a? String
arr_opts << '--no-verify' if opts[:no_verify]
arr_opts << '--allow-empty-message' if opts[:allow_empty_message]
arr_opts << '--gpg-sign' if opts[:gpg_sign] == true || "--gpg-sign=#{opts[:gpg_sign]}" if opts[:gpg_sign]
if opts[:gpg_sign]
arr_opts <<
if opts[:gpg_sign] == true
'--gpg-sign'
else
"--gpg-sign=#{opts[:gpg_sign]}"
end
end

command('commit', arr_opts)
end
Expand Down
37 changes: 37 additions & 0 deletions tests/units/test_commit_with_gpg.rb
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'

class TestCommitWithGPG < Test::Unit::TestCase
def setup
set_file_paths
end

def test_with_configured_gpg_keyid
Dir.mktmpdir do |dir|
git = Git.init(dir)
actual_cmd = nil
git.lib.define_singleton_method(:run_command) do |git_cmd, &block|
actual_cmd = git_cmd
`true`
end
message = 'My commit message'
git.commit(message, gpg_sign: true)
assert_match(/commit.*--gpg-sign['"]/, actual_cmd)
end
end

def test_with_specific_gpg_keyid
Dir.mktmpdir do |dir|
git = Git.init(dir)
actual_cmd = nil
git.lib.define_singleton_method(:run_command) do |git_cmd, &block|
actual_cmd = git_cmd
`true`
end
message = 'My commit message'
git.commit(message, gpg_sign: 'keykeykey')
assert_match(/commit.*--gpg-sign=keykeykey['"]/, actual_cmd)
end
end
end

0 comments on commit 45aeac9

Please sign in to comment.