I saw Ned Batchelder's post about a zsh function to show git hooks in the current repository, which was in turn inspired by a post by Frank Wiles, detailing a hack that nearly got him, reliant on sketchy git hooks in a shared folder.
I use fish shell, though, not zsh, so here's my version of the same script. I put this in ~/.config/fish/functions/_show_git_hooks.fish:
function _show_git_hooks --on-variable PWD
set -l git_dir (git rev-parse --git-dir 2>/dev/null)
if ! test -d "$git_dir/hooks"
# clear the shown hooks dir, since we have left a repo
set -e _git_shown_hooks_dir
return
end
set -l hooks_dir_rel "$git_dir/hooks"
set -l hooks_dir (realpath -- "$hooks_dir_rel")
# only announce once per repo, since this function is called on every prompt
if set -q _git_shown_hooks_dir
if test "$_git_shown_hooks_dir" = "$hooks_dir"
return
end
end
set -g _git_shown_hooks_dir "$hooks_dir"
set -l hooks
for hook in (find "$hooks_dir" -type f | sort)
if test -x "$hook"; and not string match -q '*.sample' -- "$hook"; and not string match -q '*.disabled' -- "$hook"; and not string match -q '*.txt' -- "$hook"
set -a hooks "$hook"
end
end
if test (count $hooks) -gt 0
echo "Git hooks found in $hooks_dir_rel:"
for hook in $hooks
echo " - "(basename $hook)
end
end
end
Then, much like in Ned's post, it's necessary to invoke the script on shell intialization, so I've added this to ~/.config/fish/conf.d/git-hooks.fish:
if status is-interactive
source ~/.config/fish/functions/_show_git_hooks.fish
end