git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC/PATCH 1/3] mailinfo: refactor commit message processing

From
Jeff King <peff@peff.net>
Date
Sep 16, 2016, 21:46 UTC
Message-ID
<20160916214641.ib7khsl3fbtn6smg@sigill.intra.peff.net>
In-Reply-To
<xmqqoa3nk6a5.fsf@gitster.mtv.corp.google.com>
On Fri, Sep 16, 2016 at 12:12:50PM -0700, Junio C Hamano wrote:
Show 16 quoted lines
> > +static int check_header_raw(struct mailinfo *mi,
> > +			    char *buf, size_t len,
> > +			    struct strbuf *hdr_data[], int overwrite) {
> > +	const struct strbuf sb = {0, len, buf};
> > +	return check_header(mi, &sb, hdr_data, overwrite);
> > +}
> 
> IIUC, this is a helper for callers that do not have a strbuf but
> instead have <buf, len> pair to perform the same check_header() the
> callers that have strbuf can do.
> 
> As check_header() uses the strbuf as a read-only entity, wrapping
> the <buf, len> pair in a temporary strbuf like this is safe.
> 
> The incoming <buf> should conceptually be "const char *", but it's
> OK.

I think the "right" way to do this would be to continue taking a "char *", and then strbuf_attach() it. That saves us from unexpectedly violating any strbuf invariants.

If our assumption that check_header() does not touch the contents turns out to be wrong, neither strategy would inform our caller, though. I think you'd want something like:

  assert(sb.buf == buf);

after check_header() returns (though I guess we are in theory protected by the "const").

That being said...
Show 14 quoted lines
> If check_header() didn't call any helper function that gets passed
> &sb as a strbuf, or if convertiong the helper function to take a
> <buf, len> pair instead, I would actually suggest refactoring this
> the other way around, though.  That is, move the implementation of
> check_header() to this function, updating its reference to line->buf
> and line->len to reference to <buf> and <len>, and then make
> check_header() a thin wrapper that does
> 
>         check_header(mi, const struct strbuf *line,
>                      struct strbuf *hdr_data[], int overwrite)
>         {
>                 return check_header_raw(mi, line->buf, line->len,
>                                         hdr_data, overwrite);
>         }

This is _way_ better, and it looks like check_header() could handle it easily. Looking at it, I also suspect the cascading if in that function could be made more pleasant by modeling cmp_header()'s interface after skip_prefix_mem(), but that is totally orthogonal and optional.

-Peff
Previous: Junio C HamanoNext: Jonathan Tan
Message 9 of 24 in “sequencer: support folding in rfc2822 footer”
  1. sequencer: support folding in rfc2822 footerJonathan Tan, Sep 2, 2016
  2. Junio C HamanoSep 3, 2016
  3. Jonathan TanSep 6, 2016
  4. Jonathan TanSep 6, 2016
  5. Jeff KingSep 7, 2016
  6. 0/3 handle multiline in-body headersJonathan Tan, Sep 16, 2016
  7. 1/3 mailinfo: refactor commit message processingJonathan Tan, Sep 16, 2016
  8. Junio C HamanoSep 16, 2016
  9. Jeff KingSep 16, 2016
  10. 2/3 mailinfo: correct malformed test exampleJonathan Tan, Sep 16, 2016
  11. Junio C HamanoSep 16, 2016
  12. Jonathan TanSep 16, 2016
  13. Junio C HamanoSep 16, 2016
  14. Jonathan TanSep 17, 2016
  15. Junio C HamanoSep 17, 2016
  16. 3/3 mailinfo: handle in-body header continuationsJonathan Tan, Sep 16, 2016
  17. Junio C HamanoSep 16, 2016
  18. Jonathan TanSep 16, 2016
  19. Junio C HamanoSep 16, 2016
  20. Jonathan TanSep 16, 2016
  21. Junio C HamanoSep 16, 2016
  22. Jonathan TanSep 17, 2016
  23. Jeff KingSep 16, 2016
  24. Junio C HamanoSep 16, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.