From a3f42fe8e4cdae8c85ba5d7d7b4c9fd1247d5227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 22 Jun 2015 15:32:29 +0200 Subject: commit: allow retrieving an arbitrary header field This allows the user to look up fields which we don't parse in libgit2, and allows them to access gpgsig or mergetag fields if they wish to check the signature. --- src/commit.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/commit.c') diff --git a/src/commit.c b/src/commit.c index ce13bdb85..616f947db 100644 --- a/src/commit.c +++ b/src/commit.c @@ -518,3 +518,58 @@ int git_commit_nth_gen_ancestor( *ancestor = parent; return 0; } + +int git_commit_header_field(git_buf *out, const git_commit *commit, const char *field) +{ + const char *buf = commit->raw_header; + const char *h, *eol; + + git_buf_sanitize(out); + while ((h = strchr(buf, '\n')) && h[1] != '\0' && h[1] != '\n') { + h++; + if (git__prefixcmp(h, field)) { + buf = h; + continue; + } + + h += strlen(field); + eol = strchr(h, '\n'); + if (h[0] != ' ') { + buf = h; + continue; + } + if (!eol) + goto malformed; + + h++; /* skip the SP */ + + git_buf_put(out, h, eol - h); + if (git_buf_oom(out)) + goto oom; + + /* If the next line starts with SP, it's multi-line, we must continue */ + while (eol[1] == ' ') { + git_buf_putc(out, '\n'); + h = eol + 2; + eol = strchr(h, '\n'); + if (!eol) + goto malformed; + + git_buf_put(out, h, eol - h); + } + + if (git_buf_oom(out)) + goto oom; + + return 0; + } + + return GIT_ENOTFOUND; + +malformed: + giterr_set(GITERR_OBJECT, "malformed header"); + return -1; +oom: + giterr_set_oom(); + return -1; +} -- cgit v1.2.1