iterator.c 6.3 KB
Newer Older
1 2 3
#include "clar_libgit2.h"
#include "refs.h"
#include "vector.h"
4
#include "odb.h"
5
#include "repository.h"
6 7 8 9 10

static git_repository *repo;

void test_refs_iterator__initialize(void)
{
11
	repo = cl_git_sandbox_init("testrepo.git");
12 13 14 15
}

void test_refs_iterator__cleanup(void)
{
16
	cl_git_sandbox_cleanup();
17 18 19
}

static const char *refnames[] = {
20
	"refs/blobs/annotated_tag_to_blob",
21 22 23 24 25 26 27 28 29 30 31 32
	"refs/heads/br2",
	"refs/heads/cannot-fetch",
	"refs/heads/chomped",
	"refs/heads/haacked",
	"refs/heads/master",
	"refs/heads/not-good",
	"refs/heads/packed",
	"refs/heads/packed-test",
	"refs/heads/subtrees",
	"refs/heads/test",
	"refs/heads/track-local",
	"refs/heads/trailing",
33
	"refs/heads/with-empty-log",
34 35 36 37 38 39 40 41 42
	"refs/notes/fanout",
	"refs/remotes/test/master",
	"refs/tags/annotated_tag_to_blob",
	"refs/tags/e90810b",
	"refs/tags/hard_tag",
	"refs/tags/point_to_blob",
	"refs/tags/taggerless",
	"refs/tags/test",
	"refs/tags/wrapped_tag",
43 44 45 46
	NULL
};

static const char *refnames_with_symlink[] = {
47
	"refs/blobs/annotated_tag_to_blob",
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	"refs/heads/br2",
	"refs/heads/cannot-fetch",
	"refs/heads/chomped",
	"refs/heads/haacked",
	"refs/heads/link/a",
	"refs/heads/link/b",
	"refs/heads/link/c",
	"refs/heads/link/d",
	"refs/heads/master",
	"refs/heads/not-good",
	"refs/heads/packed",
	"refs/heads/packed-test",
	"refs/heads/subtrees",
	"refs/heads/test",
	"refs/heads/track-local",
	"refs/heads/trailing",
64
	"refs/heads/with-empty-log",
65 66 67 68 69 70 71 72 73 74
	"refs/notes/fanout",
	"refs/remotes/test/master",
	"refs/tags/annotated_tag_to_blob",
	"refs/tags/e90810b",
	"refs/tags/hard_tag",
	"refs/tags/point_to_blob",
	"refs/tags/taggerless",
	"refs/tags/test",
	"refs/tags/wrapped_tag",
	NULL
75 76
};

Vicent Marti committed
77 78 79 80 81 82 83 84
static int refcmp_cb(const void *a, const void *b)
{
	const git_reference *refa = (const git_reference *)a;
	const git_reference *refb = (const git_reference *)b;

	return strcmp(refa->name, refb->name);
}

85
static void assert_all_refnames_match(const char **expected, git_vector *names)
86 87 88 89
{
	size_t i;
	git_reference *ref;

90
	git_vector_sort(names);
91

92 93 94
	git_vector_foreach(names, i, ref) {
		cl_assert(expected[i] != NULL);
		cl_assert_equal_s(expected[i], ref->name);
95 96
		git_reference_free(ref);
	}
97
	cl_assert(expected[i] == NULL);
98

99
	git_vector_free(names);
100 101
}

102 103 104 105
void test_refs_iterator__list(void)
{
	git_reference_iterator *iter;
	git_vector output;
Vicent Marti committed
106
	git_reference *ref;
107

108
	cl_git_pass(git_vector_init(&output, 33, &refcmp_cb));
109 110
	cl_git_pass(git_reference_iterator_new(&iter, repo));

111 112 113 114 115 116 117
	while (1) {
		int error = git_reference_next(&ref, iter);
		if (error == GIT_ITEROVER)
			break;
		cl_git_pass(error);
		cl_git_pass(git_vector_insert(&output, ref));
	}
118

Vicent Marti committed
119
	git_reference_iterator_free(iter);
120

121
	assert_all_refnames_match(refnames, &output);
122
}
123 124 125 126 127

void test_refs_iterator__empty(void)
{
	git_reference_iterator *iter;
	git_odb *odb;
Vicent Marti committed
128
	git_reference *ref;
129 130
	git_repository *empty;

131
	cl_git_pass(git_odb__new(&odb, NULL));
132
	cl_git_pass(git_repository__wrap_odb(&empty, odb, GIT_OID_SHA1));
133 134

	cl_git_pass(git_reference_iterator_new(&iter, empty));
Vicent Marti committed
135
	cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&ref, iter));
136 137 138 139 140

	git_reference_iterator_free(iter);
	git_odb_free(odb);
	git_repository_free(empty);
}
141 142 143 144 145 146 147 148 149 150 151

static int refs_foreach_cb(git_reference *reference, void *payload)
{
	git_vector *output = payload;
	cl_git_pass(git_vector_insert(output, reference));
	return 0;
}

void test_refs_iterator__foreach(void)
{
	git_vector output;
152
	cl_git_pass(git_vector_init(&output, 33, &refcmp_cb));
153
	cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	assert_all_refnames_match(refnames, &output);
}

void test_refs_iterator__foreach_through_symlink(void)
{
	git_vector output;

#ifdef GIT_WIN32
	cl_skip();
#endif

	cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));

	cl_git_pass(p_mkdir("refs", 0777));
	cl_git_mkfile("refs/a", "1234567890123456789012345678901234567890");
	cl_git_mkfile("refs/b", "1234567890123456789012345678901234567890");
	cl_git_mkfile("refs/c", "1234567890123456789012345678901234567890");
	cl_git_mkfile("refs/d", "1234567890123456789012345678901234567890");

	cl_git_pass(p_symlink("../../../refs", "testrepo.git/refs/heads/link"));

	cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
	assert_all_refnames_match(refnames_with_symlink, &output);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

static int refs_foreach_cancel_cb(git_reference *reference, void *payload)
{
	int *cancel_after = payload;

	git_reference_free(reference);

	if (!*cancel_after)
		return -333;
	(*cancel_after)--;
	return 0;
}

void test_refs_iterator__foreach_can_cancel(void)
{
	int cancel_after = 3;
	cl_git_fail_with(
		git_reference_foreach(repo, refs_foreach_cancel_cb, &cancel_after),
		-333);
	cl_assert_equal_i(0, cancel_after);
}

static int refs_foreach_name_cb(const char *name, void *payload)
{
	git_vector *output = payload;
	cl_git_pass(git_vector_insert(output, git__strdup(name)));
	return 0;
}

void test_refs_iterator__foreach_name(void)
{
	git_vector output;
	size_t i;
	char *name;

	cl_git_pass(git_vector_init(&output, 32, &git__strcmp_cb));
	cl_git_pass(
		git_reference_foreach_name(repo, refs_foreach_name_cb, &output));

	git_vector_sort(&output);

	git_vector_foreach(&output, i, name) {
220 221
		cl_assert(refnames[i] != NULL);
		cl_assert_equal_s(refnames[i], name);
222
		git__free(name);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	}

	git_vector_free(&output);
}

static int refs_foreach_name_cancel_cb(const char *name, void *payload)
{
	int *cancel_after = payload;
	if (!*cancel_after)
		return -333;
	GIT_UNUSED(name);
	(*cancel_after)--;
	return 0;
}

void test_refs_iterator__foreach_name_can_cancel(void)
{
	int cancel_after = 5;
	cl_git_fail_with(
		git_reference_foreach_name(
			repo, refs_foreach_name_cancel_cb, &cancel_after),
		-333);
	cl_assert_equal_i(0, cancel_after);
}
247 248 249 250 251 252 253 254

void test_refs_iterator__concurrent_delete(void)
{
	git_reference_iterator *iter;
	size_t full_count = 0, concurrent_count = 0;
	const char *name;
	int error;

255
	cl_git_sandbox_cleanup();
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	repo = cl_git_sandbox_init("testrepo");

	cl_git_pass(git_reference_iterator_new(&iter, repo));
	while ((error = git_reference_next_name(&name, iter)) == 0) {
		full_count++;
	}

	git_reference_iterator_free(iter);
	cl_assert_equal_i(GIT_ITEROVER, error);

	cl_git_pass(git_reference_iterator_new(&iter, repo));
	while ((error = git_reference_next_name(&name, iter)) == 0) {
		cl_git_pass(git_reference_remove(repo, name));
		concurrent_count++;
	}

	git_reference_iterator_free(iter);
	cl_assert_equal_i(GIT_ITEROVER, error);

	cl_assert_equal_i(full_count, concurrent_count);
}