Skip to content

Commit 2279289

Browse files
committed
Merge branch 'ab/send-email-validate-errors'
Clean-up codepaths that implements "git send-email --validate" option and improves the message from it. * ab/send-email-validate-errors: git-send-email: improve --validate error output git-send-email: refactor duplicate $? checks into a function git-send-email: test full --validate output
2 parents 4c6ac2d + ea7811b commit 2279289

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

git-send-email.perl

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,31 @@ sub format_2822_time {
212212
my $multiedit;
213213
my $editor;
214214

215+
sub system_or_msg {
216+
my ($args, $msg) = @_;
217+
system(@$args);
218+
my $signalled = $? & 127;
219+
my $exit_code = $? >> 8;
220+
return unless $signalled or $exit_code;
221+
222+
return sprintf(__("fatal: command '%s' died with exit code %d"),
223+
$args->[0], $exit_code);
224+
}
225+
226+
sub system_or_die {
227+
my $msg = system_or_msg(@_);
228+
die $msg if $msg;
229+
}
230+
215231
sub do_edit {
216232
if (!defined($editor)) {
217233
$editor = Git::command_oneline('var', 'GIT_EDITOR');
218234
}
235+
my $die_msg = __("the editor exited uncleanly, aborting everything");
219236
if (defined($multiedit) && !$multiedit) {
220-
for (@_) {
221-
system('sh', '-c', $editor.' "$@"', $editor, $_);
222-
if (($? & 127) || ($? >> 8)) {
223-
die(__("the editor exited uncleanly, aborting everything"));
224-
}
225-
}
237+
system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
226238
} else {
227-
system('sh', '-c', $editor.' "$@"', $editor, @_);
228-
if (($? & 127) || ($? >> 8)) {
229-
die(__("the editor exited uncleanly, aborting everything"));
230-
}
239+
system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
231240
}
232241
}
233242

@@ -698,9 +707,7 @@ sub is_format_patch_arg {
698707
if ($validate) {
699708
foreach my $f (@files) {
700709
unless (-p $f) {
701-
my $error = validate_patch($f, $target_xfer_encoding);
702-
$error and die sprintf(__("fatal: %s: %s\nwarning: no patches were sent\n"),
703-
$f, $error);
710+
validate_patch($f, $target_xfer_encoding);
704711
}
705712
}
706713
}
@@ -1952,11 +1959,14 @@ sub validate_patch {
19521959
chdir($repo->wc_path() or $repo->repo_path())
19531960
or die("chdir: $!");
19541961
local $ENV{"GIT_DIR"} = $repo->repo_path();
1955-
$hook_error = "rejected by sendemail-validate hook"
1956-
if system($validate_hook, $target);
1962+
$hook_error = system_or_msg([$validate_hook, $target]);
19571963
chdir($cwd_save) or die("chdir: $!");
19581964
}
1959-
return $hook_error if $hook_error;
1965+
if ($hook_error) {
1966+
die sprintf(__("fatal: %s: rejected by sendemail-validate hook\n" .
1967+
"%s\n" .
1968+
"warning: no patches were sent\n"), $fn, $hook_error);
1969+
}
19601970
}
19611971

19621972
# Any long lines will be automatically fixed if we use a suitable transfer
@@ -1966,7 +1976,8 @@ sub validate_patch {
19661976
or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
19671977
while (my $line = <$fh>) {
19681978
if (length($line) > 998) {
1969-
return sprintf(__("%s: patch contains a line longer than 998 characters"), $.);
1979+
die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
1980+
"warning: no patches were sent\n"), $fn, $.);
19701981
}
19711982
}
19721983
}

t/t9001-send-email.sh

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,23 @@ test_expect_success $PREREQ 'reject long lines' '
415415
z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
416416
clean_fake_sendmail &&
417417
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
419423
test_must_fail git send-email \
420424
--from="Example <[email protected]>" \
421425
422426
--smtp-server="$(pwd)/fake.sendmail" \
423427
--transfer-encoding=8bit \
424428
$patches longline.patch \
425-
2>errors &&
426-
grep longline.patch errors
429+
2>actual &&
430+
cat >expect <<-\EOF &&
431+
fatal: longline.patch:35 is longer than 998 characters
432+
warning: no patches were sent
433+
EOF
434+
test_cmp expect actual
427435
'
428436

429437
test_expect_success $PREREQ 'no patch was sent' '
@@ -527,22 +535,33 @@ test_expect_success $PREREQ "--validate respects relative core.hooksPath path" '
527535
528536
--smtp-server="$(pwd)/fake.sendmail" \
529537
--validate \
530-
longline.patch 2>err &&
538+
longline.patch 2>actual &&
531539
test_path_is_file my-hooks.ran &&
532-
grep "rejected by sendemail-validate" err
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
533546
'
534547

535548
test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
536-
test_config core.hooksPath "$(pwd)/my-hooks" &&
549+
hooks_path="$(pwd)/my-hooks" &&
550+
test_config core.hooksPath "$hooks_path" &&
537551
test_when_finished "rm my-hooks.ran" &&
538552
test_must_fail git send-email \
539553
--from="Example <[email protected]>" \
540554
541555
--smtp-server="$(pwd)/fake.sendmail" \
542556
--validate \
543-
longline.patch 2>err &&
557+
longline.patch 2>actual &&
544558
test_path_is_file my-hooks.ran &&
545-
grep "rejected by sendemail-validate" err
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
546565
'
547566

548567
for enc in 7bit 8bit quoted-printable base64

0 commit comments

Comments
 (0)