about summary refs log tree commit diff stats
path: root/common/git_template/hooks/sendemail-validate.sample
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-09-30 16:36:43 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-09-30 16:36:43 +0200
commite0d14f7b20a6e859a988206b4e16981d6a67bf87 (patch)
treec029ef323d2c5cbbbe4a633ba0e1ad3d891ad9c1 /common/git_template/hooks/sendemail-validate.sample
parentfix(common/init): Be smarter, when suggesting an owner name (diff)
downloadflake-templates-e0d14f7b20a6e859a988206b4e16981d6a67bf87.tar.gz
flake-templates-e0d14f7b20a6e859a988206b4e16981d6a67bf87.zip
feat(common/init): Ship a full git_template
Diffstat (limited to '')
-rw-r--r--common/git_template/hooks/sendemail-validate.sample77
1 files changed, 77 insertions, 0 deletions
diff --git a/common/git_template/hooks/sendemail-validate.sample b/common/git_template/hooks/sendemail-validate.sample
new file mode 100644
index 0000000..52aa61c
--- /dev/null
+++ b/common/git_template/hooks/sendemail-validate.sample
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+
+# An example hook script to validate a patch (and/or patch series) before
+# sending it via email.
+#
+# The hook should exit with non-zero status after issuing an appropriate
+# message if it wants to prevent the email(s) from being sent.
+#
+# To enable this hook, rename this file to "sendemail-validate".
+#
+# By default, it will only check that the patch(es) can be applied on top of
+# the default upstream branch without conflicts in a secondary worktree. After
+# validation (successful or not) of the last patch of a series, the worktree
+# will be deleted.
+#
+# The following config variables can be set to change the default remote and
+# remote ref that are used to apply the patches against:
+#
+#   sendemail.validateRemote (default: origin)
+#   sendemail.validateRemoteRef (default: HEAD)
+#
+# Replace the TODO placeholders with appropriate checks according to your
+# needs.
+
+validate_cover_letter () {
+	file="$1"
+	# TODO: Replace with appropriate checks (e.g. spell checking).
+	true
+}
+
+validate_patch () {
+	file="$1"
+	# Ensure that the patch applies without conflicts.
+	git am -3 "$file" || return
+	# TODO: Replace with appropriate checks for this patch
+	# (e.g. checkpatch.pl).
+	true
+}
+
+validate_series () {
+	# TODO: Replace with appropriate checks for the whole series
+	# (e.g. quick build, coding style checks, etc.).
+	true
+}
+
+# main -------------------------------------------------------------------------
+
+if test "$GIT_SENDEMAIL_FILE_COUNTER" = 1
+then
+	remote=$(git config --default origin --get sendemail.validateRemote) &&
+	ref=$(git config --default HEAD --get sendemail.validateRemoteRef) &&
+	worktree=$(mktemp --tmpdir -d sendemail-validate.XXXXXXX) &&
+	git worktree add -fd --checkout "$worktree" "refs/remotes/$remote/$ref" &&
+	git config --replace-all sendemail.validateWorktree "$worktree"
+else
+	worktree=$(git config --get sendemail.validateWorktree)
+fi || {
+	echo "sendemail-validate: error: failed to prepare worktree" >&2
+	exit 1
+}
+
+unset GIT_DIR GIT_WORK_TREE
+cd "$worktree" &&
+
+if grep -q "^diff --git " "$1"
+then
+	validate_patch "$1"
+else
+	validate_cover_letter "$1"
+fi &&
+
+if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL"
+then
+	git config --unset-all sendemail.validateWorktree &&
+	trap 'git worktree remove -ff "$worktree"' EXIT &&
+	validate_series
+fi