Skip to content

Commit 747bbff

Browse files
peffgitster
authored andcommitted
send-email: validate patches before sending anything
We try to catch errors early so that we don't end up sending half of a broken patch series. Right now the only validation is checking that line-lengths are under the SMTP-mandated limit of 998. The validation parsing is very crude (it just checks each line length without understanding the mailbox format) but should work fine for this simple check. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent aa54892 commit 747bbff

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

git-send-email.perl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ sub read_config {
332332
}
333333
}
334334

335+
foreach my $f (@files) {
336+
my $error = validate_patch($f);
337+
$error and die "fatal: $f: $error\nwarning: no patches were sent\n";
338+
}
339+
335340
if (@files) {
336341
unless ($quiet) {
337342
print $_,"\n" for (@files);
@@ -837,3 +842,15 @@ (@)
837842
}
838843
return @emails;
839844
}
845+
846+
sub validate_patch {
847+
my $fn = shift;
848+
open(my $fh, '<', $fn)
849+
or die "unable to open $fn: $!\n";
850+
while (my $line = <$fh>) {
851+
if (length($line) > 998) {
852+
return "$.: patch contains a line longer than 998 characters";
853+
}
854+
}
855+
return undef;
856+
}

t/t9001-send-email.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,24 @@ test_expect_success 'Show all headers' '
7878
diff -u expected-show-all-headers actual-show-all-headers
7979
'
8080

81+
z8=zzzzzzzz
82+
z64=$z8$z8$z8$z8$z8$z8$z8$z8
83+
z512=$z64$z64$z64$z64$z64$z64$z64$z64
84+
test_expect_success 'reject long lines' '
85+
rm -f commandline &&
86+
cp $patches longline.patch &&
87+
echo $z512$z512 >>longline.patch &&
88+
! git send-email \
89+
--from="Example <nobody@example.com>" \
90+
--to=nobody@example.com \
91+
--smtp-server="$(pwd)/fake.sendmail" \
92+
$patches longline.patch \
93+
2>errors &&
94+
grep longline.patch errors
95+
'
96+
97+
test_expect_success 'no patch was sent' '
98+
! test -e commandline
99+
'
100+
81101
test_done

0 commit comments

Comments
 (0)