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/odb.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/odb.c')
-rw-r--r-- | src/odb.c | 34 |
1 files changed, 17 insertions, 17 deletions
@@ -109,7 +109,7 @@ int git_odb__format_object_header( int git_odb__hashobj(git_oid *id, git_rawobj *obj) { - git_buf_vec vec[2]; + git_str_vec vec[2]; char header[64]; size_t hdrlen; int error; @@ -248,7 +248,7 @@ int git_odb__hashfd_filtered( git_oid *out, git_file fd, size_t size, git_object_t type, git_filter_list *fl) { int error; - git_buf raw = GIT_BUF_INIT; + git_str raw = GIT_STR_INIT; if (!fl) return git_odb__hashfd(out, fd, size, type); @@ -258,14 +258,14 @@ int git_odb__hashfd_filtered( */ if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) { - git_buf post = GIT_BUF_INIT; + git_str post = GIT_STR_INIT; error = git_filter_list__convert_buf(&post, fl, &raw); if (!error) error = git_odb_hash(out, post.ptr, post.size, type); - git_buf_dispose(&post); + git_str_dispose(&post); } return error; @@ -636,8 +636,8 @@ int git_odb__add_default_backends( static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_depth) { - git_buf alternates_path = GIT_BUF_INIT; - git_buf alternates_buf = GIT_BUF_INIT; + git_str alternates_path = GIT_STR_INIT; + git_str alternates_buf = GIT_STR_INIT; char *buffer; const char *alternate; int result = 0; @@ -646,16 +646,16 @@ static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_ if (alternate_depth > GIT_ALTERNATES_MAX_DEPTH) return 0; - if (git_buf_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0) + if (git_str_joinpath(&alternates_path, objects_dir, GIT_ALTERNATES_FILE) < 0) return -1; if (git_path_exists(alternates_path.ptr) == false) { - git_buf_dispose(&alternates_path); + git_str_dispose(&alternates_path); return 0; } if (git_futils_readbuffer(&alternates_buf, alternates_path.ptr) < 0) { - git_buf_dispose(&alternates_path); + git_str_dispose(&alternates_path); return -1; } @@ -672,17 +672,17 @@ static int load_alternates(git_odb *odb, const char *objects_dir, int alternate_ * the current repository. */ if (*alternate == '.' && !alternate_depth) { - if ((result = git_buf_joinpath(&alternates_path, objects_dir, alternate)) < 0) + if ((result = git_str_joinpath(&alternates_path, objects_dir, alternate)) < 0) break; - alternate = git_buf_cstr(&alternates_path); + alternate = git_str_cstr(&alternates_path); } if ((result = git_odb__add_default_backends(odb, alternate, true, alternate_depth + 1)) < 0) break; } - git_buf_dispose(&alternates_path); - git_buf_dispose(&alternates_buf); + git_str_dispose(&alternates_path); + git_str_dispose(&alternates_buf); return result; } @@ -1337,15 +1337,15 @@ static int read_prefix_1(git_odb_object **out, git_odb *db, data = raw.data; if (found && git_oid__cmp(&full_oid, &found_full_oid)) { - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; - git_buf_printf(&buf, "multiple matches for prefix: %s", + git_str_printf(&buf, "multiple matches for prefix: %s", git_oid_tostr_s(&full_oid)); - git_buf_printf(&buf, " %s", + git_str_printf(&buf, " %s", git_oid_tostr_s(&found_full_oid)); error = git_odb__error_ambiguous(buf.ptr); - git_buf_dispose(&buf); + git_str_dispose(&buf); git_mutex_unlock(&db->lock); goto out; } |