diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2015-06-22 15:32:29 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2015-06-22 15:56:31 +0200 |
commit | a3f42fe8e4cdae8c85ba5d7d7b4c9fd1247d5227 (patch) | |
tree | 123f33cbc69abe842fe66f1d1eea448a9127575d /src | |
parent | e96a97f18e8f961c434e4fa4fc2c7d950480b9e9 (diff) | |
download | libgit2-cmn/commit-header-field.tar.gz |
commit: allow retrieving an arbitrary header fieldcmn/commit-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/commit.c | 55 |
1 files changed, 55 insertions, 0 deletions
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; +} |