Skip to content

Commit 07d4c76

Browse files
committed
Merge branch 'jt/send-email-validate-hook'
"git send-email" learned to run sendemail-validate hook to inspect and reject a message before sending it out. * jt/send-email-validate-hook: send-email: support validate hook
2 parents c05e123 + 6489660 commit 07d4c76

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

Documentation/git-send-email.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ have been specified, in which case default to 'compose'.
377377
Currently, validation means the following:
378378
+
379379
--
380+
* Invoke the sendemail-validate hook if present (see linkgit:githooks[5]).
380381
* Warn of patches that contain lines longer than 998 characters; this
381382
is due to SMTP limits as described by http://www.ietf.org/rfc/rfc2821.txt.
382383
--

Documentation/githooks.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ rebase::
447447
The commits are guaranteed to be listed in the order that they were
448448
processed by rebase.
449449

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+
450458

451459
GIT
452460
---

git-send-email.perl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
use Text::ParseWords;
2626
use Term::ANSIColor;
2727
use File::Temp qw/ tempdir tempfile /;
28-
use File::Spec::Functions qw(catfile);
28+
use File::Spec::Functions qw(catdir catfile);
2929
use Error qw(:try);
30+
use Cwd qw(abs_path cwd);
3031
use Git;
3132
use Git::I18N;
3233

@@ -1737,6 +1738,23 @@ sub unique_email_list {
17371738

17381739
sub validate_patch {
17391740
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+
17401758
open(my $fh, '<', $fn)
17411759
or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
17421760
while (my $line = <$fh>) {

t/t9001-send-email.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,4 +1913,44 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
19131913
test_cmp expected-list actual-list
19141914
'
19151915

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+
19161956
test_done

0 commit comments

Comments
 (0)