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

Re: [PATCH] am: match --signoff to the original scripted version

From
Jeff King <peff@peff.net>
Date
Sep 8, 2015, 06:18 UTC
Message-ID
<20150908061836.GD26331@sigill.intra.peff.net>
In-Reply-To
<xmqqd1xvbgw3.fsf@gitster.mtv.corp.google.com>
On Sun, Sep 06, 2015 at 10:24:12AM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> >> +       /* Does it end with our own sign-off? */
> >> +       strbuf_addf(&mine, "\n%s%s\n",
> >> +                   sign_off_header,
> >> +                   fmt_name(getenv("GIT_COMMITTER_NAME"),
> >> +                            getenv("GIT_COMMITTER_EMAIL")));
> >
> > Maybe use git_committer_info() here?
> 
> Perhaps, but I wanted to make sure I am doing the same thing as the
> codepath of sequencer.c::append_signoff(), which the original ended
> up calling.  git_committer_info() does way more than that, no?

Not really. I think git_committer_info(IDENT_STRICT | IDENT_NO_DATE) runs the exact same code, with one exception: we would also set the ident_explicitly_given variables. But nobody in builtin/am.c calls committer_ident_sufficiently_given(). And if they did, I think the change would be an improvement.

Show 7 quoted lines
> >> +       if (mine.len < sb->len &&
> >> +           !strcmp(mine.buf, sb->buf + sb->len - mine.len))
> >
> > Perhaps use ends_with()?
> 
> This caller already _knows_ how long the sb->buf string is; it is
> pointless to have ends_with() run strlen() on it.

That actually goes double. We know sb.len. The ends_with() function is built around strip_suffix(), which both operate on strings. But we do not have ends_with_mem() built around strip_suffix_mem().

But we also know mine.len. Even strip_suffix_mem() assumes the suffix itself is a string. So what you really want is:

  int strip_suffix_mem_mem(const char *buf, size_t *len,
                           const char *suffix, size_t suffix_len);

and then you can trivially build the existing strip_suffix_mem() around it, build strip_suffix() around that, and then build ends_with(), ends_with_mem() and ends_with_mem_mem() around those. And don't forget strbuf_ends_with(), strbuf_ends_with_mem(), and strbuf_ends_with_strbuf()

:) I am only half tongue in cheek. The proliferation of names is tedious (and not appropriate for an -rc regression fix), but I do think the resulting code is a lot more obvious as:

  if (strbuf_ends_with_strbuf(&sb, &mine))
      ...
or even:
  if (ends_with_mem_mem(sb->buf, sb->len, mine.buf, mine.len))
      ...

Of course given that this is run only once per program, and that these _are_ in fact strings, we can probably not bother to optimize it and just accept:

  if (ends_with(sb->buf, mine.buf))
      ...

But if you want to go all-out on optimization, I think you can replace your strcmp with memcmp:

  if (mine.len < sb->len &&
      !memcmp(mine.buf, sb->buf + sb->len - mine.len, mine.len))
(assuming that memcmp is in fact faster than strcmp).
-Peff
Previous: Junio C HamanoNext: Paul Tan
Message 25 of 30 in “More builtin git-am issues..”
  1. Linus TorvaldsSep 4, 2015
  2. Linus TorvaldsSep 4, 2015
  3. Jeff KingSep 5, 2015
  4. Linus TorvaldsSep 5, 2015
  5. Junio C HamanoSep 5, 2015
  6. Linus TorvaldsSep 5, 2015
  7. Junio C HamanoSep 5, 2015
  8. Linus TorvaldsSep 5, 2015
  9. Junio C HamanoSep 5, 2015
  10. Johannes SixtSep 5, 2015
  11. Jeff KingSep 5, 2015
  12. Junio C HamanoSep 5, 2015
  13. Junio C HamanoSep 5, 2015
  14. Junio C HamanoSep 5, 2015
  15. Junio C HamanoSep 5, 2015
  16. Christian CouderSep 7, 2015
  17. Junio C HamanoSep 5, 2015
  18. Junio C HamanoSep 5, 2015
  19. Junio C HamanoSep 5, 2015
  20. Linus TorvaldsSep 5, 2015
  21. Junio C HamanoSep 5, 2015
  22. am: match --signoff to the original scripted versionJunio C Hamano, Sep 6, 2015
  23. Paul TanSep 6, 2015
  24. Junio C HamanoSep 6, 2015
  25. Jeff KingSep 8, 2015
  26. Paul TanSep 6, 2015
  27. Junio C HamanoSep 6, 2015
  28. Linus TorvaldsSep 6, 2015
  29. Jeff KingSep 8, 2015
  30. Junio C HamanoSep 8, 2015

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.