summaryrefslogtreecommitdiff
path: root/etc/functions/git-hook
blob: 5243ee1627a8556ae529a7fd9820665abd3aadbf (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
## vim:ft=zsh
## Small function to help with git hooks
## Written 2008 by Daniel Friesel <derf@derf.homelinux.org>

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