Commit e15ccf203d8c

Vincent Demeester <vincent@sbr.pm>
2020-05-09 14:05:23
Add .githooks and a Makefile target…
… to install them Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 9506b3a
.githooks/pre-commit
@@ -0,0 +1,4 @@
+#!/bin/sh
+unset NIX_BUILD_SHELL
+export NIX_BUILD_SHELL
+nix-shell -p gnumake --run 'make update-docs'
.githooks/pre-push
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+# Based on https://gist.github.com/Quintok/815396509ff79d886656b2855e1a8a46
+
+# A pre-push hook based on the git template. This will verify that no WIP or
+# autosquash commits are present. If such a commit is present, pushing will not
+# be possible.
+
+# This hook is called with the following parameters:
+#
+# $1 -- Name of the remote to which the push is being done
+# $2 -- URL to which the push is being done
+#
+# If pushing without using a named remote those arguments will be equal.
+#
+# Information about the commits which are being pushed is supplied as lines to
+# the standard input in the form:
+#
+#   <local ref> <local sha1> <remote ref> <remote sha1>
+
+z40=0000000000000000000000000000000000000000
+
+IFS=' '
+while read -r local_ref local_sha remote_ref remote_sha
+do
+  echo "$remote_ref" > /dev/null
+
+  if [ "$local_sha" = $z40 ]
+  then
+    # Ignore delete
+    :
+  else
+    if [ "$remote_sha" = $z40 ]
+    then
+      # New branch, examine all commits since master
+      range="$(git merge-base $local_sha master)..$local_sha"
+    else
+      # Update to existing branch, examine new commits
+      range="$remote_sha..$local_sha"
+    fi
+
+    # Check for WIP commits
+    commit=$(git rev-list -n 1 --grep '^WIP' "$range")
+    if [ "${local_ref}" = refs/heads/master ] && [ -n "$commit" ]
+    then
+      printf "\nPush rejected: WIP commit detected\n\n"
+      exit 1
+    fi
+
+    # Check for autosquash commits
+    commit=$(git rev-list -n 1 --grep '^\(fixup\|squash\)!' "$range")
+    if [ -n "$commit" ]
+    then
+      printf "\nPush rejected: autosquash commit detected\n\n"
+      exit 1
+    fi
+  fi
+done
+
+# Local Variables:
+# sh-basic-offset: 2
+# End:
.githooks/prepare-commit-msg
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+if [ -n "$2" ]; then
+  exit
+fi
+
+# Skip if a string like 'squash!' is included in the original message
+if grep --quiet -P "^\\w+!" "$1"; then
+  exit 0
+fi
+COMMIT=$(git symbolic-ref HEAD)
+
+# Generate a commit message from the diff of the commit
+IFS=$'\n'
+for s in $(git --no-pager diff --cached --name-status "$COMMIT"); do
+  FILENAME=$(echo "$s" | cut -f2)
+  STATUS=${s:0:1}
+  # Insert a line into the message depending on the status of the file
+  case ${STATUS} in
+  M)
+  SED_COMMAND="1i${FILENAME}: \n" ;;
+  A)
+  SED_COMMAND="1i${FILENAME}: Init\n" ;;
+  esac
+  sed -i "${SED_COMMAND}" "$1"
+done
+
+# Local Variables:
+# sh-basic-offset: 2
+# End:
Makefile
@@ -61,6 +61,9 @@ switch: assets setup
 		home-manager switch;\
 	fi
 
+install-hooks:
+	if [ -e .git ]; then nix-shell -p git --run 'git config core.hooksPath .githooks'; fi
+
 # Cleaning
 .PHONY: clean
 clean: