Commit dbd4bc26 authored by Elan Ruusamäe's avatar Elan Ruusamäe Committed by Sean McGivern

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
parent c5cf5439
...@@ -36,13 +36,6 @@ class GitlabCustomHook ...@@ -36,13 +36,6 @@ class GitlabCustomHook
private 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) def call_receive_hook(hook, changes)
# Prepare the hook subprocess. Attach a pipe to its stdin, and merge # Prepare the hook subprocess. Attach a pipe to its stdin, and merge
# both its stdout and stderr into our own stdout. # both its stdout and stderr into our own stdout.
...@@ -64,9 +57,42 @@ class GitlabCustomHook ...@@ -64,9 +57,42 @@ class GitlabCustomHook
$?.success? $?.success?
end end
def hook_file(hook_type, repo_path) # lookup hook files in this order:
hook_path = File.join(repo_path.strip, 'custom_hooks') #
hook_file = "#{hook_path}/#{hook_type}" # 1. <repository>.git/custom_hooks/<hook_name> - per project hook
hook_file if File.executable?(hook_file) # 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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment