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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "pack.h"
static git_odb *_odb;
static git_repository *_repo;
void test_odb_foreach__cleanup(void)
{
git_odb_free(_odb);
git_repository_free(_repo);
_odb = NULL;
_repo = NULL;
}
static int foreach_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return 0;
}
/*
* $ git --git-dir tests/resources/testrepo.git count-objects --verbose
* count: 60
* size: 240
* in-pack: 1640
* packs: 3
* size-pack: 425
* prune-packable: 0
* garbage: 0
*/
void test_odb_foreach__foreach(void)
{
int nobj = 0;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
git_repository_odb(&_odb, _repo);
cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
}
void test_odb_foreach__one_pack(void)
{
git_odb_backend *backend = NULL;
int nobj = 0;
cl_git_pass(git_odb_new(&_odb));
cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
cl_git_pass(git_odb_add_backend(_odb, backend, 1));
_repo = NULL;
cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
cl_assert(nobj == 1628);
}
static int foreach_stop_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return (*nobj == 1000) ? -321 : 0;
}
static int foreach_stop_first_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return -123;
}
static int foreach_stop_cb_positive_ret(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return (*nobj == 1000) ? 321 : 0;
}
void test_odb_foreach__interrupt_foreach(void)
{
int nobj = 0;
git_oid id;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
git_repository_odb(&_odb, _repo);
cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
cl_assert(nobj == 1000);
nobj = 0;
cl_assert_equal_i(321, git_odb_foreach(_odb, foreach_stop_cb_positive_ret, &nobj));
cl_assert(nobj == 1000);
git_odb_free(_odb);
git_repository_free(_repo);
cl_git_pass(git_repository_init(&_repo, "onlyloose.git", true));
git_repository_odb(&_odb, _repo);
cl_git_pass(git_odb_write(&id, _odb, "", 0, GIT_OBJECT_BLOB));
cl_assert_equal_i(-123, git_odb_foreach(_odb, foreach_stop_first_cb, &nobj));
}
void test_odb_foreach__files_in_objects_dir(void)
{
git_repository *repo;
git_odb *odb;
git_str buf = GIT_STR_INIT;
int nobj = 0;
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_str_joinpath(&buf, git_repository_path(repo), "objects/somefile"));
cl_git_mkfile(buf.ptr, "");
git_str_dispose(&buf);
cl_git_pass(git_repository_odb(&odb, repo));
cl_git_pass(git_odb_foreach(odb, foreach_cb, &nobj));
cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
git_odb_free(odb);
git_repository_free(repo);
cl_fixture_cleanup("testrepo.git");
}