diff options
author | Robin Jarry <robin@jarry.cc> | 2023-07-16 16:06:09 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-08-04 23:36:37 +0200 |
commit | 046b6e34ee6b53e274f03b59c895df40494eb923 (patch) | |
tree | 2b202c7611d235fecf1b0b12b9946eb8de41050a /contrib/sendemail-validate | |
parent | dee1adce3f6ac9ba889fca89561231408e251aaa (diff) | |
download | aerc-046b6e34ee6b53e274f03b59c895df40494eb923.tar.gz |
contrib: update sendemail-validate hook
With git 2.41, git send-email exports a patch counter to the validate
hook. Copy the example hook from git and adapt it for aerc.
Link: https://github.com/git/git/commit/3c8d3adeae83
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'contrib/sendemail-validate')
-rwxr-xr-x | contrib/sendemail-validate | 87 |
1 files changed, 57 insertions, 30 deletions
diff --git a/contrib/sendemail-validate b/contrib/sendemail-validate index d94f6c3e..35b7d9e3 100755 --- a/contrib/sendemail-validate +++ b/contrib/sendemail-validate @@ -1,41 +1,68 @@ #!/bin/sh -set -e +# 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) -die() { - echo "error: $*" >&2 - exit 1 +validate_cover_letter () { + file="$1" + true } -run() { - echo "+ $*" >&2 - "$@" +validate_patch () { + file="$1" + # Ensure that the patch applies without conflicts. + git am -3 "$file" } -set -- -while read -r file; do - # skip empty patches (cover letter) - if grep -q "^diff --git " "$file"; then - set -- "$@" "$file" - fi -done -if [ $# -eq 0 ]; then - exit 0 -fi +validate_series () { + make all tests lint check-patches +} -echo 'Cloning upstream repo in temp dir ...' -tmp=$(mktemp -d) -trap "rm -rf -- $tmp" EXIT -run git clone -q --depth=1 "https://git.sr.ht/~rjarry/aerc" "$tmp" || - die "Failed to clone upstream repository. No network connection?" -export GIT_DIR="$tmp/.git" +# main ------------------------------------------------------------------------- -run cd $tmp +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" && -run git am -3 "$@" || - die "Failed to apply patch on upstream master branch. git pull --rebase?" +if grep -q "^diff --git " "$1" +then + validate_patch "$1" +else + validate_cover_letter "$1" +fi && -for target in all lint tests check-patches; do - run make $target || - die "Please fix the above issues and amend your patch(es)." -done +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 |