Commit 585c54c5 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

enable & disable commands support links

enable & disable originally tried only to set or unset the executable
bit on a file, which is not a viable method with links to shared hook
scripts. They now change the target of links, prepending 'disabled:' to
disabled links.
parent 87df0a2b
Loading
Loading
Loading
Loading
+32 −7
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ HOOKS="update pre-receive post-receive"

usage ()
{
	local IFS
	for func in `declare -F | cut -f3 -d\ | grep ^cmd_`; do
		commands=( "${commands[@]}" ${func#cmd_} )
	done
@@ -66,6 +67,11 @@ mv_unique ()
	rm "$src"
}

path ()
{
	echo "$GIT_DIR/hooks/${1:+$1.d}${2+/$2}"
}

cmd_init ()
{
	summary="set up git-hook directories in a repository"
@@ -99,6 +105,7 @@ cmd_init ()

		[ x$ignore_def == xyes ] && continue

		unset IFS
		defaults=`$GIT_CONFIG --get git-hooks.$hook` || continue
		IFS=$',' defaults=( $defaults )
		for default in "${defaults[@]}"; do
@@ -129,9 +136,10 @@ _change_check ()

_installed_check ()
{
	[ -e "$GIT_DIR"/hooks/$hook.d/"$script" ] && \
		! [ -d "$GIT_DIR"/hooks/$hook.d/"$script" ] || \
	local path=`path $hook "$script"`
	if ! ( [ -e "$path" ] && ! [ -L "$path" ] ) && [ -d "$path" ]; then
		die "there is no script '$script' in the '$hook' hook directory"
	fi
}

cmd_add ()
@@ -177,6 +185,7 @@ cmd_enable ()
{
	summary="enable a script from a hook"
	local hook script
	local path link
	add_option hook yes
	add_option script yes
	parse_args "$@" || return
@@ -184,13 +193,22 @@ cmd_enable ()
	_change_check "$@"
	_installed_check "$@"

	path=`path $hook "$script"`
	if [ -L "$path" ]; then
		link=`readlink "$path"`
		link="${link#disabled:}"
		rm "$path"
		ln -s "$link" "$path"
	else
		chmod +x "$GIT_DIR"/hooks/$hook.d/"$script"
	fi
}

cmd_disable ()
{
	summary="disable a script from a hook"
	local hook script
	local path link
	add_option hook yes
	add_option script yes
	parse_args "$@" || return
@@ -198,7 +216,14 @@ cmd_disable ()
	_change_check "$@"
	_installed_check "$@"

	path=`path $hook "$script"`
	if [ -L "$path" ]; then
		link=`readlink "$path"`
		rm "$path"
		ln -s "disabled:$link" "$path"
	else
		chmod -x "$GIT_DIR"/hooks/$hook.d/"$script"
	fi
}

cmd_installed ()
@@ -209,8 +234,8 @@ cmd_installed ()
	parse_args "$@" || return
	_basic_check "$@"

	cd "$GIT_DIR"/hooks/$hook.d
	find -type f -printf '%P\n'
	cd "`path $hook`"
	find -type f -o -type l -printf '%P\n'
}

cmd_hooks ()