diff options
author | Jeff King <peff@peff.net> | 2014-08-27 03:56:01 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-08-27 10:25:28 -0700 |
commit | fe6eb7f2c506190c817407accf27834005a57f2b (patch) | |
tree | fde1f530c243bfa9b71762fe29862528320f7ec4 /pretty.c | |
parent | 6c4ab27f2378ce67940b4496365043119d7ffff2 (diff) | |
download | git-fe6eb7f2c506190c817407accf27834005a57f2b.tar.gz |
commit: provide a function to find a header in a buffer
Usually when we parse a commit, we read it line by line and
handle each individual line (e.g., parse_commit and
parse_commit_header). Sometimes, however, we only care
about extracting a single header. Code in this situation is
stuck doing an ad-hoc parse of the commit buffer.
Let's provide a reusable function to locate a header within
the commit. The code is modeled after pretty.c's
get_header, which is used to extract the encoding.
Since some callers may not have the "struct commit" to go
along with the buffer, we drop that parameter. The only
thing lost is a warning for truncated commits, but that's
OK. This shouldn't happen in practice, and even if it does,
there's no particular reason that this function needs to
complain about it. It either finds the header it was asked
for, or it doesn't (and in the latter case, the caller will
typically complain).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pretty.c')
-rw-r--r-- | pretty.c | 33 |
1 files changed, 6 insertions, 27 deletions
@@ -547,31 +547,11 @@ static void add_merge_info(const struct pretty_print_context *pp, strbuf_addch(sb, '\n'); } -static char *get_header(const struct commit *commit, const char *msg, - const char *key) +static char *get_header(const char *msg, const char *key) { - int key_len = strlen(key); - const char *line = msg; - - while (line) { - const char *eol = strchrnul(line, '\n'), *next; - - if (line == eol) - return NULL; - if (!*eol) { - warning("malformed commit (header is missing newline): %s", - sha1_to_hex(commit->object.sha1)); - next = NULL; - } else - next = eol + 1; - if (eol - line > key_len && - !strncmp(line, key, key_len) && - line[key_len] == ' ') { - return xmemdupz(line + key_len + 1, eol - line - key_len - 1); - } - line = next; - } - return NULL; + size_t len; + const char *v = find_commit_header(msg, key, &len); + return v ? xmemdupz(v, len) : NULL; } static char *replace_encoding_header(char *buf, const char *encoding) @@ -617,11 +597,10 @@ const char *logmsg_reencode(const struct commit *commit, if (!output_encoding || !*output_encoding) { if (commit_encoding) - *commit_encoding = - get_header(commit, msg, "encoding"); + *commit_encoding = get_header(msg, "encoding"); return msg; } - encoding = get_header(commit, msg, "encoding"); + encoding = get_header(msg, "encoding"); if (commit_encoding) *commit_encoding = encoding; use_encoding = encoding ? encoding : utf8; |