send-email: support validate hook

Currently, send-email has support for rudimentary e-mail validation. Allow the user to add support for more validation by providing a sendemail-validate hook. Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed May 12, 2017 at 15:38 UTC 6489660b4bba7456fac0d0a41f5d6295c5900c5f
4 files changed +68 -1
Documentation/git-send-email.txt
+1
@@ -377,6 +377,7 @@ have been specified, in which case default to 'compose'.
377 Currently, validation means the following:
378 +
379 --
380 + * Invoke the sendemail-validate hook if present (see linkgit:githooks[5]).
381 * Warn of patches that contain lines longer than 998 characters; this
382 is due to SMTP limits as described by http://www.ietf.org/rfc/rfc2821.txt.
383 --
Documentation/githooks.txt
+8
@@ -447,6 +447,14 @@ rebase::
447 The commits are guaranteed to be listed in the order that they were
448 processed by rebase.
449
450 +sendemail-validate
451 +~~~~~~~~~~~~~~~~~~
452 +
453 +This hook is invoked by 'git send-email'. It takes a single parameter,
454 +the name of the file that holds the e-mail to be sent. Exiting with a
455 +non-zero status causes 'git send-email' to abort before sending any
456 +e-mails.
457 +
458
459 GIT
460 ---
git-send-email.perl
+19 -1
@@ -25,8 +25,9 @@ use Getopt::Long;
25 use Text::ParseWords;
26 use Term::ANSIColor;
27 use File::Temp qw/ tempdir tempfile /;
28 -use File::Spec::Functions qw(catfile);
28 +use File::Spec::Functions qw(catdir catfile);
29 use Error qw(:try);
30 +use Cwd qw(abs_path cwd);
31 use Git;
32 use Git::I18N;
33
@@ -1737,6 +1738,23 @@ sub unique_email_list {
1738
1739 sub validate_patch {
1740 my $fn = shift;
1741 +
1742 + my $validate_hook = catfile(catdir($repo->repo_path(), 'hooks'),
1743 + 'sendemail-validate');
1744 + my $hook_error;
1745 + if (-x $validate_hook) {
1746 + my $target = abs_path($fn);
1747 + # The hook needs a correct cwd and GIT_DIR.
1748 + my $cwd_save = cwd();
1749 + chdir($repo->wc_path() or $repo->repo_path())
1750 + or die("chdir: $!");
1751 + local $ENV{"GIT_DIR"} = $repo->repo_path();
1752 + $hook_error = "rejected by sendemail-validate hook"
1753 + if system($validate_hook, $target);
1754 + chdir($cwd_save) or die("chdir: $!");
1755 + }
1756 + return $hook_error if $hook_error;
1757 +
1758 open(my $fh, '<', $fn)
1759 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1760 while (my $line = <$fh>) {
t/t9001-send-email.sh
+40
@@ -1913,4 +1913,44 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
1913 test_cmp expected-list actual-list
1914 '
1915
1916 +test_expect_success $PREREQ 'invoke hook' '
1917 + mkdir -p .git/hooks &&
1918 +
1919 + write_script .git/hooks/sendemail-validate <<-\EOF &&
1920 + # test that we have the correct environment variable, pwd, and
1921 + # argument
1922 + case "$GIT_DIR" in
1923 + *.git)
1924 + true
1925 + ;;
1926 + *)
1927 + false
1928 + ;;
1929 + esac &&
1930 + test -f 0001-add-master.patch &&
1931 + grep "add master" "$1"
1932 + EOF
1933 +
1934 + mkdir subdir &&
1935 + (
1936 + # Test that it works even if we are not at the root of the
1937 + # working tree
1938 + cd subdir &&
1939 + git send-email \
1940 + --from="Example <nobody@example.com>" \
1941 + --to=nobody@example.com \
1942 + --smtp-server="$(pwd)/../fake.sendmail" \
1943 + ../0001-add-master.patch &&
1944 +
1945 + # Verify error message when a patch is rejected by the hook
1946 + sed -e "s/add master/x/" ../0001-add-master.patch >../another.patch &&
1947 + git send-email \
1948 + --from="Example <nobody@example.com>" \
1949 + --to=nobody@example.com \
1950 + --smtp-server="$(pwd)/../fake.sendmail" \
1951 + ../another.patch 2>err
1952 + test_i18ngrep "rejected by sendemail-validate hook" err
1953 + )
1954 +'
1955 +
1956 test_done