diff options
author | Elan Ruusamäe <glen@delfi.ee> | 2016-10-26 21:13:01 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2016-12-01 11:40:11 +0000 |
commit | dbd4bc264baf611b91c03d799f77376963633551 (patch) | |
tree | 28270e55df35d9f7201aaa7f8d9d9e59e7dec2d1 | |
parent | c5cf54392a8853c07352a54f94a754a61259124f (diff) | |
download | gitlab-shell-dbd4bc264baf611b91c03d799f77376963633551.tar.gz |
custom_hook: chain custom hooks
update hooks lookup to use <hook>.d/* from repository hooks dir
the order would be:
1. <repository>.git/custom_hooks/<hook_name> - per project hook
2. <repository>.git/custom_hooks/<hook_name>.d/* - per project hooks
3. <repository>.git/hooks/<hook_name>.d/* - global hooks
only executable files are matched and backup files excluded (*~)
and the resulting list is sorted per each lookup
-rw-r--r-- | lib/gitlab_custom_hook.rb | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/lib/gitlab_custom_hook.rb b/lib/gitlab_custom_hook.rb index f7ae6de..451d5b4 100644 --- a/lib/gitlab_custom_hook.rb +++ b/lib/gitlab_custom_hook.rb @@ -36,13 +36,6 @@ class GitlabCustomHook private - def find_hooks(hook_name) - [ - hook_file(hook_name, @repo_path), - hook_file(hook_name, ROOT_PATH) - ].compact - end - def call_receive_hook(hook, changes) # Prepare the hook subprocess. Attach a pipe to its stdin, and merge # both its stdout and stderr into our own stdout. @@ -64,9 +57,42 @@ class GitlabCustomHook $?.success? end - def hook_file(hook_type, repo_path) - hook_path = File.join(repo_path.strip, 'custom_hooks') - hook_file = "#{hook_path}/#{hook_type}" - hook_file if File.executable?(hook_file) + # lookup hook files in this order: + # + # 1. <repository>.git/custom_hooks/<hook_name> - per project hook + # 2. <repository>.git/custom_hooks/<hook_name>.d/* - per project hooks + # 3. <repository>.git/hooks/<hook_name>.d/* - global hooks + # + def find_hooks(hook_name) + hook_files = [] + + # <repository>.git/custom_hooks/<hook_name> + hook_file = File.join(@repo_path, 'custom_hooks', hook_name) + hook_files.push(hook_file) if File.executable?(hook_file) + + # <repository>.git/custom_hooks/<hook_name>.d/* + hook_path = File.join(@repo_path, 'custom_hooks', "#{hook_name}.d") + if Dir.exist?(hook_path) + hook_files += match_hook_files(hook_path) + end + + # <repository>.git/hooks/<hook_name>.d/* + hook_path = File.join(@repo_path, 'hooks', "#{hook_name}.d") + if Dir.exist?(hook_path) + hook_files += match_hook_files(hook_path) + end + + hook_files + end + + # match files from path: + # 1. file must be executable + # 2. file must not match backup file + # + # the resulting list is sorted + def match_hook_files(path) + Dir["#{path}/*"].select do |f| + !f[/~$/] && File.executable?(f) + end.sort end end |