1#!/bin/sh
2
3# An example hook script to validate a patch (and/or patch series) before
4# sending it via email.
5#
6# The hook should exit with non-zero status after issuing an appropriate
7# message if it wants to prevent the email(s) from being sent.
8#
9# To enable this hook, rename this file to "sendemail-validate".
10#
11# By default, it will only check that the patch(es) can be applied on top of
12# the default upstream branch without conflicts in a secondary worktree. After
13# validation (successful or not) of the last patch of a series, the worktree
14# will be deleted.
15#
16# The following config variables can be set to change the default remote and
17# remote ref that are used to apply the patches against:
18#
19#   sendemail.validateRemote (default: origin)
20#   sendemail.validateRemoteRef (default: HEAD)
21#
22# Replace the TODO placeholders with appropriate checks according to your
23# needs.
24
25validate_cover_letter () {
26	file="$1"
27	# TODO: Replace with appropriate checks (e.g. spell checking).
28	true
29}
30
31validate_patch () {
32	file="$1"
33	# Ensure that the patch applies without conflicts.
34	git am -3 "$file" || return
35	# TODO: Replace with appropriate checks for this patch
36	# (e.g. checkpatch.pl).
37	true
38}
39
40validate_series () {
41	# TODO: Replace with appropriate checks for the whole series
42	# (e.g. quick build, coding style checks, etc.).
43	true
44}
45
46# main -------------------------------------------------------------------------
47
48if test "$GIT_SENDEMAIL_FILE_COUNTER" = 1
49then
50	remote=$(git config --default origin --get sendemail.validateRemote) &&
51	ref=$(git config --default HEAD --get sendemail.validateRemoteRef) &&
52	worktree=$(mktemp --tmpdir -d sendemail-validate.XXXXXXX) &&
53	git worktree add -fd --checkout "$worktree" "refs/remotes/$remote/$ref" &&
54	git config --replace-all sendemail.validateWorktree "$worktree"
55else
56	worktree=$(git config --get sendemail.validateWorktree)
57fi || {
58	echo "sendemail-validate: error: failed to prepare worktree" >&2
59	exit 1
60}
61
62unset GIT_DIR GIT_WORK_TREE
63cd "$worktree" &&
64
65if grep -q "^diff --git " "$1"
66then
67	validate_patch "$1"
68else
69	validate_cover_letter "$1"
70fi &&
71
72if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL"
73then
74	git config --unset-all sendemail.validateWorktree &&
75	trap 'git worktree remove -ff "$worktree"' EXIT &&
76	validate_series
77fi
78