From: Matthieu Moy <Matthieu.Moy@grenoble-inp•fr>
To: Samuel GROOT <samuel.groot@grenoble-inp•org>
Cc: git@vger•kernel.org, e@80x24•org,
erwan.mathoniere@grenoble-inp•org,
jordan.de-gea@grenoble-inp•org, gitster@pobox•com,
aaron@schrab•com, Tom RUSSELLO <tom.russello@grenoble-inp•org>
Subject: Re: [WIP-PATCH 1/2] send-email: create email parser subroutine
Date: Sat, 28 May 2016 17:22:17 +0200 [thread overview]
Message-ID: <vpqeg8mi4wm.fsf@anie.imag.fr> (raw)
In-Reply-To: <20160527140104.11192-2-samuel.groot@grenoble-inp.org> (Samuel GROOT's message of "Fri, 27 May 2016 16:01:03 +0200")
Samuel GROOT <samuel.groot@grenoble-inp•org> writes:
> Parsing and processing in send-email is done in the same loop.
>
> To make the code more maintainable, we create two subroutines:
> - `parse_email` to separate header and body
> - `parse_header` to retrieve data from header
These routines are not specific to git send-email, nor to Git.
Does it make sense to use an external library, like
http://search.cpan.org/~rjbs/Email-Simple-2.210/lib/Email/Simple.pm ,
either by depending on it, or by copying it in Git's source tree ?
If not, I think it would be better to introduce an email parsing library
in a dedicated Perl module in perl/ in our source tree, to keep
git-send-email.perl more focused on the "send-email" logic.
> +sub parse_email {
> + my @header = ();
> + my @body = ();
> + my $fh = shift;
> +
> + # First unfold multiline header fields
> + while (<$fh>) {
> + last if /^\s*$/;
> + if (/^\s+\S/ and @header) {
> + chomp($header[$#header]);
> + s/^\s+/ /;
> + $header[$#header] .= $_;
> + } else {
> + push(@header, $_);
> + }
> + }
> +
> + # Now unfold the message body
Why "unfold"? Don't you mean "split message body into a list of lines"?
> + while (<$fh>) {
> + push @body, $_;
> + }
> +
> + return (@header, @body);
> +}
Please document your functions. See e.g. perl/Git.pm for an example of
what perldoc allows you to do.
This also lacks tests. One advantage of having a clean API is that it
also makes it simpler to do unit-testing. Grep "Test::More" in t/ to see
some existing unit-tests in Perl.
> + foreach(@_) {
Style: space before (.
> + if (defined $input_format && $input_format eq 'mbox') {
> + if (/^Subject:\s+(.*)$/i) {
> + $subject = $1;
> + } elsif (/^From:\s+(.*)$/i) {
> + $from = $1;
Not sure we need thes if/elsif/ for generic headers. Email::Simple's API
seems much simpler and general: $email->header("From");
> + foreach my $addr (parse_address_line($1)) {
> + push @to, $addr;
> + }
3 lines for an array concatenation in a high-level language. It looks
like 2 more than needed ;-).
> + }
> +
> + } else {
Useless blank line.
--
Matthieu Moy
http://www-verimag.imag.fr/~moy/
next prev parent reply other threads:[~2016-05-28 15:22 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-27 14:01 [WIP-PATCH 0/2] send-email: refactor the email parser loop Samuel GROOT
2016-05-27 14:01 ` [WIP-PATCH 1/2] send-email: create email parser subroutine Samuel GROOT
2016-05-28 15:22 ` Matthieu Moy [this message]
2016-05-28 23:33 ` Eric Wong
2016-05-29 17:15 ` Samuel GROOT
2016-05-29 17:53 ` Matthieu Moy
2016-05-30 13:28 ` Samuel GROOT
2016-06-02 16:57 ` Samuel GROOT
2016-06-02 19:58 ` Eric Wong
2016-05-27 14:01 ` [WIP-PATCH 2/2] send-email: use refactored subroutine to parse patches Samuel GROOT
2016-05-27 20:14 ` [WIP-PATCH 0/2] send-email: refactor the email parser loop Eric Wong
2016-05-28 15:04 ` Matthieu Moy
2016-05-29 17:21 ` Samuel GROOT
2016-05-29 18:05 ` Matthieu Moy
2016-05-30 14:01 ` Samuel GROOT
2016-05-30 14:20 ` Matthieu Moy
2016-05-30 18:28 ` Samuel GROOT
2016-05-30 19:29 ` Matthieu Moy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=vpqeg8mi4wm.fsf@anie.imag.fr \
--to=matthieu.moy@grenoble-inp$(echo .)fr \
--cc=aaron@schrab$(echo .)com \
--cc=e@80x24$(echo .)org \
--cc=erwan.mathoniere@grenoble-inp$(echo .)org \
--cc=git@vger$(echo .)kernel.org \
--cc=gitster@pobox$(echo .)com \
--cc=jordan.de-gea@grenoble-inp$(echo .)org \
--cc=samuel.groot@grenoble-inp$(echo .)org \
--cc=tom.russello@grenoble-inp$(echo .)org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox