Merge branch 'rs/commit-parsing-optim' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 21 Mar 2017 22:03:29 +0000 (15:03 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 21 Mar 2017 22:03:29 +0000 (15:03 -0700)
The code that parses header fields in the commit object has been
updated for (micro)performance and code hygiene.

* rs/commit-parsing-optim:
commit: don't check for space twice when looking for header
commit: be more precise when searching for headers

1  2 
commit.c
diff --combined commit.c
index 2cf85158b4899b664a3cbae8d0777f5a9e473318,609d597a8d79373aacf9e236b76feb492e7832c1..fab8269731b4764351c7b68543337ded138d4aab
+++ b/commit.c
@@@ -1308,11 -1308,11 +1308,11 @@@ void for_each_mergetag(each_mergetag_f
  
  static inline int standard_header_field(const char *field, size_t len)
  {
-       return ((len == 4 && !memcmp(field, "tree ", 5)) ||
-               (len == 6 && !memcmp(field, "parent ", 7)) ||
-               (len == 6 && !memcmp(field, "author ", 7)) ||
-               (len == 9 && !memcmp(field, "committer ", 10)) ||
-               (len == 8 && !memcmp(field, "encoding ", 9)));
+       return ((len == 4 && !memcmp(field, "tree", 4)) ||
+               (len == 6 && !memcmp(field, "parent", 6)) ||
+               (len == 6 && !memcmp(field, "author", 6)) ||
+               (len == 9 && !memcmp(field, "committer", 9)) ||
+               (len == 8 && !memcmp(field, "encoding", 8)));
  }
  
  static int excluded_header_field(const char *field, size_t len, const char **exclude)
  
        while (*exclude) {
                size_t xlen = strlen(*exclude);
-               if (len == xlen &&
-                   !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
+               if (len == xlen && !memcmp(field, *exclude, xlen))
                        return 1;
                exclude++;
        }
@@@ -1354,12 -1353,11 +1353,11 @@@ static struct commit_extra_header *read
                strbuf_reset(&buf);
                it = NULL;
  
-               eof = strchr(line, ' ');
-               if (next <= eof)
+               eof = memchr(line, ' ', next - line);
+               if (!eof)
                        eof = next;
-               if (standard_header_field(line, eof - line) ||
-                   excluded_header_field(line, eof - line, exclude))
+               else if (standard_header_field(line, eof - line) ||
+                        excluded_header_field(line, eof - line, exclude))
                        continue;
  
                it = xcalloc(1, sizeof(*it));
@@@ -1511,9 -1509,9 +1509,9 @@@ static int verify_utf8(struct strbuf *b
  }
  
  static const char commit_utf8_warn[] =
 -"Warning: commit message did not conform to UTF-8.\n"
 -"You may want to amend it after fixing the message, or set the config\n"
 -"variable i18n.commitencoding to the encoding your project uses.\n";
 +N_("Warning: commit message did not conform to UTF-8.\n"
 +   "You may want to amend it after fixing the message, or set the config\n"
 +   "variable i18n.commitencoding to the encoding your project uses.\n");
  
  int commit_tree_extended(const char *msg, size_t msg_len,
                         const unsigned char *tree,
  
        /* And check the encoding */
        if (encoding_is_utf8 && !verify_utf8(&buffer))
 -              fprintf(stderr, commit_utf8_warn);
 +              fprintf(stderr, _(commit_utf8_warn));
  
        if (sign_commit && do_sign_commit(&buffer, sign_commit))
                return -1;
@@@ -1649,7 -1647,7 +1647,7 @@@ const char *find_commit_header(const ch
  }
  
  /*
 - * Inspect sb and determine the true "end" of the log message, in
 + * Inspect the given string and determine the true "end" of the log message, in
   * order to find where to put a new Signed-off-by: line.  Ignored are
   * trailing comment lines and blank lines, and also the traditional
   * "Conflicts:" block that is not commented out, so that we can use
   * Returns the number of bytes from the tail to ignore, to be fed as
   * the second parameter to append_signoff().
   */
 -int ignore_non_trailer(struct strbuf *sb)
 +int ignore_non_trailer(const char *buf, size_t len)
  {
        int boc = 0;
        int bol = 0;
        int in_old_conflicts_block = 0;
  
 -      while (bol < sb->len) {
 -              char *next_line;
 +      while (bol < len) {
 +              const char *next_line = memchr(buf + bol, '\n', len - bol);
  
 -              if (!(next_line = memchr(sb->buf + bol, '\n', sb->len - bol)))
 -                      next_line = sb->buf + sb->len;
 +              if (!next_line)
 +                      next_line = buf + len;
                else
                        next_line++;
  
 -              if (sb->buf[bol] == comment_line_char || sb->buf[bol] == '\n') {
 +              if (buf[bol] == comment_line_char || buf[bol] == '\n') {
                        /* is this the first of the run of comments? */
                        if (!boc)
                                boc = bol;
                        /* otherwise, it is just continuing */
 -              } else if (starts_with(sb->buf + bol, "Conflicts:\n")) {
 +              } else if (starts_with(buf + bol, "Conflicts:\n")) {
                        in_old_conflicts_block = 1;
                        if (!boc)
                                boc = bol;
 -              } else if (in_old_conflicts_block && sb->buf[bol] == '\t') {
 +              } else if (in_old_conflicts_block && buf[bol] == '\t') {
                        ; /* a pathname in the conflicts block */
                } else if (boc) {
                        /* the previous was not trailing comment */
                        boc = 0;
                        in_old_conflicts_block = 0;
                }
 -              bol = next_line - sb->buf;
 +              bol = next_line - buf;
        }
 -      return boc ? sb->len - boc : 0;
 +      return boc ? len - boc : 0;
  }