sendemail-validate.sample 2.3 KB

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