diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-22 11:24:20 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-22 11:24:20 -0400 |
| commit | 0b6ed4f96c7253b97a86f7e88ba4d69888c72382 (patch) | |
| tree | b66690fdb301026533d63be7f0e5fdba40ca1dd4 /src | |
| parent | 0c94deb90f1ce0e8caf171c85522c66ceb37a4ab (diff) | |
| parent | a3f42fe8e4cdae8c85ba5d7d7b4c9fd1247d5227 (diff) | |
| download | libgit2-0b6ed4f96c7253b97a86f7e88ba4d69888c72382.tar.gz | |
Merge pull request #3240 from libgit2/cmn/commit-header-field
commit: allow retrieving an arbitrary header field
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; +} |
