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/describe.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/describe.c')
-rw-r--r-- | src/describe.c | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/describe.c b/src/describe.c index 103d0da5c..1033eac50 100644 --- a/src/describe.c +++ b/src/describe.c @@ -12,6 +12,7 @@ #include "git2/diff.h" #include "git2/status.h" +#include "buf.h" #include "commit.h" #include "commit_list.h" #include "oidmap.h" @@ -322,7 +323,7 @@ static unsigned long finish_depth_computation( return seen_commits; } -static int display_name(git_buf *buf, git_repository *repo, struct commit_name *n) +static int display_name(git_str *buf, git_repository *repo, struct commit_name *n) { if (n->prio == 2 && !n->tag) { if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) { @@ -346,9 +347,9 @@ static int display_name(git_buf *buf, git_repository *repo, struct commit_name * } if (n->tag) - git_buf_printf(buf, "%s", git_tag_name(n->tag)); + git_str_printf(buf, "%s", git_tag_name(n->tag)); else - git_buf_printf(buf, "%s", n->path); + git_str_printf(buf, "%s", n->path); return 0; } @@ -388,7 +389,7 @@ static int find_unique_abbrev_size( } static int show_suffix( - git_buf *buf, + git_str *buf, int depth, git_repository *repo, const git_oid *id, @@ -403,11 +404,11 @@ static int show_suffix( git_oid_fmt(hex_oid, id); - git_buf_printf(buf, "-%d-g", depth); + git_str_printf(buf, "-%d-g", depth); - git_buf_put(buf, hex_oid, size); + git_str_put(buf, hex_oid, size); - return git_buf_oom(buf) ? -1 : 0; + return git_str_oom(buf) ? -1 : 0; } #define MAX_CANDIDATES_TAGS FLAG_BITS - 1 @@ -769,7 +770,10 @@ static int normalize_format_options( return 0; } -int git_describe_format(git_buf *out, const git_describe_result *result, const git_describe_format_options *given) +static int git_describe__format( + git_str *out, + const git_describe_result *result, + const git_describe_format_options *given) { int error; git_repository *repo; @@ -782,10 +786,6 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options"); normalize_format_options(&opts, given); - if ((error = git_buf_sanitize(out)) < 0) - return error; - - if (opts.always_use_long_format && opts.abbreviated_size == 0) { git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - " "'always_use_long_format' is incompatible with a zero" @@ -809,9 +809,9 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g } if (result->dirty && opts.dirty_suffix) - git_buf_puts(out, opts.dirty_suffix); + git_str_puts(out, opts.dirty_suffix); - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; } /* If we didn't find *any* tags, we fall back to the commit's id */ @@ -824,12 +824,12 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g return -1; git_oid_fmt(hex_oid, &result->commit_id); - git_buf_put(out, hex_oid, size); + git_str_put(out, hex_oid, size); if (result->dirty && opts.dirty_suffix) - git_buf_puts(out, opts.dirty_suffix); + git_str_puts(out, opts.dirty_suffix); - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; } /* Lastly, if we found a matching tag, we show that */ @@ -845,10 +845,18 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g } if (result->dirty && opts.dirty_suffix) { - git_buf_puts(out, opts.dirty_suffix); + git_str_puts(out, opts.dirty_suffix); } - return git_buf_oom(out) ? -1 : 0; + return git_str_oom(out) ? -1 : 0; +} + +int git_describe_format( + git_buf *out, + const git_describe_result *result, + const git_describe_format_options *given) +{ + GIT_BUF_WRAP_PRIVATE(out, git_describe__format, result, given); } void git_describe_result_free(git_describe_result *result) |