Hash :
f0e693b1
Author :
Date :
2021-09-07T17:53:49
str: introduce `git_str` for internal, `git_buf` is external 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include "clar_libgit2.h"
#include "futils.h"
/* Fixture setup and teardown */
void test_core_futils__initialize(void)
{
cl_must_pass(p_mkdir("futils", 0777));
}
void test_core_futils__cleanup(void)
{
cl_fixture_cleanup("futils");
}
void test_core_futils__writebuffer(void)
{
git_str out = GIT_STR_INIT,
append = GIT_STR_INIT;
/* create a new file */
git_str_puts(&out, "hello!\n");
git_str_printf(&out, "this is a %s\n", "test");
cl_git_pass(git_futils_writebuffer(&out, "futils/test-file", O_RDWR|O_CREAT, 0666));
cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
/* append some more data */
git_str_puts(&append, "And some more!\n");
git_str_put(&out, append.ptr, append.size);
cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR|O_APPEND, 0666));
cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
git_str_dispose(&out);
git_str_dispose(&append);
}
void test_core_futils__write_hidden_file(void)
{
#ifndef GIT_WIN32
cl_skip();
#else
git_str out = GIT_STR_INIT, append = GIT_STR_INIT;
bool hidden;
git_str_puts(&out, "hidden file.\n");
git_futils_writebuffer(&out, "futils/test-file", O_RDWR | O_CREAT, 0666);
cl_git_pass(git_win32__set_hidden("futils/test-file", true));
/* append some more data */
git_str_puts(&append, "And some more!\n");
git_str_put(&out, append.ptr, append.size);
cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR | O_APPEND, 0666));
cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
cl_git_pass(git_win32__hidden(&hidden, "futils/test-file"));
cl_assert(hidden);
git_str_dispose(&out);
git_str_dispose(&append);
#endif
}
void test_core_futils__recursive_rmdir_keeps_symlink_targets(void)
{
if (!git_path_supports_symlinks(clar_sandbox_path()))
cl_skip();
cl_git_pass(git_futils_mkdir_r("a/b", 0777));
cl_git_pass(git_futils_mkdir_r("dir-target", 0777));
cl_git_mkfile("dir-target/file", "Contents");
cl_git_mkfile("file-target", "Contents");
cl_must_pass(p_symlink("dir-target", "a/symlink"));
cl_must_pass(p_symlink("file-target", "a/b/symlink"));
cl_git_pass(git_futils_rmdir_r("a", NULL, GIT_RMDIR_REMOVE_FILES));
cl_assert(git_path_exists("dir-target"));
cl_assert(git_path_exists("file-target"));
cl_must_pass(p_unlink("dir-target/file"));
cl_must_pass(p_rmdir("dir-target"));
cl_must_pass(p_unlink("file-target"));
}