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 /tests/clar_libgit2.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 'tests/clar_libgit2.c')
-rw-r--r-- | tests/clar_libgit2.c | 74 |
1 files changed, 35 insertions, 39 deletions
diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c index c4550c32a..adea597e3 100644 --- a/tests/clar_libgit2.c +++ b/tests/clar_libgit2.c @@ -68,7 +68,7 @@ void cl_git_rmfile(const char *filename) char *cl_getenv(const char *name) { - git_buf out = GIT_BUF_INIT; + git_str out = GIT_STR_INIT; int error = git__getenv(&out, name); cl_assert(error >= 0 || error == GIT_ENOTFOUND); @@ -83,7 +83,7 @@ char *cl_getenv(const char *name) return dup; } - return git_buf_detach(&out); + return git_str_detach(&out); } bool cl_is_env_set(const char *name) @@ -278,11 +278,11 @@ const char* cl_git_path_url(const char *path) static char url[4096 + 1]; const char *in_buf; - git_buf path_buf = GIT_BUF_INIT; - git_buf url_buf = GIT_BUF_INIT; + git_str path_buf = GIT_STR_INIT; + git_str url_buf = GIT_STR_INIT; cl_git_pass(git_path_prettify_dir(&path_buf, path, NULL)); - cl_git_pass(git_buf_puts(&url_buf, "file://")); + cl_git_pass(git_str_puts(&url_buf, "file://")); #ifdef GIT_WIN32 /* @@ -294,29 +294,29 @@ const char* cl_git_path_url(const char *path) * *nix: file:///usr/home/... * Windows: file:///C:/Users/... */ - cl_git_pass(git_buf_putc(&url_buf, '/')); + cl_git_pass(git_str_putc(&url_buf, '/')); #endif - in_buf = git_buf_cstr(&path_buf); + in_buf = git_str_cstr(&path_buf); /* * A very hacky Url encoding that only takes care of escaping the spaces */ while (*in_buf) { if (*in_buf == ' ') - cl_git_pass(git_buf_puts(&url_buf, "%20")); + cl_git_pass(git_str_puts(&url_buf, "%20")); else - cl_git_pass(git_buf_putc(&url_buf, *in_buf)); + cl_git_pass(git_str_putc(&url_buf, *in_buf)); in_buf++; } cl_assert(url_buf.size < sizeof(url) - 1); - strncpy(url, git_buf_cstr(&url_buf), sizeof(url) - 1); + strncpy(url, git_str_cstr(&url_buf), sizeof(url) - 1); url[sizeof(url) - 1] = '\0'; - git_buf_dispose(&url_buf); - git_buf_dispose(&path_buf); + git_str_dispose(&url_buf); + git_str_dispose(&path_buf); return url; } @@ -324,15 +324,15 @@ const char *cl_git_sandbox_path(int is_dir, ...) { const char *path = NULL; static char _temp[GIT_PATH_MAX]; - git_buf buf = GIT_BUF_INIT; + git_str buf = GIT_STR_INIT; va_list arg; - cl_git_pass(git_buf_sets(&buf, clar_sandbox_path())); + cl_git_pass(git_str_sets(&buf, clar_sandbox_path())); va_start(arg, is_dir); while ((path = va_arg(arg, const char *)) != NULL) { - cl_git_pass(git_buf_joinpath(&buf, buf.ptr, path)); + cl_git_pass(git_str_joinpath(&buf, buf.ptr, path)); } va_end(arg); @@ -341,10 +341,10 @@ const char *cl_git_sandbox_path(int is_dir, ...) git_path_to_dir(&buf); /* make sure we won't truncate */ - cl_assert(git_buf_len(&buf) < sizeof(_temp)); - git_buf_copy_cstr(_temp, sizeof(_temp), &buf); + cl_assert(git_str_len(&buf) < sizeof(_temp)); + git_str_copy_cstr(_temp, sizeof(_temp), &buf); - git_buf_dispose(&buf); + git_str_dispose(&buf); return _temp; } @@ -354,7 +354,7 @@ typedef struct { size_t filename_len; } remove_data; -static int remove_placeholders_recurs(void *_data, git_buf *path) +static int remove_placeholders_recurs(void *_data, git_str *path) { remove_data *data = (remove_data *)_data; size_t pathlen; @@ -380,12 +380,12 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename) { int error; remove_data data; - git_buf buffer = GIT_BUF_INIT; + git_str buffer = GIT_STR_INIT; if (git_path_isdir(directory_path) == false) return -1; - if (git_buf_sets(&buffer, directory_path) < 0) + if (git_str_sets(&buffer, directory_path) < 0) return -1; data.filename = filename; @@ -393,7 +393,7 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename) error = remove_placeholders_recurs(&data, &buffer); - git_buf_dispose(&buffer); + git_str_dispose(&buffer); return error; } @@ -548,30 +548,26 @@ void clar__assert_equal_file( (size_t)expected_bytes, (size_t)total_bytes); } -static char *_cl_restore_home = NULL; +static git_buf _cl_restore_home = GIT_BUF_INIT; void cl_fake_home_cleanup(void *payload) { - char *restore = _cl_restore_home; - _cl_restore_home = NULL; - GIT_UNUSED(payload); - if (restore) { + if (_cl_restore_home.ptr) { cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, restore)); - git__free(restore); + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, _cl_restore_home.ptr)); + git_buf_dispose(&_cl_restore_home); } } void cl_fake_home(void) { - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; cl_git_pass(git_libgit2_opts( - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &path)); + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &_cl_restore_home)); - _cl_restore_home = git_buf_detach(&path); cl_set_cleanup(cl_fake_home_cleanup, NULL); if (!git_path_exists("home")) @@ -579,14 +575,14 @@ void cl_fake_home(void) cl_git_pass(git_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); - git_buf_dispose(&path); + git_str_dispose(&path); } void cl_sandbox_set_search_path_defaults(void) { - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; - git_buf_joinpath(&path, clar_sandbox_path(), "__config"); + git_str_joinpath(&path, clar_sandbox_path(), "__config"); if (!git_path_exists(path.ptr)) cl_must_pass(p_mkdir(path.ptr, 0777)); @@ -600,18 +596,18 @@ void cl_sandbox_set_search_path_defaults(void) git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_PROGRAMDATA, path.ptr); - git_buf_dispose(&path); + git_str_dispose(&path); } #ifdef GIT_WIN32 bool cl_sandbox_supports_8dot3(void) { - git_buf longpath = GIT_BUF_INIT; + git_str longpath = GIT_STR_INIT; char *shortname; bool supported; cl_git_pass( - git_buf_joinpath(&longpath, clar_sandbox_path(), "longer_than_8dot3")); + git_str_joinpath(&longpath, clar_sandbox_path(), "longer_than_8dot3")); cl_git_write2file(longpath.ptr, "", 0, O_RDWR|O_CREAT, 0666); shortname = git_win32_path_8dot3_name(longpath.ptr); @@ -619,7 +615,7 @@ bool cl_sandbox_supports_8dot3(void) supported = (shortname != NULL); git__free(shortname); - git_buf_dispose(&longpath); + git_str_dispose(&longpath); return supported; } |