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/diff.c | |
parent | 5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff) | |
download | libgit2-ethomson/gitstr.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/diff.c')
-rw-r--r-- | src/diff.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/diff.c b/src/diff.c index 44b67ee06..9840d6050 100644 --- a/src/diff.c +++ b/src/diff.c @@ -8,6 +8,7 @@ #include "diff.h" #include "common.h" +#include "buf.h" #include "patch.h" #include "email.h" #include "commit.h" @@ -162,6 +163,7 @@ int git_diff_format_email( const git_diff_format_email_options *opts) { git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT; + git_str email = GIT_STR_INIT; int error; GIT_ASSERT_ARG(out); @@ -172,14 +174,29 @@ int git_diff_format_email( GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, "git_format_email_options"); + /* This is a `git_buf` special case; subsequent calls append. */ + email.ptr = out->ptr; + email.asize = out->reserved; + email.size = out->size; + + out->ptr = git_str__initstr; + out->reserved = 0; + out->size = 0; + if ((opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0) email_create_opts.subject_prefix = ""; - - error = git_email__append_from_diff(out, diff, opts->patch_no, + error = git_email__append_from_diff(&email, diff, opts->patch_no, opts->total_patches, opts->id, opts->summary, opts->body, opts->author, &email_create_opts); + if (error < 0) + goto done; + + error = git_buf_fromstr(out, &email); + +done: + git_str_dispose(&email); return error; } @@ -282,7 +299,7 @@ static int flush_hunk(git_oid *result, git_hash_ctx *ctx) return 0; } -static void strip_spaces(git_buf *buf) +static void strip_spaces(git_str *buf) { char *src = buf->ptr, *dst = buf->ptr; char c; @@ -295,7 +312,7 @@ static void strip_spaces(git_buf *buf) } } - git_buf_truncate(buf, len); + git_str_truncate(buf, len); } static int diff_patchid_print_callback_to_buf( @@ -305,7 +322,7 @@ static int diff_patchid_print_callback_to_buf( void *payload) { struct patch_id_args *args = (struct patch_id_args *) payload; - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; int error = 0; if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL || @@ -331,7 +348,7 @@ static int diff_patchid_print_callback_to_buf( args->first_file = 0; out: - git_buf_dispose(&buf); + git_str_dispose(&buf); return error; } |