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/fetchhead.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/fetchhead.c')
-rw-r--r-- | src/fetchhead.c | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/fetchhead.c b/src/fetchhead.c index 88c567e48..6511124ef 100644 --- a/src/fetchhead.c +++ b/src/fetchhead.c @@ -10,7 +10,7 @@ #include "git2/types.h" #include "git2/oid.h" -#include "buffer.h" +#include "str.h" #include "futils.h" #include "filebuf.h" #include "refs.h" @@ -44,7 +44,7 @@ static char *sanitized_remote_url(const char *remote_url) int error; if (git_net_url_parse(&url, remote_url) == 0) { - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; git__free(url.username); git__free(url.password); @@ -53,7 +53,7 @@ static char *sanitized_remote_url(const char *remote_url) if ((error = git_net_url_fmt(&buf, &url)) < 0) goto fallback; - sanitized = git_buf_detach(&buf); + sanitized = git_str_detach(&buf); } fallback: @@ -143,22 +143,22 @@ static int fetchhead_ref_write( int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs) { git_filebuf file = GIT_FILEBUF_INIT; - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; unsigned int i; git_fetchhead_ref *fetchhead_ref; GIT_ASSERT_ARG(repo); GIT_ASSERT_ARG(fetchhead_refs); - if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) + if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) return -1; if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_APPEND, GIT_REFS_FILE_MODE) < 0) { - git_buf_dispose(&path); + git_str_dispose(&path); return -1; } - git_buf_dispose(&path); + git_str_dispose(&path); git_vector_sort(fetchhead_refs); @@ -171,7 +171,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs) static int fetchhead_ref_parse( git_oid *oid, unsigned int *is_merge, - git_buf *ref_name, + git_str *ref_name, const char **remote_url, char *line, size_t line_num) @@ -259,12 +259,12 @@ static int fetchhead_ref_parse( *remote_url = desc; } - git_buf_clear(ref_name); + git_str_clear(ref_name); if (type) - git_buf_join(ref_name, '/', type, name); + git_str_join(ref_name, '/', type, name); else if(name) - git_buf_puts(ref_name, name); + git_str_puts(ref_name, name); return error; } @@ -273,7 +273,7 @@ int git_repository_fetchhead_foreach(git_repository *repo, git_repository_fetchhead_foreach_cb cb, void *payload) { - git_buf path = GIT_BUF_INIT, file = GIT_BUF_INIT, name = GIT_BUF_INIT; + git_str path = GIT_STR_INIT, file = GIT_STR_INIT, name = GIT_STR_INIT; const char *ref_name; git_oid oid; const char *remote_url; @@ -285,10 +285,10 @@ int git_repository_fetchhead_foreach(git_repository *repo, GIT_ASSERT_ARG(repo); GIT_ASSERT_ARG(cb); - if (git_buf_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) + if (git_str_joinpath(&path, repo->gitdir, GIT_FETCH_HEAD_FILE) < 0) return -1; - if ((error = git_futils_readbuffer(&file, git_buf_cstr(&path))) < 0) + if ((error = git_futils_readbuffer(&file, git_str_cstr(&path))) < 0) goto done; buffer = file.ptr; @@ -300,8 +300,8 @@ int git_repository_fetchhead_foreach(git_repository *repo, &oid, &is_merge, &name, &remote_url, line, line_num)) < 0) goto done; - if (git_buf_len(&name) > 0) - ref_name = git_buf_cstr(&name); + if (git_str_len(&name) > 0) + ref_name = git_str_cstr(&name); else ref_name = NULL; @@ -319,9 +319,9 @@ int git_repository_fetchhead_foreach(git_repository *repo, } done: - git_buf_dispose(&file); - git_buf_dispose(&path); - git_buf_dispose(&name); + git_str_dispose(&file); + git_str_dispose(&path); + git_str_dispose(&name); return error; } |