send-email: use 'git hook run' for 'sendemail-validate'

Change the "sendmail-validate" hook to be run via the "git hook run" wrapper instead of via a direct invocation. This is the smallest possibly change to get "send-email" using "git hook run". We still check the hook itself with "-x", and set a "GIT_DIR" variable, both of which are asserted by our tests. We'll need to get rid of this special behavior if we start running N hooks, but for now let's be as close to bug-for-bug compatible as possible. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Dec 22, 2021 at 04:59 UTC a75553045467d81f504d55379c773222f227736b
2 files changed +16 -10
git-send-email.perl
+14 -8
@@ -225,13 +225,13 @@ my $multiedit;
225 my $editor;
226
227 sub system_or_msg {
228 - my ($args, $msg) = @_;
228 + my ($args, $msg, $cmd_name) = @_;
229 system(@$args);
230 my $signalled = $? & 127;
231 my $exit_code = $? >> 8;
232 return unless $signalled or $exit_code;
233
234 - my @sprintf_args = ($args->[0], $exit_code);
234 + my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code);
235 if (defined $msg) {
236 # Quiet the 'redundant' warning category, except we
237 # need to support down to Perl 5.8, so we can't do a
@@ -2075,10 +2075,10 @@ sub validate_patch {
2075 my ($fn, $xfer_encoding) = @_;
2076
2077 if ($repo) {
2078 + my $hook_name = 'sendemail-validate';
2079 my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
2080 require File::Spec;
2080 - my $validate_hook = File::Spec->catfile($hooks_path,
2081 - 'sendemail-validate');
2081 + my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
2082 my $hook_error;
2083 if (-x $validate_hook) {
2084 require Cwd;
@@ -2088,13 +2088,19 @@ sub validate_patch {
2088 chdir($repo->wc_path() or $repo->repo_path())
2089 or die("chdir: $!");
2090 local $ENV{"GIT_DIR"} = $repo->repo_path();
2091 - $hook_error = system_or_msg([$validate_hook, $target]);
2091 + my @cmd = ("git", "hook", "run", "--ignore-missing",
2092 + $hook_name, "--");
2093 + my @cmd_msg = (@cmd, "<patch>");
2094 + my @cmd_run = (@cmd, $target);
2095 + $hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg");
2096 chdir($cwd_save) or die("chdir: $!");
2097 }
2098 if ($hook_error) {
2095 - die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" .
2096 - "%s\n" .
2097 - "warning: no patches were sent\n"), $fn, $hook_error);
2099 + $hook_error = sprintf(__("fatal: %s: rejected by %s hook\n" .
2100 + $hook_error . "\n" .
2101 + "warning: no patches were sent\n"),
2102 + $fn, $hook_name);
2103 + die $hook_error;
2104 }
2105 }
2106
t/t9001-send-email.sh
+2 -2
@@ -539,7 +539,7 @@ test_expect_success $PREREQ "--validate respects relative core.hooksPath path" '
539 test_path_is_file my-hooks.ran &&
540 cat >expect <<-EOF &&
541 fatal: longline.patch: rejected by sendemail-validate hook
542 - fatal: command '"'"'my-hooks/sendemail-validate'"'"' died with exit code 1
542 + fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch>'"'"' died with exit code 1
543 warning: no patches were sent
544 EOF
545 test_cmp expect actual
@@ -558,7 +558,7 @@ test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
558 test_path_is_file my-hooks.ran &&
559 cat >expect <<-EOF &&
560 fatal: longline.patch: rejected by sendemail-validate hook
561 - fatal: command '"'"'$hooks_path/sendemail-validate'"'"' died with exit code 1
561 + fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch>'"'"' died with exit code 1
562 warning: no patches were sent
563 EOF
564 test_cmp expect actual