diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-07 17:53:49 -0400 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-10-17 09:49:01 -0400 |
commit | f0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch) | |
tree | be5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/commit.c | |
parent | 5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff) | |
download | libgit2-f0e693b18afbe1de37d7da5b5a8967b6c87d8e53.tar.gz |
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by
`git_buf`. We require:
1. A general purpose string class that provides a number of utility APIs
for manipulating data (eg, concatenating, truncating, etc).
2. A structure that we can use to return strings to callers that they
can take ownership of.
By using a single class (`git_buf`) for both of these purposes, we have
confused the API to the point that refactorings are difficult and
reasoning about correctness is also difficult.
Move the utility class `git_buf` to be called `git_str`: this represents
its general purpose, as an internal string buffer class. The name also
is an homage to Junio Hamano ("gitstr").
The public API remains `git_buf`, and has a much smaller footprint. It
is generally only used as an "out" param with strict requirements that
follow the documentation. (Exceptions exist for some legacy APIs to
avoid breaking callers unnecessarily.)
Utility functions exist to convert a user-specified `git_buf` to a
`git_str` so that we can call internal functions, then converting it
back again.
Diffstat (limited to 'src/commit.c')
-rw-r--r-- | src/commit.c | 148 |
1 files changed, 102 insertions, 46 deletions
diff --git a/src/commit.c b/src/commit.c index 96259d5bb..752d98b02 100644 --- a/src/commit.c +++ b/src/commit.c @@ -14,10 +14,10 @@ #include "git2/mailmap.h" #include "git2/sys/commit.h" +#include "buf.h" #include "odb.h" #include "commit.h" #include "signature.h" -#include "message.h" #include "refs.h" #include "object.h" #include "array.h" @@ -42,7 +42,7 @@ void git_commit__free(void *_commit) } static int git_commit__create_buffer_internal( - git_buf *out, + git_str *out, const git_signature *author, const git_signature *committer, const char *message_encoding, @@ -67,17 +67,17 @@ static int git_commit__create_buffer_internal( git_signature__writebuf(out, "committer ", committer); if (message_encoding != NULL) - git_buf_printf(out, "encoding %s\n", message_encoding); + git_str_printf(out, "encoding %s\n", message_encoding); - git_buf_putc(out, '\n'); + git_str_putc(out, '\n'); - if (git_buf_puts(out, message) < 0) + if (git_str_puts(out, message) < 0) goto on_error; return 0; on_error: - git_buf_dispose(out); + git_str_dispose(out); return -1; } @@ -136,7 +136,7 @@ static int git_commit__create_internal( int error; git_odb *odb; git_reference *ref = NULL; - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; const git_oid *current_id = NULL; git_array_oid_t parents = GIT_ARRAY_INIT; @@ -179,7 +179,7 @@ static int git_commit__create_internal( cleanup: git_array_clear(parents); git_reference_free(ref); - git_buf_dispose(&buf); + git_str_dispose(&buf); return error; } @@ -545,7 +545,7 @@ const char *git_commit_message(const git_commit *commit) const char *git_commit_summary(git_commit *commit) { - git_buf summary = GIT_BUF_INIT; + git_str summary = GIT_STR_INIT; const char *msg, *space; bool space_contains_newline = false; @@ -570,17 +570,17 @@ const char *git_commit_summary(git_commit *commit) /* process any recorded whitespace */ if (space) { if(space_contains_newline) - git_buf_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */ + git_str_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */ else - git_buf_put(&summary, space, (msg - space)); /* otherwise copy it */ + git_str_put(&summary, space, (msg - space)); /* otherwise copy it */ space = NULL; } /* copy the next character */ - git_buf_putc(&summary, next_character); + git_str_putc(&summary, next_character); } } - commit->summary = git_buf_detach(&summary); + commit->summary = git_str_detach(&summary); if (!commit->summary) commit->summary = git__strdup(""); } @@ -678,11 +678,22 @@ int git_commit_nth_gen_ancestor( return 0; } -int git_commit_header_field(git_buf *out, const git_commit *commit, const char *field) +int git_commit_header_field( + git_buf *out, + const git_commit *commit, + const char *field) +{ + GIT_BUF_WRAP_PRIVATE(out, git_commit__header_field, commit, field); +} + +int git_commit__header_field( + git_str *out, + const git_commit *commit, + const char *field) { const char *eol, *buf = commit->raw_header; - git_buf_clear(out); + git_str_clear(out); while ((eol = strchr(buf, '\n'))) { /* We can skip continuations here */ @@ -706,22 +717,22 @@ int git_commit_header_field(git_buf *out, const git_commit *commit, const char * buf++; /* skip the SP */ - git_buf_put(out, buf, eol - buf); - if (git_buf_oom(out)) + git_str_put(out, buf, eol - buf); + if (git_str_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'); + git_str_putc(out, '\n'); buf = eol + 2; eol = strchr(buf, '\n'); if (!eol) goto malformed; - git_buf_put(out, buf, eol - buf); + git_str_put(out, buf, eol - buf); } - if (git_buf_oom(out)) + if (git_str_oom(out)) goto oom; return 0; @@ -738,7 +749,35 @@ oom: return -1; } -int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field) +int git_commit_extract_signature( + git_buf *signature_out, + git_buf *signed_data_out, + git_repository *repo, + git_oid *commit_id, + const char *field) +{ + git_str signature = GIT_STR_INIT, signed_data = GIT_STR_INIT; + int error; + + if ((error = git_buf_tostr(&signature, signature_out)) < 0 || + (error = git_buf_tostr(&signed_data, signed_data_out)) < 0 || + (error = git_commit__extract_signature(&signature, &signed_data, repo, commit_id, field)) < 0 || + (error = git_buf_fromstr(signature_out, &signature)) < 0 || + (error = git_buf_fromstr(signed_data_out, &signed_data)) < 0) + goto done; + +done: + git_str_dispose(&signature); + git_str_dispose(&signed_data); + return error; +} + +int git_commit__extract_signature( + git_str *signature, + git_str *signed_data, + git_repository *repo, + git_oid *commit_id, + const char *field) { git_odb_object *obj; git_odb *odb; @@ -746,8 +785,8 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r const char *h, *eol; int error; - git_buf_clear(signature); - git_buf_clear(signed_data); + git_str_clear(signature); + git_str_clear(signed_data); if (!field) field = "gpgsig"; @@ -769,7 +808,7 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r while ((h = strchr(buf, '\n')) && h[1] != '\0') { h++; if (git__prefixcmp(buf, field)) { - if (git_buf_put(signed_data, buf, h - buf) < 0) + if (git_str_put(signed_data, buf, h - buf) < 0) return -1; buf = h; @@ -788,25 +827,25 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r h++; /* skip the SP */ - git_buf_put(signature, h, eol - h); - if (git_buf_oom(signature)) + git_str_put(signature, h, eol - h); + if (git_str_oom(signature)) goto oom; /* If the next line starts with SP, it's multi-line, we must continue */ while (eol[1] == ' ') { - git_buf_putc(signature, '\n'); + git_str_putc(signature, '\n'); h = eol + 2; eol = strchr(h, '\n'); if (!eol) goto malformed; - git_buf_put(signature, h, eol - h); + git_str_put(signature, h, eol - h); } - if (git_buf_oom(signature)) + if (git_str_oom(signature)) goto oom; - error = git_buf_puts(signed_data, eol+1); + error = git_str_puts(signed_data, eol+1); git_odb_object_free(obj); return error; } @@ -826,12 +865,29 @@ oom: cleanup: git_odb_object_free(obj); - git_buf_clear(signature); - git_buf_clear(signed_data); + git_str_clear(signature); + git_str_clear(signed_data); return error; } -int git_commit_create_buffer(git_buf *out, +int git_commit_create_buffer( + git_buf *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]) +{ + GIT_BUF_WRAP_PRIVATE(out, git_commit__create_buffer, repo, + author, committer, message_encoding, message, + tree, parent_count, parents); +} + +int git_commit__create_buffer( + git_str *out, git_repository *repo, const git_signature *author, const git_signature *committer, @@ -866,7 +922,7 @@ int git_commit_create_buffer(git_buf *out, /** * Append to 'out' properly marking continuations when there's a newline in 'content' */ -static int format_header_field(git_buf *out, const char *field, const char *content) +static int format_header_field(git_str *out, const char *field, const char *content) { const char *lf; @@ -874,19 +930,19 @@ static int format_header_field(git_buf *out, const char *field, const char *cont GIT_ASSERT_ARG(field); GIT_ASSERT_ARG(content); - git_buf_puts(out, field); - git_buf_putc(out, ' '); + git_str_puts(out, field); + git_str_putc(out, ' '); while ((lf = strchr(content, '\n')) != NULL) { - git_buf_put(out, content, lf - content); - git_buf_puts(out, "\n "); + git_str_put(out, content, lf - content); + git_str_puts(out, "\n "); content = lf + 1; } - git_buf_puts(out, content); - git_buf_putc(out, '\n'); + git_str_puts(out, content); + git_str_putc(out, '\n'); - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; } static const git_oid *commit_parent_from_commit(size_t n, void *payload) @@ -908,7 +964,7 @@ int git_commit_create_with_signature( int error = 0; const char *field; const char *header_end; - git_buf commit = GIT_BUF_INIT; + git_str commit = GIT_STR_INIT; git_commit *parsed; git_array_oid_t parents = GIT_ARRAY_INIT; @@ -933,7 +989,7 @@ int git_commit_create_with_signature( /* The header ends after the first LF */ header_end++; - git_buf_put(&commit, commit_content, header_end - commit_content); + git_str_put(&commit, commit_content, header_end - commit_content); if (signature != NULL) { field = signature_field ? signature_field : "gpgsig"; @@ -942,9 +998,9 @@ int git_commit_create_with_signature( goto cleanup; } - git_buf_puts(&commit, header_end); + git_str_puts(&commit, header_end); - if (git_buf_oom(&commit)) + if (git_str_oom(&commit)) return -1; if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) @@ -955,7 +1011,7 @@ int git_commit_create_with_signature( cleanup: git_commit__free(parsed); - git_buf_dispose(&commit); + git_str_dispose(&commit); return error; } |