Commit fb4c3892 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Implemented the add & remove commands

parent 269d777c
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -81,6 +81,17 @@ cmd_init ()
	done
}

_add_remove_check ()
{
	contains "$HOOKS" "$hook" || die "unknown hook '$hook'"
	[ -d "$GIT_DIR"/hooks/$hook.d ] || \
		die "the '$hook' hook directory does not appear to exist, you should \
		     run 'git-hooks init'"
	[ -w "$GIT_DIR"/hooks/$hook.d ] || \
		die "you do not have permissions to add the script to \
		     $GIT_DIR/hooks/$hook.d"
}

cmd_add ()
{
	summary="add a script to a hook"
@@ -88,6 +99,14 @@ cmd_add ()
	add_option hook yes
	add_option script yes
	parse_args "$@" || return
	_add_remove_check

	[ -e "$script" ] || die "unknown script '$script'"
	[ -e "$GIT_DIR"/hooks/$hook.d/"`basename "$script"`" ] && \
		die "the script '`basename "$script"`' (or one with the same name) \
		     already exists in the '$hook' hook directory"

	cp "$script" "$GIT_DIR"/hooks/$hook.d
}

cmd_remove ()
@@ -97,6 +116,12 @@ cmd_remove ()
	add_option hook yes
	add_option script yes
	parse_args "$@" || return
	_add_remove_check

	[ -e "$GIT_DIR"/hooks/$hook.d/"$script" ] || \
		die "there is no script '$script' in the '$hook' hook directory"

	rm "$GIT_DIR"/hooks/$hook.d/"$script"
}

cmd_help ()
+59 −0
Original line number Diff line number Diff line
@@ -115,3 +115,62 @@ die ()
	echo
	exit ${EXIT_CODE:-1}
} >&2


##
# contains()
# Check if a delimited string contains a value.
#
# Checks if a value exists within delimiters in a string.
#
# Usage:
# contains "<delimited string>" "<pattern>" [<delimiter default:[SPACE]>]
#
# Parameters:
# $1
#   A delimited list of values. eg. "foo,bar,baz" "foo bar baz"
# $2
#   The pattern to look for between delimiters; may contain * (match anything, 
#   any length) and ? (match anything, single character). The delimiter itself 
#   will not be matched.
# $3
#   The delimiter to use, defaults to a space (0x20)
#
# Examples:
# contains "foo bar" "foo"            # "foo" is one of the words in the string
# contains "foo1,foo2,bar" "foo*" "," # there is a value in the CSL that starts
#                                     # with "foo"
##
contains ()
{
	local list="$1" pattern="$2" delim="${3:- }"
	local value org

	while ! [ "$org" = "$list" ]; do
		value=`trim "${list%%$delim*}"`
		org="$list"
		list="${list#*$delim}"
		case "$value" in
			$pattern)
				return 0
				;;
		esac
	done
	return 1
}


##
# trim()
# Trim whitespace from around a string.
#
# Usage:
# trim "<string>" ["<string>" ["<string>" ... ]]
##
trim ()
{
	local string="$*"
	string="${string##*( )}"
	string="${string%%*( )}"
	echo "$string"
}