diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-09-15 16:24:27 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-09-16 09:20:12 +0300 |
commit | 6d027f267769398654a92e9b63adc3fd271db914 (patch) | |
tree | ef51aa774b42ed308c1936b6b04374471b80df0f /ruby/lib/gitlab/git/hook.rb | |
parent | 838d4b88d3fa78345cdc1c2a34244de6e1d38f37 (diff) |
hooks: Extract function to call hooks accepting stdin
As there are multiple hooks in Git which accept both arguments and input
from stdin, it makes sense to refactor calling such hooks into its own
function. So this commit pulls out most of the implementation of the
receive hooks and makes it generic to make it reusable for other hooks.
Diffstat (limited to 'ruby/lib/gitlab/git/hook.rb')
-rw-r--r-- | ruby/lib/gitlab/git/hook.rb | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/ruby/lib/gitlab/git/hook.rb b/ruby/lib/gitlab/git/hook.rb index c391bbf78..dd3875d2f 100644 --- a/ruby/lib/gitlab/git/hook.rb +++ b/ruby/lib/gitlab/git/hook.rb @@ -43,30 +43,22 @@ module Gitlab private - def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref, push_options, transaction) - changes = [oldrev, newrev, ref].join(" ") - + def call_stdin_hook(args, input, env) exit_status = false exit_message = nil - vars = env_base_vars(gl_id, gl_username) - vars.merge!(push_options.env_data) if push_options - vars.merge!(transaction.env_vars) if transaction - options = { chdir: repo_path } - Open3.popen3(vars, path, options) do |stdin, stdout, stderr, wait_thr| + Open3.popen3(env, path, *args, options) do |stdin, stdout, stderr, wait_thr| exit_status = true stdin.sync = true - # in git, pre- and post- receive hooks may just exit without - # reading stdin. We catch the exception to avoid a broken pipe - # warning + # in git, hooks may just exit without reading stdin. We catch the + # exception to avoid a broken pipe warning begin - # inject all the changes as stdin to the hook - changes.lines do |line| + input.lines do |line| stdin.puts line end rescue Errno::EPIPE @@ -83,6 +75,16 @@ module Gitlab [exit_status, exit_message] end + def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref, push_options, transaction) + changes = [oldrev, newrev, ref].join(" ") + + vars = env_base_vars(gl_id, gl_username) + vars.merge!(push_options.env_data) if push_options + vars.merge!(transaction.env_vars) if transaction + + call_stdin_hook([], changes, vars) + end + def call_update_hook(gl_id, gl_username, oldrev, newrev, ref) options = { chdir: repo_path |