summaryrefslogtreecommitdiff
path: root/etc/functions/git-hook
blob: 86c2bbb3a0aaf9f5ef9118de0ade5d9a28c6d0a6 (plain)
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## vim:ft=zsh
## Small function to help with git hooks
## Copyright (C) 2008 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>

autoload fdie
typeset hook_dir action=${1=list} hook=$2

if [[ -n $GIT_DIR && -d $GIT_DIR ]] {
	hook_dir=$GIT_DIR/hooks
} elif [[ -d .git/hooks ]] {
	hook_dir=$PWD/.git/hooks
} elif [[ -d hooks ]] {
	hook_dir=$PWD/hooks
} elif [[ $PWD == */hooks ]] {
	hook_dir=$PWD
} else {
	hook_dir=$(git rev-parse --git-dir)/hooks ||
	return
}

function usage {
	cat <<- ENDOFHELP
	Usage: git-hook enable|disable <hook>
	ENDOFHELP
}

function hook_enable {
	if [[ -e $hook_dir/$hook ]] {
		if ! [[ -x $hook_dir/$hook ]] {
			chmod +x $hook_dir/$hook
		}
	} else {
		fdie "No such hook: '$hook'"; return
	}
}

function hook_disable {
	if [[ -e $hook_dir/$hook ]] {
		if [[ -x $hook_dir/$hook ]] {
			chmod -x $hook_dir/$hook
		}
	} else {
		fdie "No such hook: '$hook'"; return
	}
}

function hook_list {
	for hook in $hook_dir/*; {
		[[ -x $hook ]] && echo -n $green
		echo -n ${hook:t}
		echo $reset
	}
}

case $action in
	enable) shift 2; hook_enable $* ;;
	disable) shift 2; hook_disable $* ;;
	list) shift; hook_list $* ;;
	*) usage ;;
esac