2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/githooker.rb', line 2
def self.hook(hook_name, file_contents)
git_dir = self.find_git_dir
acceptable_hooks = ["applypatch-msg","pre-applypatch","post-applypatch","pre-commit","prepare-commit-msg","commit-msg","post-commit","pre-rebase","post-checkout","post-merge","pre-push","pre-receive","update","post-receive","post-update","push-to-checkout","pre-auto-gc","post-rewrite","rebase"]
unless acceptable_hooks.include? hook_name
puts "Please provide an acceptable hook name. Acceptable hooks are as follows:"
acceptable_hooks.each {|hook| puts " - '" + hook + "'"}
puts "Refer to git docs for more information."
exit(1)
end
if git_dir
hooks_dir = File.join(git_dir, "hooks")
hook_full_path = File.join(hooks_dir, hook_name)
unless File.exist? hook_full_path
hook_file = File.open(hook_full_path, "w") do |file|
file_contents.each_line { |line| file.puts line }
end
system("chmod +x #{hook_full_path}")
puts "Added githook on hook #{hook_name}, located in hooks folder #{hooks_dir}"
else
puts "Githook for this action (#{hook_name}) already exists. This script is unable to manage duplicate executables for the same githook. Exiting..."
exit(1)
end
else
puts "No git repository found... exiting... "
exit(1)
end
end
|