git
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 () {26file="$1"27# TODO: Replace with appropriate checks (e.g. spell checking).28true29}
30
31validate_patch () {32file="$1"33# Ensure that the patch applies without conflicts.34git am -3 "$file" || return35# TODO: Replace with appropriate checks for this patch36# (e.g. checkpatch.pl).37true38}
39
40validate_series () {41# TODO: Replace with appropriate checks for the whole series42# (e.g. quick build, coding style checks, etc.).43true44}
45
46# main -------------------------------------------------------------------------
47
48if test "$GIT_SENDEMAIL_FILE_COUNTER" = 149then
50remote=$(git config --default origin --get sendemail.validateRemote) &&51ref=$(git config --default HEAD --get sendemail.validateRemoteRef) &&52worktree=$(mktemp --tmpdir -d sendemail-validate.XXXXXXX) &&53git worktree add -fd --checkout "$worktree" "refs/remotes/$remote/$ref" &&54git config --replace-all sendemail.validateWorktree "$worktree"55else
56worktree=$(git config --get sendemail.validateWorktree)57fi || {58echo "sendemail-validate: error: failed to prepare worktree" >&259exit 160}
61
62unset GIT_DIR GIT_WORK_TREE63cd "$worktree" &&64
65if grep -q "^diff --git " "$1"66then
67validate_patch "$1"68else
69validate_cover_letter "$1"70fi &&71
72if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL"73then
74git config --unset-all sendemail.validateWorktree &&75trap 'git worktree remove -ff "$worktree"' EXIT &&76validate_series
77fi
78