diff --git a/README.md b/README.md index bc651c34..ab63d2fa 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 8dcb4f8a..57220f07 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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 diff --git a/tests/units/test_commit_with_gpg.rb b/tests/units/test_commit_with_gpg.rb new file mode 100644 index 00000000..97fb4de9 --- /dev/null +++ b/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