aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKate Ward <kate.ward@forestent.com>2020-04-10 15:17:35 +0200
committerKate Ward <kate.ward@forestent.com>2020-04-10 15:17:35 +0200
commit2523169c1a297331a400e548edec32fa891c1bed (patch)
tree3b6337f73d44cbfaeb4be41da066256726a105e2
parent77a6d9e2425071609c843751f83b2eaca5c9a06a (diff)
downloadshflags-2523169c1a297331a400e548edec32fa891c1bed.tar.gz
Fixes to hooks.
-rwxr-xr-x.githooks/generic28
-rwxr-xr-x.githooks/pre-commit13
-rwxr-xr-x.githooks/pre-commit.shellcheck2
-rwxr-xr-xinit_githooks.sh21
4 files changed, 40 insertions, 24 deletions
diff --git a/.githooks/generic b/.githooks/generic
new file mode 100755
index 0000000..b5f3e4c
--- /dev/null
+++ b/.githooks/generic
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# A generic git hook proxy.
+# https://git-scm.com/docs/githooks
+
+run() {
+ hook=$1
+ file=$2
+
+ n=$(echo "${file}" |sed "s/^.*${hook}\.//")
+ echo "running ${n} ${hook}"
+ ${file}
+}
+
+# Redirect output to stderr.
+exec 1>&2
+
+githooks='.githooks'
+basename=$(basename "$0")
+
+for f in $(cd ${githooks} && echo *); do
+ case "${f}" in
+ pre-commit.*)
+ # Called by "git commit" with no arguments.
+ [ "${basename}" = 'pre-commit' ] && run pre-commit "${githooks}/${f}"
+ ;;
+ esac
+done
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
deleted file mode 100755
index 5ee533a..0000000
--- a/.githooks/pre-commit
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-#
-# A hook script to verify what is about to be committed.
-# Called by "git commit" with no arguments.
-
-# Redirect output to stderr.
-exec 1>&2
-
-for f in .githooks/pre-commit.*; do
- n=$(echo "${f}" |sed 's/^.*pre-commit\.//')
- echo "running ${n} pre-commit"
- $(${f})
-done
diff --git a/.githooks/pre-commit.shellcheck b/.githooks/pre-commit.shellcheck
index d8f364e..034f7b9 100755
--- a/.githooks/pre-commit.shellcheck
+++ b/.githooks/pre-commit.shellcheck
@@ -12,7 +12,7 @@ if ! command -v shellcheck >/dev/null; then
return 0
fi
-for f in $(git diff --name-only); do
+for f in $(git diff --cached --name-only); do
case "${f}" in
shflags|shflags_test_helpers) shellcheck -s sh "${f}" ;;
*.sh) shellcheck "${f}" ;;
diff --git a/init_githooks.sh b/init_githooks.sh
index d5a7f50..19a380f 100755
--- a/init_githooks.sh
+++ b/init_githooks.sh
@@ -3,13 +3,18 @@
# Initialize the local git hooks this repository.
# https://git-scm.com/docs/githooks
-topLevel=$(git rev-parse --show-toplevel) && cd "${topLevel}"
+topLevel=$(git rev-parse --show-toplevel)
+if ! cd "${topLevel}"; then
+ echo "filed to cd into topLevel directory '${topLevel}'"
+ exit 1
+fi
+
hooksDir="${topLevel}/.githooks"
-hooksPath=$(git config core.hooksPath)
-if [ $? -ne 0 ]; then
- hooksPath="${topLevel}/.git/hooks"
+if ! hooksPath=$(git config core.hooksPath); then
+ hooksPath="${topLevel}/.git/hooks"
fi
+src="${hooksDir}/generic"
echo "linking hooks..."
for hook in \
applypatch-msg \
@@ -36,11 +41,7 @@ for hook in \
p4-pre-submit \
post-index-change
do
- src="${hooksDir}/${hook}"
+ echo "- ${hook}"
dest="${hooksPath}/${hook}"
-
- [ -x "${src}" ] || continue
-
- echo "- ${hook}"
- ln -sf "${src}" "${dest}"
+ ln -sf "${src}" "${dest}"
done