git-send-email: improve --validate error output

Improve the output we emit on --validate error to: * Say "FILE:LINE" instead of "FILE: LINE", to match "grep -n", compiler error messages etc. * Don't say "patch contains a" after just mentioning the filename, just leave it at "FILE:LINE: is longer than[...]. The "contains a" sounded like we were talking about the file in general, when we're actually checking it line-by-line. * Don't just say "rejected by sendemail-validate hook", but combine that with the system_or_msg() output to say what exit code the hook died with. I had an aborted attempt to make the line length checker note all lines that were longer than the limit. I didn't think that was worth the effort, but I've left in the testing change to check that we die as soon as we spot the first long line. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Apr 6, 2021 at 16:00 UTC ea7811b37e0e6fabb2a410475f198eab33110dcf
2 files changed +18 -11
git-send-email.perl
+6 -6
@@ -219,8 +219,8 @@ sub system_or_msg {
219 my $exit_code = $? >> 8;
220 return unless $signalled or $exit_code;
221
222 - return sprintf(__("failed to run command %s, died with code %d"),
223 - "@$args", $exit_code);
222 + return sprintf(__("fatal: command '%s' died with exit code %d"),
223 + $args->[0], $exit_code);
224 }
225
226 sub system_or_die {
@@ -1964,7 +1964,8 @@ sub validate_patch {
1964 }
1965 if ($hook_error) {
1966 die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" .
1967 - "warning: no patches were sent\n"), $fn);
1967 + "%s\n" .
1968 + "warning: no patches were sent\n"), $fn, $hook_error);
1969 }
1970 }
1971
@@ -1975,9 +1976,8 @@ sub validate_patch {
1976 or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
1977 while (my $line = <$fh>) {
1978 if (length($line) > 998) {
1978 - die sprintf(__("fatal: %s: %d: patch contains a line longer than 998 characters\n" .
1979 - "warning: no patches were sent\n"),
1980 - $fn, $.);
1979 + die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
1980 + "warning: no patches were sent\n"), $fn, $.);
1981 }
1982 }
1983 }
t/t9001-send-email.sh
+12 -5
@@ -415,7 +415,11 @@ test_expect_success $PREREQ 'reject long lines' '
415 z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
416 clean_fake_sendmail &&
417 cp $patches longline.patch &&
418 - echo $z512$z512 >>longline.patch &&
418 + cat >>longline.patch <<-EOF &&
419 + $z512$z512
420 + not a long line
421 + $z512$z512
422 + EOF
423 test_must_fail git send-email \
424 --from="Example <nobody@example.com>" \
425 --to=nobody@example.com \
@@ -424,7 +428,7 @@ test_expect_success $PREREQ 'reject long lines' '
428 $patches longline.patch \
429 2>actual &&
430 cat >expect <<-\EOF &&
427 - fatal: longline.patch: 35: patch contains a line longer than 998 characters
431 + fatal: longline.patch:35 is longer than 998 characters
432 warning: no patches were sent
433 EOF
434 test_cmp expect actual
@@ -533,15 +537,17 @@ test_expect_success $PREREQ "--validate respects relative core.hooksPath path" '
537 --validate \
538 longline.patch 2>actual &&
539 test_path_is_file my-hooks.ran &&
536 - cat >expect <<-\EOF &&
540 + cat >expect <<-EOF &&
541 fatal: longline.patch: rejected by sendemail-validate hook
542 + fatal: command '"'"'$(pwd)/my-hooks/sendemail-validate'"'"' died with exit code 1
543 warning: no patches were sent
544 EOF
545 test_cmp expect actual
546 '
547
548 test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
544 - test_config core.hooksPath "$(pwd)/my-hooks" &&
549 + hooks_path="$(pwd)/my-hooks" &&
550 + test_config core.hooksPath "$hooks_path" &&
551 test_when_finished "rm my-hooks.ran" &&
552 test_must_fail git send-email \
553 --from="Example <nobody@example.com>" \
@@ -550,8 +556,9 @@ test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
556 --validate \
557 longline.patch 2>actual &&
558 test_path_is_file my-hooks.ran &&
553 - cat >expect <<-\EOF &&
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
562 warning: no patches were sent
563 EOF
564 test_cmp expect actual