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/apply.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/apply.c')
-rw-r--r-- | src/apply.c | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/apply.c b/src/apply.c index 7c65cd79d..18304da4d 100644 --- a/src/apply.c +++ b/src/apply.c @@ -265,7 +265,7 @@ done: } static int apply_hunks( - git_buf *out, + git_str *out, const char *source, size_t source_len, git_patch *patch, @@ -286,7 +286,7 @@ static int apply_hunks( } git_vector_foreach(&image.lines, i, line) - git_buf_put(out, line->content, line->content_len); + git_str_put(out, line->content, line->content_len); done: patch_image_free(&image); @@ -295,24 +295,24 @@ done: } static int apply_binary_delta( - git_buf *out, + git_str *out, const char *source, size_t source_len, git_diff_binary_file *binary_file) { - git_buf inflated = GIT_BUF_INIT; + git_str inflated = GIT_STR_INIT; int error = 0; /* no diff means identical contents */ if (binary_file->datalen == 0) - return git_buf_put(out, source, source_len); + return git_str_put(out, source, source_len); error = git_zstream_inflatebuf(&inflated, binary_file->data, binary_file->datalen); if (!error && inflated.size != binary_file->inflatedlen) { error = apply_err("inflated delta does not match expected length"); - git_buf_dispose(out); + git_str_dispose(out); } if (error < 0) @@ -330,7 +330,7 @@ static int apply_binary_delta( out->asize = data_len; } else if (binary_file->type == GIT_DIFF_BINARY_LITERAL) { - git_buf_swap(out, &inflated); + git_str_swap(out, &inflated); } else { error = apply_err("unknown binary delta type"); @@ -338,17 +338,17 @@ static int apply_binary_delta( } done: - git_buf_dispose(&inflated); + git_str_dispose(&inflated); return error; } static int apply_binary( - git_buf *out, + git_str *out, const char *source, size_t source_len, git_patch *patch) { - git_buf reverse = GIT_BUF_INIT; + git_str reverse = GIT_STR_INIT; int error = 0; if (!patch->binary.contains_data) { @@ -378,14 +378,14 @@ static int apply_binary( done: if (error < 0) - git_buf_dispose(out); + git_str_dispose(out); - git_buf_dispose(&reverse); + git_str_dispose(&reverse); return error; } int git_apply__patch( - git_buf *contents_out, + git_str *contents_out, char **filename_out, unsigned int *mode_out, const char *source, @@ -423,13 +423,13 @@ int git_apply__patch( else if (patch->hunks.size) error = apply_hunks(contents_out, source, source_len, patch, &ctx); else - error = git_buf_put(contents_out, source, source_len); + error = git_str_put(contents_out, source, source_len); if (error) goto done; if (patch->delta->status == GIT_DELTA_DELETED && - git_buf_len(contents_out) > 0) { + git_str_len(contents_out) > 0) { error = apply_err("removal patch leaves file contents"); goto done; } @@ -456,7 +456,7 @@ static int apply_one( const git_apply_options *opts) { git_patch *patch = NULL; - git_buf pre_contents = GIT_BUF_INIT, post_contents = GIT_BUF_INIT; + git_str pre_contents = GIT_STR_INIT, post_contents = GIT_STR_INIT; const git_diff_delta *delta; char *filename = NULL; unsigned int mode; @@ -579,8 +579,8 @@ static int apply_one( git_strmap_delete(removed_paths, delta->new_file.path); done: - git_buf_dispose(&pre_contents); - git_buf_dispose(&post_contents); + git_str_dispose(&pre_contents); + git_str_dispose(&post_contents); git__free(filename); git_patch_free(patch); |