From 8743556784a13fd1242037c660ef2e912269413f Mon Sep 17 00:00:00 2001 From: Bradley Buda Date: Tue, 2 Aug 2022 16:29:39 -0700 Subject: [PATCH 1/3] Add gpg_sign: false to send --no-gpg-sign to git Signed-off-by: Bradley Buda --- README.md | 3 +++ lib/git/lib.rb | 6 ++++-- tests/units/test_commit_with_gpg.rb | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d4b68c55..d5d8623d 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,9 @@ g.commit('message', gpg_sign: true) key_id = '0A46826A' g.commit('message', gpg_sign: key_id) +# Skip signing a commit (overriding any global gpgsign setting) +g.commit('message', gpg_sign: false) + 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 cb408246..24e78c87 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -647,7 +647,7 @@ def remove(path = '.', opts = {}) # :date # :no_verify # :allow_empty_message - # :gpg_sign + # :gpg_sign (accepts true, false, or a gpg key ID as a String) # # @param [String] message the commit message to be used # @param [Hash] opts the commit options to be used @@ -661,10 +661,12 @@ 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] - if opts[:gpg_sign] + if opts.has_key?(:gpg_sign) arr_opts << if opts[:gpg_sign] == true '--gpg-sign' + elsif opts[:gpg_sign] == false + '--no-gpg-sign' else "--gpg-sign=#{opts[:gpg_sign]}" end diff --git a/tests/units/test_commit_with_gpg.rb b/tests/units/test_commit_with_gpg.rb index 97fb4de9..af4b5269 100644 --- a/tests/units/test_commit_with_gpg.rb +++ b/tests/units/test_commit_with_gpg.rb @@ -34,4 +34,18 @@ def test_with_specific_gpg_keyid assert_match(/commit.*--gpg-sign=keykeykey['"]/, actual_cmd) end end + + def test_disabling_gpg_sign + 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: false) + assert_match(/commit.*--no-gpg-sign['"]/, actual_cmd) + end + end end From b79028fe341fbe9342acf4e55343bde97951b915 Mon Sep 17 00:00:00 2001 From: Bradley Buda Date: Wed, 17 Aug 2022 17:50:29 -0700 Subject: [PATCH 2/3] Make :no_gpg_sign its own option Signed-off-by: Bradley Buda --- lib/git/lib.rb | 12 ++++++++---- tests/units/test_commit_with_gpg.rb | 13 ++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 24e78c87..fce8b274 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -647,7 +647,8 @@ def remove(path = '.', opts = {}) # :date # :no_verify # :allow_empty_message - # :gpg_sign (accepts true, false, or a gpg key ID as a String) + # :gpg_sign (accepts true or a gpg key ID as a String) + # :no_gpg_sign (conflicts with :gpg_sign) # # @param [String] message the commit message to be used # @param [Hash] opts the commit options to be used @@ -661,15 +662,18 @@ 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] - if opts.has_key?(:gpg_sign) + + if opts[:gpg_sign] && opts[:no_gpg_sign] + raise ArgumentError, 'cannot specify :gpg_sign and :no_gpg_sign' + elsif opts[:gpg_sign] arr_opts << if opts[:gpg_sign] == true '--gpg-sign' - elsif opts[:gpg_sign] == false - '--no-gpg-sign' else "--gpg-sign=#{opts[:gpg_sign]}" end + elsif opts[:no_gpg_sign] + arr_opts << '--no-gpg-sign' end command('commit', arr_opts) diff --git a/tests/units/test_commit_with_gpg.rb b/tests/units/test_commit_with_gpg.rb index af4b5269..5663def3 100644 --- a/tests/units/test_commit_with_gpg.rb +++ b/tests/units/test_commit_with_gpg.rb @@ -44,8 +44,19 @@ def test_disabling_gpg_sign `true` end message = 'My commit message' - git.commit(message, gpg_sign: false) + git.commit(message, no_gpg_sign: true) assert_match(/commit.*--no-gpg-sign['"]/, actual_cmd) end end + + def test_conflicting_gpg_sign_options + Dir.mktmpdir do |dir| + git = Git.init(dir) + message = 'My commit message' + + assert_raises ArgumentError do + git.commit(message, gpg_sign: true, no_gpg_sign: true) + end + end + end end From ee0baa4dd51cf13640cb3f3594945be641831197 Mon Sep 17 00:00:00 2001 From: Bradley Buda Date: Wed, 17 Aug 2022 17:55:35 -0700 Subject: [PATCH 3/3] Update README to match new argument name Signed-off-by: Bradley Buda --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d5d8623d..db38fbf6 100644 --- a/README.md +++ b/README.md @@ -245,7 +245,7 @@ key_id = '0A46826A' g.commit('message', gpg_sign: key_id) # Skip signing a commit (overriding any global gpgsign setting) -g.commit('message', gpg_sign: false) +g.commit('message', no_gpg_sign: true) g = Git.clone(repo, 'myrepo') g.chdir do