| 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 |
validate_cover_letter () { |
| 23 |
file="$1" |
| 24 |
true |
| 25 |
} |
| 26 |
|
| 27 |
validate_patch () { |
| 28 |
file="$1" |
| 29 |
# Ensure that the patch applies without conflicts. |
| 30 |
git am -3 "$file" || return |
| 31 |
# Sign the patch if patatt is available. |
| 32 |
case "$(git config --default false --get sendemail.runPatatt)" in |
| 33 |
TRUE|True|true|yes|YES|Yes|Y|y|on|ON|On|1) |
| 34 |
command -v patatt >/dev/null 2>&1 || return |
| 35 |
patatt sign --hook "$file" || return 1 |
| 36 |
;; |
| 37 |
esac |
| 38 |
} |
| 39 |
|
| 40 |
validate_series () { |
| 41 |
command -v gmake >/dev/null 2>&1 && make="gmake" || make="make" |
| 42 |
$make validate |
| 43 |
} |
| 44 |
|
| 45 |
# fallback for git 2.40 and older |
| 46 |
: ${GIT_SENDEMAIL_FILE_COUNTER:=1} |
| 47 |
: ${GIT_SENDEMAIL_FILE_TOTAL:=1} |
| 48 |
|
| 49 |
if test "$GIT_SENDEMAIL_FILE_COUNTER" = 1 |
| 50 |
then |
| 51 |
remote=$(git config --default origin --get sendemail.validateRemote) && |
| 52 |
ref=$(git config --default master --get sendemail.validateRemoteRef) && |
| 53 |
worktree=$(mktemp -d -t sendemail-validate.XXXXXXX) && |
| 54 |
git worktree add -fd --checkout "$worktree" "refs/remotes/$remote/$ref" && |
| 55 |
git config --replace-all sendemail.validateWorktree "$worktree" |
| 56 |
else |
| 57 |
worktree=$(git config --get sendemail.validateWorktree) |
| 58 |
fi || { |
| 59 |
echo "sendemail-validate: error: failed to prepare worktree" >&2 |
| 60 |
exit 1 |
| 61 |
} |
| 62 |
|
| 63 |
unset GIT_DIR GIT_WORK_TREE |
| 64 |
cd "$worktree" && |
| 65 |
|
| 66 |
if grep -q "^diff --git " "$1" |
| 67 |
then |
| 68 |
validate_patch "$1" |
| 69 |
else |
| 70 |
validate_cover_letter "$1" |
| 71 |
fi && |
| 72 |
|
| 73 |
if test "$GIT_SENDEMAIL_FILE_COUNTER" = "$GIT_SENDEMAIL_FILE_TOTAL" |
| 74 |
then |
| 75 |
git config --unset-all sendemail.validateWorktree && |
| 76 |
trap 'git worktree remove -ff "$worktree"' EXIT && |
| 77 |
validate_series |
| 78 |
fi |