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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#include "clar_libgit2.h"
#include "futils.h"
#include "fetchhead.h"
#include "../fetchhead/fetchhead_data.h"
#include "git2/clone.h"
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
static git_repository *g_repo;
static git_clone_options g_options;
void test_online_fetchhead__initialize(void)
{
git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
g_options.fetch_opts = dummy_fetch;
}
void test_online_fetchhead__cleanup(void)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
cl_fixture_cleanup("./foo");
}
static void fetchhead_test_clone(void)
{
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
}
static size_t count_references(void)
{
git_strarray array;
size_t refs;
cl_git_pass(git_reference_list(&array, g_repo));
refs = array.count;
git_strarray_dispose(&array);
return refs;
}
static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
{
git_remote *remote;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
git_str fetchhead_buf = GIT_STR_INIT;
int equals = 0;
git_strarray array, *active_refs = NULL;
cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
if(fetchspec != NULL) {
array.count = 1;
array.strings = (char **) &fetchspec;
active_refs = &array;
}
cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL));
git_remote_free(remote);
cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD"));
equals = (strcmp(fetchhead_buf.ptr, expected_fetchhead) == 0);
git_str_dispose(&fetchhead_buf);
cl_assert(equals);
}
void test_online_fetchhead__wildcard_spec(void)
{
fetchhead_test_clone();
fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA2);
cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
cl_git_pass(git_tag_delete(g_repo, "blob"));
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA);
}
void test_online_fetchhead__explicit_spec(void)
{
fetchhead_test_clone();
fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA);
}
void test_online_fetchhead__no_merges(void)
{
git_config *config;
fetchhead_test_clone();
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_delete_entry(config, "branch.master.remote"));
cl_git_pass(git_config_delete_entry(config, "branch.master.merge"));
git_config_free(config);
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA2);
cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
cl_git_pass(git_tag_delete(g_repo, "blob"));
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA);
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3);
}
void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
{
git_reference *ref;
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA);
cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL));
cl_assert_equal_i(refs + 1, count_references());
git_reference_free(ref);
}
void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
{
git_reference *ref;
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA);
cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL));
cl_assert_equal_i(refs, count_references());
}
void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
{
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA);
cl_assert_equal_i(refs, count_references());
}
void test_online_fetchhead__creds_get_stripped(void)
{
git_str buf = GIT_STR_INIT;
git_remote *remote;
cl_git_pass(git_repository_init(&g_repo, "./foo", 0));
cl_git_pass(git_remote_create_anonymous(&remote, g_repo, "https://foo:bar@github.com/libgit2/TestGitRepository"));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
cl_git_pass(git_futils_readbuffer(&buf, "./foo/.git/FETCH_HEAD"));
cl_assert_equal_s(buf.ptr,
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\thttps://github.com/libgit2/TestGitRepository\n");
git_remote_free(remote);
git_str_dispose(&buf);
}