support mboxrd format
cover support mboxrd format
Sometimes users will copy+paste an entire mbox into a commit message, leading to bad splits when a patch is output as an email.
Unlike other mbox-family formats, mboxrd allows reversible round-tripping while avoiding bad splits for old "mboxo" readers.
I'm also considering altering the current
"From ${COMMIT} Mon Sep 17 00:00:00 2001" line to something else so mailsplit (or "am") can autodetect.
Maybe:
From ${COMMIT}@mboxrd Mon Sep 17 00:00:00 2001?
We may also want to default to single escaping "From " in commit messages for --pretty=email to avoid corruption when somebody copy+pastes an mbox into the commit message. This is a technically incompatible change, but I think it's preferable to breaking splitting complete.
In other words, --pretty=email changes to output "mboxo" for now.
Long term (possibly git 3.0?), maybe mboxrd can become the default mail format. IMHO, it should've been since 2005.
ref: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/mail-mbox-formats.html
Eric Wong (3):
pretty: support "mboxrd" output format
mailsplit: support unescaping mboxrd messages
am: support --patch-format=mboxrd
Documentation/git-am.txt | 3 ++- Documentation/git-mailsplit.txt | 7 ++++++- builtin/am.c | 14 ++++++++++--- builtin/log.c | 2 +- builtin/mailsplit.c | 23 +++++++++++++++++++++ commit.h | 6 ++++++ log-tree.c | 4 ++-- pretty.c | 45 +++++++++++++++++++++++++++++++++-------- t/t4014-format-patch.sh | 27 +++++++++++++++++++++++++ t/t4150-am.sh | 20 ++++++++++++++++++ t/t5100-mailinfo.sh | 13 ++++++++++++ t/t5100/0001mboxrd | 4 ++++ t/t5100/0002mboxrd | 3 +++ t/t5100/sample.mboxrd | 17 ++++++++++++++++ 14 files changed, 172 insertions(+), 16 deletions(-)
PATCH 1/3 pretty: support "mboxrd" output format
This output format prevents format-patch output from breaking readers if somebody copy+pasted an mbox into a commit message.
Unlike the traditional "mboxo" format, "mboxrd" is designed to be fully-reversible. "mboxrd" also gracefully degrades to showing extra ">" in existing "mboxo" readers.
This degradation is preferable to breaking message splitting completely, a problem I've seen in "mboxcl" due to having multiple, non-existent, or inaccurate Content-Length headers.
"mboxcl2" is a non-starter since it's inherits the problems of "mboxcl" while being completely incompatible with existing tooling based around mailsplit.
ref: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/mail-mbox-formats.html
--- builtin/log.c | 2 +- commit.h | 6 ++++++ log-tree.c | 4 ++-- pretty.c | 45 +++++++++++++++++++++++++++++++++++++-------- t/t4014-format-patch.sh | 27 +++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 11 deletions(-)
builtin/log.c +1 -1
@@ -953,7 +953,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,struct pretty_print_context pp = {0 };struct commit *head = list[0 ]; -if (rev->commit_format !=CMIT_FMT_EMAIL ) +if (!cmit_fmt_is_mail (rev->commit_format))die (_ ("Cover letter needs email format" )); committer =git_committer_info (0 );
commit.h +6 -0
@@ -131,11 +131,17 @@ enum cmit_fmt {CMIT_FMT_FULLER ,CMIT_FMT_ONELINE ,CMIT_FMT_EMAIL , +CMIT_FMT_MBOXRD ,CMIT_FMT_USERFORMAT ,CMIT_FMT_UNSPECIFIED }; +static inline int cmit_fmt_is_mail (enum cmit_fmt fmt) +{ +return (fmt ==CMIT_FMT_EMAIL || fmt ==CMIT_FMT_MBOXRD ); +} + struct pretty_print_context {/* * Callers should tweak these to change the behavior of pp_* functions.
log-tree.c +2 -2
@@ -603,7 +603,7 @@ void show_log(struct rev_info *opt)* Print header line of header.. */ -if (opt->commit_format ==CMIT_FMT_EMAIL ) { +if (cmit_fmt_is_mail (opt->commit_format)) {log_write_email_headers (opt, commit, &ctx.subject, &extra_headers, &ctx.need_8bit_cte); }else if (opt->commit_format !=CMIT_FMT_USERFORMAT ) {
@@ -694,7 +694,7 @@ void show_log(struct rev_info *opt)if ((ctx.fmt !=CMIT_FMT_USERFORMAT ) && ctx.notes_message && *ctx.notes_message) { -if (ctx.fmt ==CMIT_FMT_EMAIL ) { +if (cmit_fmt_is_mail (ctx.fmt)) {strbuf_addstr (&msgbuf,"---\n" ); opt->shown_dashes =1 ; }
pretty.c +37 -8
118 lines not on this page · raw message
t/t4014-format-patch.sh +27 -0
@@ -1565,4 +1565,31 @@ test_expect_success 'format-patch --base overrides format.useAutoBase' ' test_cmp expected actual ' +test_expect_success 'format-patch --pretty=mboxrd' ' + cat >msg <<-INPUT_END && + mboxrd should escape the body + + From could trip up a loose mbox parser + >From extra escape for reversibility + >>From extra escape for reversibility 2 + from lower case not escaped + Fromm bad speling not escaped + From with leading space not escaped + INPUT_END + + cat >expect <<-INPUT_END && + >From could trip up a loose mbox parser + >>From extra escape for reversibility + >>>From extra escape for reversibility 2 + from lower case not escaped + Fromm bad speling not escaped + From with leading space not escaped + INPUT_END + + C=$(git commit-tree HEAD^^{tree} -p HEAD <msg) && + git format-patch --pretty=mboxrd --stdout -1 $C~1..$C >patch && + grep -A5 "^>From could trip up a loose mbox parser" patch >actual && + test_cmp expect actual +' + test_done
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 31 lines · Eric Wong
This output format prevents format-patch output from breaking readers if somebody copy+pasted an mbox into a commit message.
Unlike the traditional "mboxo" format, "mboxrd" is designed to be fully-reversible. "mboxrd" also gracefully degrades to showing extra ">" in existing "mboxo" readers.
[...]Signed-off-by Eric Wong <[email protected]>---
pretty.c +14
-0@@ -1697,12 +1699,34 @@ static void pp_handle_indent(struct pretty_print_context *pp, +static regex_t *mboxrd_prepare (void ) +{ +static regex_t preg; +const char re[] ="^>*From " ; +int err =regcomp (&preg, re,REG_NOSUB |REG_EXTENDED ); +[...] +return &preg; +} +void pp_remainder (struct pretty_print_context *pp,const char **msg_p,struct strbuf *sb,int indent) { +static regex_t *mboxrd_from; + +if (pp->fmt ==CMIT_FMT_MBOXRD && !mboxrd_from) + mboxrd_from =mboxrd_prepare (); +
pretty.c void pp_remainder(struct pretty_print_context *pp, · line 1755
Quoted 8 lines
pretty.c +4
-1@@ -1725,8 +1749,13 @@ void pp_remainder(struct pretty_print_context *pp,else if (pp->expand_tabs_in_log)strbuf_add_tabexpand (sb, pp->expand_tabs_in_log, line, linelen); -else +else { +if (pp->fmt ==CMIT_FMT_MBOXRD && + !regexec (mboxrd_from, line,0 ,0 ,0 )) +strbuf_addch (sb,'>' );
At first glance, this seems dangerous since it's handing 'line' to regexec() without paying attention to 'linelen'. For an arbitrary regex, this could result in erroneous matches on subsequent "lines", however, since this expression is anchored with '^', it's not a problem. But, it is a bit subtle.
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n)
{
size_t f = strlen("From ");
const char *t = s + n;
while (s < t && *s == '>')
s++;
return t - s >= f && !memcmp(s, "From ", f);
}
...
if (is_mboxrd_from(line, linelen)
strbuf_addch(sb, '>');or something.
t/t4014-format-patch.sh test_expect_success 'format-patch --base overrides format.useAutoBase' ' · line 1566
Quoted 10 lines
pretty.c +2
-0+strbuf_add (sb, line, linelen); + }strbuf_addch (sb,'\n' ); } }t/t4014-format-patch.sh +2
-0@@ -1565,4 +1565,31 @@ test_expect_success 'format-patch --base overrides format.useAutoBase' ' +test_expect_success 'format-patch --pretty=mboxrd' ' + cat >msg <<-INPUT_END &&
Maybe use <<-\INPUT_END to indicate that no variable interpolation is expected. Ditto below.
Quoted 12 lines
t/t4014-format-patch.sh +12
-0+ mboxrd should escape the body + + From could trip up a loose mbox parser + >From extra escape for reversibility + >>From extra escape for reversibility 2 + from lower case not escaped + Fromm bad speling not escaped + From with leading space not escaped + INPUT_END + + cat >expect <<-INPUT_END && + >From could trip up a loose mbox parser
t/t4014-format-patch.sh test_expect_success 'format-patch --base overrides format.useAutoBase' ' · line 1591
Quoted 10 lines
t/t4014-format-patch.sh +10
-0+ >>From extra escape for reversibility + >>>From extra escape for reversibility 2 + from lower case not escaped + Fromm bad speling not escaped + From with leading space not escaped + INPUT_END + + C=$(git commit-tree HEAD^^{tree} -p HEAD <msg) && + git format-patch --pretty=mboxrd --stdout -1 $C~1..$C >patch && + grep -A5 "^>From could trip up a loose mbox parser" patch >actual &&
Hmm, -A is not POSIX and is otherwise not used in Git tests. Perhaps you could use 'git grep --no-index -A' instead or something?
Quoted 4 lines
t/t4014-format-patch.sh +3
-0+ test_cmp expect actual +' + test_done
PATCH 2/3 mailsplit: support unescaping mboxrd messages
In reply to Eric WongThis will allow us to parse the output of --pretty=mboxrd and the output of other mboxrd generators.
--- Documentation/git-mailsplit.txt | 7 ++++++- builtin/mailsplit.c | 23 +++++++++++++++++++++++ t/t5100-mailinfo.sh | 13 +++++++++++++ t/t5100/0001mboxrd | 4 ++++ t/t5100/0002mboxrd | 3 +++ t/t5100/sample.mboxrd | 17 +++++++++++++++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 t/t5100/0001mboxrd create mode 100644 t/t5100/0002mboxrd create mode 100644 t/t5100/sample.mboxrd
Documentation/git-mailsplit.txt +6 -1
@@ -8,7 +8,8 @@ git-mailsplit - Simple UNIX mbox splitter program SYNOPSIS -------- [verse] -'git mailsplit' [-b] [-f<nn>] [-d<prec>] [--keep-cr] -o<directory> [--] [(<mbox>|<Maildir>)...] +'git mailsplit' [-b] [-f<nn>] [-d<prec>] [--keep-cr] [--mboxrd] + -o<directory> [--] [(<mbox>|<Maildir>)...] DESCRIPTION -----------
@@ -47,6 +48,10 @@ OPTIONS --keep-cr:: Do not remove `\r` from lines ending with `\r\n`. +--mboxrd:: + Input is of the "mboxrd" format and "^>+From " line escaping is + reversed. + GIT --- Part of the linkgit:git[1] suite
builtin/mailsplit.c +23 -0
@@ -45,6 +45,7 @@ static int is_from_line(const char *line, int len)static struct strbuf buf =STRBUF_INIT ;static int keep_cr; +static regex_t *gtfrom;/* Called with the first line (potentially partial) * already in buf[] -- normally that should begin with
@@ -77,6 +78,10 @@ static int split_one(FILE *mbox, const char *name, int allow_bare)strbuf_addch (&buf,'\n' ); } +if (gtfrom && buf.len > (sizeof (">From " ) -1 ) && + !regexec (gtfrom, buf.buf,0 ,0 ,0 )) +strbuf_remove (&buf,0 ,1 ); +if (fwrite (buf.buf,1 , buf.len, output) != buf.len)die_errno ("cannot write output" );
@@ -242,6 +247,22 @@ out:return ret; } +static regex_t *gtfrom_prepare (void ) +{ +static regex_t preg; +const char re[] ="^>+From " ; +int err =regcomp (&preg, re,REG_NOSUB |REG_EXTENDED ); + +if (err) { +char errbuf[1024 ]; +regerror (err, &preg, errbuf,sizeof (errbuf)); +regfree (&preg); +die ("Cannot prepare regexp `%s': %s" , re, errbuf); + } + +return &preg; +} +int cmd_mailsplit (int argc,const char **argv,const char *prefix) {int nr =0 , nr_prec =4 , num =0 ;
@@ -271,6 +292,8 @@ int cmd_mailsplit(int argc, const char **argv, const char *prefix) keep_cr =1 ; }else if ( arg[1 ] =='o' && arg[2 ] ) { dir = arg+2 ; + }else if (!strcmp (arg,"--mboxrd" )) { + gtfrom =gtfrom_prepare (); }else if ( arg[1 ] =='-' && !arg[2 ] ) { argp++;/* -- marks end of options */ break ;
t/t5100-mailinfo.sh +13 -0
@@ -111,4 +111,17 @@ test_expect_success 'mailinfo on message with quoted >From' ' test_cmp "$TEST_DIRECTORY"/t5100/quoted-from.expect quoted-from/msg ' +test_expect_success 'mailinfo unescapes with --mboxrd' ' + mkdir mboxrd && + git mailsplit -omboxrd --mboxrd \ + "$TEST_DIRECTORY"/t5100/sample.mboxrd >last && + test x"$(cat last)" = x2 && + for i in 0001 0002 + do + git mailinfo mboxrd/msg mboxrd/patch \ + <mboxrd/$i >mboxrd/out && + test_cmp "$TEST_DIRECTORY"/t5100/${i}mboxrd mboxrd/msg + done +' + test_done
t/t5100/0001mboxrd new file +4 -0
@@ -0,0 +1,4 @@ +From the beginning, mbox should have been mboxrd +>From escaped +From not mangled but this line should have been escaped +
t/t5100/0002mboxrd new file +3 -0
@@ -0,0 +1,3 @@ + >From unchanged + From also unchanged +
t/t5100/sample.mboxrd new file +17 -0
@@ -0,0 +1,17 @@ +From mboxrd Mon Sep 17 00:00:00 2001 +From: mboxrd writer <[email protected]> +Date: Fri, 9 Jun 2006 00:44:16 -0700 +Subject: [PATCH] a commit with escaped From lines + +>From the beginning, mbox should have been mboxrd +>>From escaped +From not mangled but this line should have been escaped + +From mboxrd Mon Sep 17 00:00:00 2001 +From: mboxrd writer <[email protected]> +Date: Fri, 9 Jun 2006 00:44:16 -0700 +Subject: [PATCH 2/2] another with fake From lines + + >From unchanged + From also unchanged +
PATCH 3/3 am: support --patch-format=mboxrd
In reply to Eric WongCombined with "git format-patch --pretty=mboxrd", this should allow us to round-trip commit messages with embedded mbox "From " lines without corruption.
--- Documentation/git-am.txt | 3 ++- builtin/am.c | 14 +++++++++++--- t/t4150-am.sh | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-)
Documentation/git-am.txt +2 -1
@@ -116,7 +116,8 @@ default. You can use `--no-utf8` to override this. By default the command will try to detect the patch format automatically. This option allows the user to bypass the automatic detection and specify the patch format that the patch(es) should be - interpreted as. Valid formats are mbox, stgit, stgit-series and hg. + interpreted as. Valid formats are mbox, mboxrd, + stgit, stgit-series and hg. -i:: --interactive::
builtin/am.c +11 -3
@@ -70,7 +70,8 @@ enum patch_format {PATCH_FORMAT_MBOX ,PATCH_FORMAT_STGIT ,PATCH_FORMAT_STGIT_SERIES , -PATCH_FORMAT_HG +PATCH_FORMAT_HG , +PATCH_FORMAT_MBOXRD }; enum keep_type {
@@ -712,7 +713,8 @@ done:* Splits out individual email patches from `paths`, where each path is either * a mbox file or a Maildir. Returns 0 on success, -1 on failure. */ -static int split_mail_mbox (struct am_state *state,const char **paths,int keep_cr) +static int split_mail_mbox (struct am_state *state,const char **paths, +int keep_cr,int mboxrd) {struct child_process cp =CHILD_PROCESS_INIT ;struct strbuf last =STRBUF_INIT ;
@@ -724,6 +726,8 @@ static int split_mail_mbox(struct am_state *state, const char **paths, int keep_argv_array_push (&cp.args,"-b" );if (keep_cr)argv_array_push (&cp.args,"--keep-cr" ); +if (mboxrd) +argv_array_push (&cp.args,"--mboxrd" );argv_array_push (&cp.args,"--" );argv_array_pushv (&cp.args, paths);
@@ -965,13 +969,15 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,switch (patch_format) {case PATCH_FORMAT_MBOX : -return split_mail_mbox (state, paths, keep_cr); +return split_mail_mbox (state, paths, keep_cr,0 );case PATCH_FORMAT_STGIT :return split_mail_conv (stgit_patch_to_mail, state, paths, keep_cr);case PATCH_FORMAT_STGIT_SERIES :return split_mail_stgit_series (state, paths, keep_cr);case PATCH_FORMAT_HG :return split_mail_conv (hg_patch_to_mail, state, paths, keep_cr); +case PATCH_FORMAT_MBOXRD : +return split_mail_mbox (state, paths, keep_cr,1 );default :die ("BUG: invalid patch_format" ); }
@@ -2201,6 +2207,8 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int *opt_value =PATCH_FORMAT_STGIT_SERIES ; else if (!strcmp(arg, "hg")) *opt_value =PATCH_FORMAT_HG ; + else if (!strcmp(arg, "mboxrd")) + *opt_value =PATCH_FORMAT_MBOXRD ; else return error(_("Invalid value for --patch-format: %s"), arg);return 0 ;
t/t4150-am.sh +20 -0
@@ -957,4 +957,24 @@ test_expect_success 'am -s unexpected trailer block' ' test_cmp expect actual ' +test_expect_success 'am --patch-format=mboxrd handles mboxrd' ' + rm -fr .git/rebase-apply && + git checkout -f first && + echo mboxrd >>file && + git add file && + cat >msg <<-INPUT_END && + mboxrd should escape the body + + From could trip up a loose mbox parser + >From extra escape for reversibility + INPUT_END + git commit -F msg && + git format-patch --pretty=mboxrd --stdout -1 >mboxrd1 && + grep "^>From could trip up a loose mbox parser" mboxrd1 && + git checkout -f first && + git am --patch-format=mboxrd mboxrd1 && + git cat-file commit HEAD | tail -n4 >out && + test_cmp msg out +' + test_done
Re: [PATCH 1/3] pretty: support "mboxrd" output format
In reply to Eric SunshineQuoted 29 lines · Eric Sunshine
Quoted 28 lines · Eric Wong
pretty.c +16
-1@@ -1697,12 +1699,34 @@ static void pp_handle_indent(struct pretty_print_context *pp, +static regex_t *mboxrd_prepare (void ) +{ +static regex_t preg; +const char re[] ="^>*From " ; +int err =regcomp (&preg, re,REG_NOSUB |REG_EXTENDED ); +[...] +return &preg; +} +void pp_remainder (struct pretty_print_context *pp,const char **msg_p,struct strbuf *sb,int indent) { +static regex_t *mboxrd_from; + +if (pp->fmt ==CMIT_FMT_MBOXRD && !mboxrd_from) + mboxrd_from =mboxrd_prepare (); +@@ -1725,8 +1749,13 @@ void pp_remainder(struct pretty_print_context *pp,else if (pp->expand_tabs_in_log)strbuf_add_tabexpand (sb, pp->expand_tabs_in_log, line, linelen); -else +else { + if (pp->fmt == CMIT_FMT_MBOXRD &&
Quoted 8 lines
pretty.c void pp_remainder(struct pretty_print_context *pp, · line 1755
Quoted 2 lines
pretty.c +2
-0+ !regexec(mboxrd_from, line, 0, 0, 0)) +strbuf_addch (sb,'>' );At first glance, this seems dangerous since it's handing 'line' to
regexec()without paying attention to 'linelen'. For an arbitrary regex, this could result in erroneous matches on subsequent "lines", however, since this expression is anchored with '^', it's not a problem. But, it is a bit subtle.
Maybe having more context of the pp_remainder function and seeing the get_one_line call would've helped in the diff; I didn't think of this issue once I figured out where to place the change.
On the other hand, not being too familiar with git C APIs, I was more worried strbuf was not NUL-terminated for regexec, but it seems to be.
Quoted 12 lines
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }
Quoted 1 line
or something.
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
I admit a regex isn't necessary, but I'm wondering if the above could be made less frightening to someone like me.
Or maybe I'm just easily frightened :x
Maybe extra test cases + valgrind can quell my fears :)
Quoted 7 lines
t/t4014-format-patch.sh test_expect_success 'format-patch --base overrides format.useAutoBase' ' · line 1566
Quoted 4 lines
t/t4014-format-patch.sh +2
-0@@ -1565,4 +1565,31 @@ test_expect_success 'format-patch --base overrides format.useAutoBase' ' +test_expect_success 'format-patch --pretty=mboxrd' ' + cat >msg <<-INPUT_END &&Maybe use <<-\INPUT_END to indicate that no variable interpolation is expected. Ditto below.
Noted, though I may add variable interpolation to test a line with trailing whitespace (just "From ") to ensure we don't overrun a buffer.
Quoted 2 lines
Hmm, -A is not POSIX and is otherwise not used in Git tests. Perhaps you could use 'git grep --no-index -A' instead or something?
Noted and will fix for v2. Will wait a day or two for further comments on this series.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 12 lines · Eric Wong
Quoted 10 lines · Eric Sunshine
pretty.c void pp_remainder(struct pretty_print_context *pp, · line 1755
Quoted 3 lines · Eric Wong
pretty.c +3
-0+if (pp->fmt ==CMIT_FMT_MBOXRD && + !regexec (mboxrd_from, line,0 ,0 ,0 )) +strbuf_addch (sb,'>' );At first glance, this seems dangerous since it's handing 'line' to
regexec()without paying attention to 'linelen'. For an arbitrary regex, this could result in erroneous matches on subsequent "lines", however, since this expression is anchored with '^', it's not a problem. But, it is a bit subtle.
Quoted 4 lines
Maybe having more context of the
pp_remainderfunction and seeing theget_one_linecall would've helped in the diff; I didn't think of this issue once I figured out where to place the change.
No, extra context wouldn't have helped. The problem is that get_one_line() merely returns the length of the line, which might be where the NUL-terminator is or it might be where the next newline is. Therefore, you can't rely upon the "current line" being NUL-terminated. So, in general, it's not "safe" to pass it to a function expecting the "line" to end at a NUL-terminator.
Quoted 3 lines
On the other hand, not being too familiar with git C APIs, I was more worried strbuf was not NUL-terminated for regexec, but it seems to be.
Yes, that's a guarantee, but it doesn't help in this case. Given line="foo\nbar", get_one_line(line) will return 4, the length of "foo\n", but regexec() won't know to stop looking until it hits the NUL after the 'r'. An arbitrary regex, such as /bar/ will match beyond what get_one_line() considers the end-of-line, which is why this code looks scary (wrong) at first glance.
Quoted 12 lines
Quoted 12 lines
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }
Quoted 4 lines
Quoted 1 line
or something.
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
The hand-coded is_mboxrd_from() above is pretty much idiomatic C and (I think) typical of how such a function would be coded in Git itself, so it looks normal and easy to grok to me (but, of course, I'm probably biased since I wrote it).
Quoted 2 lines
I admit a regex isn't necessary, but I'm wondering if the above could be made less frightening to someone like me.
Perhaps, but it's difficult to say without knowing how it frightens you.
Quoted 1 line
Maybe extra test cases + valgrind can quell my fears :)
I can envision tests such as:
""
"F"
"From"
"From "
"From "
"From foobar"and so on, if that's what you mean.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 2 lines · Eric Sunshine
Quoted 1 line · Eric Wong
Eric Sunshine <[email protected]> wrote:
<snip> Ah thanks, I can see your point-of-view, now. I always had the '^' in my mind since I've written the same thing in Perl and Ruby.
Quoted 22 lines
Quoted 12 lines
Quoted 12 lines
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }Quoted 4 lines
Quoted 1 line
or something.
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
The hand-coded
is_mboxrd_from()above is pretty much idiomatic C and (I think) typical of how such a function would be coded in Git itself, so it looks normal and easy to grok to me (but, of course, I'm probably biased since I wrote it).
Quoted 4 lines
Quoted 2 lines
I admit a regex isn't necessary, but I'm wondering if the above could be made less frightening to someone like me.
Perhaps, but it's difficult to say without knowing how it frightens you.
Pointer arithmetic leading to buffer overruns; but yeah, I don't do string parsing in C often, if ever.
Quoted 4 lines
Quoted 1 line
Maybe extra test cases + valgrind can quell my fears :)
I can envision tests such as:
Quoted 8 lines
"" "F" "From" "From " "From " "From foobar"and so on, if that's what you mean.
Yes, I also noticed trailing spaces are dropped anyways, so there's no perfect round-tripping going on.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 6 lines · Eric Wong
Quoted 2 lines · Eric Sunshine
Quoted 1 line · Eric Wong
Eric Sunshine <[email protected]> wrote:
<snip> Ah thanks, I can see your point-of-view, now. I always had the '^' in my mind since I've written the same thing in Perl and Ruby.
On reflection, even with the '^' anchor, it isn't safe the way it's coded since '^' will match following a newline, won't it? Therefore, because 'line' isn't necessarily NUL-terminated, the pattern could match on some line beyond what get_one_line() considers the "current line".
This makes the hand-coded is_mboxrd_from() even more attractive; plus you can re-use it in patch 2.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 5 lines · Eric Sunshine
On reflection, even with the '^' anchor, it isn't safe the way it's coded since '^' will match following a newline, won't it? Therefore, because 'line' isn't necessarily NUL-terminated, the pattern could match on some line beyond what
get_one_line()considers the "current line".
I guess that behavior only kicks in if you specify REG_NEWLINE, which you didn't.
Quoted 2 lines
This makes the hand-coded
is_mboxrd_from()even more attractive; plus you can re-use it in patch 2.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
In reply to Eric WongQuoted 17 lines · Eric Wong
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · Eric Wong
Quoted 13 lines · Eric Sunshine
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }or something.
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
The hand-coded
is_mboxrd_from()above is pretty much idiomatic C and (I think) typical of how such a function would be coded in Git itself, so it looks normal and easy to grok to me (but, of course, I'm probably biased since I wrote it).
For reference, here is the gfrom function from qmail (gfrom.c, source package netqmail-1.06 in Debian, reformatted git style)
int gfrom(char *s, int len)
{
while ((len > 0) && (*s == '>')) {
++s;
--len;
}
return (len >= 5) && !str_diffn(s, "From ", 5);
}Similar to yours, but a several small things improves readability for me:
* the avoidance of subtraction from the "return" conditional
* s/n/len/ variable name
* extra parentheses
* removal of "t" variable (t for "terminal/termination"?)
str_diffn is memcmp-like, I assume. My eyes glazed over when I saw that function implemented in str_diffn.c, too.
Just thinking out loud, with sufficient tests I could go with either. Will reroll when/if I get the chance tomorrow.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
[cc:+junio]
Quoted 30 lines · Eric Wong
Quoted 17 lines · Eric Wong
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · Eric Wong
Quoted 13 lines · Eric Sunshine
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }or something.
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
The hand-coded
is_mboxrd_from()above is pretty much idiomatic C and (I think) typical of how such a function would be coded in Git itself, so it looks normal and easy to grok to me (but, of course, I'm probably biased since I wrote it).For reference, here is the gfrom function from qmail (
gfrom.c, source package netqmail-1.06 in Debian, reformatted git style)int
gfrom(char *s, int len)
Quoted 8 lines
{
while ((len > 0) && (*s == '>')) {
++s;
--len;
}
return (len >= 5) && !str_diffn(s, "From ", 5);
}Seems less idiomatic and less like what we might see elsewhere in the Git codebase, but that's subjective. Functionally, it appears correct.
Quoted 5 lines
Similar to yours, but a several small things improves readability for me:
* the avoidance of subtraction from the "return" conditional
* s/n/len/ variable name
Idiomatic C code favors concise names such as 'i', 'j', or 'n', for instance, but I don't care strongly.
Quoted 1 line
* extra parentheses
Unnecessary syntactic noise (consuming reviewer brain cycles).
Quoted 1 line
* removal of "t" variable (t for "terminal/termination"?)
Heh, no, just the next letter after 's'. Again, just an idiom, as 'i', 'j', 'k' are often used for integers, 's' and 't' are common for strings.
Quoted 5 lines
str_diffnis memcmp-like, I assume. My eyes glazed over when I saw that function implemented instr_diffn.c, too.Just thinking out loud, with sufficient tests I could go with either. Will reroll when/if I get the chance tomorrow.
As mentioned above, it's all subjective and, of course, I have a bias toward the example I provided, but don't otherwise feel strongly about it. I do, however, like the idea of using a simple hand-coded matching function over the regex (but no so much that I would complain about it). Use whatever you and Junio feel is appropriate.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 19 lines · Eric Sunshine
Quoted 18 lines · Eric Wong
Quoted 17 lines · Eric Wong
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · Eric Wong
Quoted 13 lines · Eric Sunshine
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }or something.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
As mentioned above, it's all subjective and, of course, I have a bias toward the example I provided, but don't otherwise feel strongly about it. I do, however, like the idea of using a simple hand-coded matching function over the regex (but no so much that I would complain about it). Use whatever you and Junio feel is appropriate.
This is meant to be a replacement for the original that uses regexec(), which in turn means the string we are checking is guaranteed to be NUL terminated, right?
static int is_mboxrd_from(const char *line) {
return starts_with(line + strspn(line, ">"), "From ");
}is sufficiently high-level that no longer is scary, hopefully?
I agree with you that regexec() is way overkill for something small and simple like this.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 29 lines · Junio C Hamano
Quoted 19 lines · Eric Sunshine
Quoted 18 lines · Eric Wong
Quoted 17 lines · Eric Wong
Quoted 16 lines · Eric Sunshine
Quoted 15 lines · Eric Wong
Quoted 13 lines · Eric Sunshine
I wonder if hand-coding, rather than using a regex, could be an improvement:
static int is_mboxrd_from(const char *s, size_t n) { size_t f = strlen("From "); const char *t = s + n; while (s < t && *s == '>') s++; return t - s >= f && !memcmp(s, "From ", f); }or something.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Yikes. I mostly work in high-level languages and do my best to avoid string parsing in C; so that scares me. A lot.
As mentioned above, it's all subjective and, of course, I have a bias toward the example I provided, but don't otherwise feel strongly about it. I do, however, like the idea of using a simple hand-coded matching function over the regex (but no so much that I would complain about it). Use whatever you and Junio feel is appropriate.
Quoted 3 lines
This is meant to be a replacement for the original that uses
regexec(), which in turn means the string we are checking is guaranteed to be NUL terminated, right?
Yes, this is meant as a replacement for the regexec(), but no, there is no guarantee that the "line" is NUL-terminated. In this case, the 'line' in pretty.c:pp_remainder() might end at a NUL or it might just end at the next newline in the larger buffer.
The bit about sending the "line" to regexec() without taking its logical end-of-line into account was what caught my attention in the first place.
Quoted 5 lines
static int is_mboxrd_from(const char *line) { return starts_with(line + strspn(line, ">"), "From "); }is sufficiently high-level that no longer is scary, hopefully?
That's nice and concise but unfortunately not useful for this case where we must respect the logical end-of-line (within the larger buffer), represented by line+linelen.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 9 lines · Eric Sunshine
Quoted 5 lines
static int is_mboxrd_from(const char *line) { return starts_with(line + strspn(line, ">"), "From "); }is sufficiently high-level that no longer is scary, hopefully?
That's nice and concise but unfortunately not useful for this case where we must respect the logical end-of-line (within the larger buffer), represented by line+linelen.
Hmph, none of ">", "F", "r", "o", "m", or " " is "\n"; would eol still be an issue?
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Eric Sunshine
Quoted 4 lines
static int is_mboxrd_from(const char *line) { return starts_with(line + strspn(line, ">"), "From "); }
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
is sufficiently high-level that no longer is scary, hopefully?
That's nice and concise but unfortunately not useful for this case where we must respect the logical end-of-line (within the larger buffer), represented by line+linelen.
Hmph, none of ">", "F", "r", "o", "m", or " " is "\n"; would eol still be an issue?
Ah right. Sorry for being slow.
Your concise version of is_mboxrd_from() is a nice alternative, as well.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 13 lines · Eric Sunshine
Quoted 12 lines · Junio C Hamano
Quoted 9 lines · Eric Sunshine
Quoted 5 lines
static int is_mboxrd_from(const char *line) { return starts_with(line + strspn(line, ">"), "From "); }is sufficiently high-level that no longer is scary, hopefully?
That's nice and concise but unfortunately not useful for this case where we must respect the logical end-of-line (within the larger buffer), represented by line+linelen.
Quoted 6 lines
Quoted 2 lines
Hmph, none of ">", "F", "r", "o", "m", or " " is "\n"; would eol still be an issue?
Ah right. Sorry for being slow.
Your concise version of
is_mboxrd_from()is a nice alternative, as well.
My only minor reservation is that it your concise version is still subtle with regard to not taking 'linelen' into account. At first glance, it looks like a bug that it doesn't consider the logical end-of-line, and someone reading the code has to put in extra effort to convince himself that the code works as intended.
For that reason, I have a bit of a preference for a version of is_mboxrd_from() which does take 'linelen' into account explicitly.
Re: [PATCH 1/3] pretty: support "mboxrd" output format
Quoted 9 lines · Eric Sunshine
My only minor reservation is that it your concise version is still subtle with regard to not taking 'linelen' into account. At first glance, it looks like a bug that it doesn't consider the logical end-of-line, and someone reading the code has to put in extra effort to convince himself that the code works as intended.
For that reason, I have a bit of a preference for a version of
is_mboxrd_from()which does take 'linelen' into account explicitly.
That certainly is true. You could check that the end result did not consume more than linelen after the single-liner computes its result, but I am not sure under what condition that check would trigger. Even though we may hold the entire e-mail file without NUL-terminating each line, we have a NUL-termination at the end of the file, no?