create.c 11.3 KB
Newer Older
Ben Straub committed
1 2 3 4 5
#include "clar_libgit2.h"

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
6
#include "ref_helpers.h"
Ben Straub committed
7

8
static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
Ben Straub committed
9 10 11 12
static const char *current_head_target = "refs/heads/master";

static git_repository *g_repo;

13
void test_refs_create__initialize(void)
Ben Straub committed
14
{
15
	g_repo = cl_git_sandbox_init("testrepo");
16
	p_fsync__cnt = 0;
Ben Straub committed
17 18
}

19
void test_refs_create__cleanup(void)
Ben Straub committed
20
{
21
	cl_git_sandbox_cleanup();
22

23
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
24
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 1));
25
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
Ben Straub committed
26 27
}

28
void test_refs_create__symbolic(void)
Ben Straub committed
29
{
30
	/* create a new symbolic reference */
Ben Straub committed
31 32 33 34
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_repository *repo2;
	git_oid id;

35
	const char *new_head_tracker = "ANOTHER_HEAD_TRACKER";
Ben Straub committed
36 37 38 39

	git_oid_fromstr(&id, current_master_tip);

	/* Create and write the new symbolic reference */
40
	cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0, NULL));
Ben Straub committed
41 42 43 44

	/* Ensure the reference can be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
	cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
45
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
46
	cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
Ben Straub committed
47 48 49 50 51 52

	/* ...peeled.. */
	cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
	cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID);

	/* ...and that it points to the current master tip */
53
	cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
Ben Straub committed
54 55 56 57 58 59 60 61
	git_reference_free(looked_up_ref);
	git_reference_free(resolved_ref);

	/* Similar test with a fresh new repository */
	cl_git_pass(git_repository_open(&repo2, "testrepo"));

	cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker));
	cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
62
	cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
Ben Straub committed
63 64 65 66 67 68 69 70

	git_repository_free(repo2);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
	git_reference_free(resolved_ref);
}

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
void test_refs_create__symbolic_with_arbitrary_content(void)
{
	git_reference *new_reference, *looked_up_ref;
	git_repository *repo2;
	git_oid id;

	const char *new_head_tracker = "ANOTHER_HEAD_TRACKER";
	const char *arbitrary_target = "ARBITRARY DATA";

	git_oid_fromstr(&id, current_master_tip);

	/* Attempt to create symbolic ref with arbitrary data in target
	 * fails by default
	 */
	cl_git_fail(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL));

	git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 0);

	/* With strict target validation disabled, ref creation succeeds */
	cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL));

	/* Ensure the reference can be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
	cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
	cl_assert(reference_is_packed(looked_up_ref) == 0);
	cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
97
	git_reference_free(looked_up_ref);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

	/* Ensure the target is what we expect it to be */
	cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target);

	/* Similar test with a fresh new repository object */
	cl_git_pass(git_repository_open(&repo2, "testrepo"));

	/* Ensure the reference can be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker));
	cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC);
	cl_assert(reference_is_packed(looked_up_ref) == 0);
	cl_assert_equal_s(looked_up_ref->name, new_head_tracker);

	/* Ensure the target is what we expect it to be */
	cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target);

	git_repository_free(repo2);
	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
}

119
void test_refs_create__deep_symbolic(void)
Ben Straub committed
120
{
121
	/* create a deep symbolic reference */
Ben Straub committed
122 123 124 125 126 127 128
	git_reference *new_reference, *looked_up_ref, *resolved_ref;
	git_oid id;

	const char *new_head_tracker = "deep/rooted/tracker";

	git_oid_fromstr(&id, current_master_tip);

129
	cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0, NULL));
Ben Straub committed
130 131
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
	cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
132
	cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
Ben Straub committed
133 134 135 136 137 138

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
	git_reference_free(resolved_ref);
}

139
void test_refs_create__oid(void)
Ben Straub committed
140
{
141
	/* create a new OID reference */
Ben Straub committed
142 143 144 145 146 147 148 149 150
	git_reference *new_reference, *looked_up_ref;
	git_repository *repo2;
	git_oid id;

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, current_master_tip);

	/* Create and write the new object id reference */
151
	cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
Ben Straub committed
152 153 154 155

	/* Ensure the reference can be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
	cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID);
156
	cl_assert(reference_is_packed(looked_up_ref) == 0);
Vicent Martí committed
157
	cl_assert_equal_s(looked_up_ref->name, new_head);
Ben Straub committed
158 159

	/* ...and that it points to the current master tip */
160
	cl_assert_equal_oid(&id, git_reference_target(looked_up_ref));
Ben Straub committed
161 162 163 164 165 166
	git_reference_free(looked_up_ref);

	/* Similar test with a fresh new repository */
	cl_git_pass(git_repository_open(&repo2, "testrepo"));

	cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head));
167
	cl_assert_equal_oid(&id, git_reference_target(looked_up_ref));
Ben Straub committed
168 169 170 171 172 173 174

	git_repository_free(repo2);

	git_reference_free(new_reference);
	git_reference_free(looked_up_ref);
}

175
/* Can by default create a reference that targets at an unknown id */
176
void test_refs_create__oid_unknown_succeeds_without_strict(void)
177 178 179 180 181 182 183 184
{
	git_reference *new_reference, *looked_up_ref;
	git_oid id;

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644");

185 186
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));

187 188
	/* Create and write the new object id reference */
	cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
189
	git_reference_free(new_reference);
190 191 192 193 194 195 196

	/* Ensure the reference can't be looked-up... */
	cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
	git_reference_free(looked_up_ref);
}

/* Strict object enforcement enforces valid object id */
197
void test_refs_create__oid_unknown_fails_by_default(void)
Ben Straub committed
198 199 200 201 202 203 204 205 206
{
	git_reference *new_reference, *looked_up_ref;
	git_oid id;

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644");

	/* Create and write the new object id reference */
207
	cl_git_fail(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
Ben Straub committed
208 209 210 211

	/* Ensure the reference can't be looked-up... */
	cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head));
}
212 213 214 215 216 217 218 219 220

void test_refs_create__propagate_eexists(void)
{
	int error;
	git_oid oid;
	git_reference *ref;

	/* Make sure it works for oid and for symbolic both */
	git_oid_fromstr(&oid, current_master_tip);
221
	error = git_reference_create(&ref, g_repo, current_head_target, &oid, false, NULL);
222 223
	cl_assert(error == GIT_EEXISTS);

224
	error = git_reference_symbolic_create(&ref, g_repo, "HEAD", current_head_target, false, NULL);
225 226
	cl_assert(error == GIT_EEXISTS);
}
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
void test_refs_create__existing_dir_propagates_edirectory(void)
{
	git_reference *new_reference, *fail_reference;
	git_oid id;
	const char *dir_head = "refs/heads/new-dir/new-head",
		*fail_head = "refs/heads/new-dir";

	git_oid_fromstr(&id, current_master_tip);

	/* Create and write the new object id reference */
	cl_git_pass(git_reference_create(&new_reference, g_repo, dir_head, &id, 1, NULL));
	cl_git_fail_with(GIT_EDIRECTORY,
		git_reference_create(&fail_reference, g_repo, fail_head, &id, false, NULL));

	git_reference_free(new_reference);
}

245
static void test_invalid_name(const char *name)
246 247 248 249 250 251 252
{
	git_reference *new_reference;
	git_oid id;

	git_oid_fromstr(&id, current_master_tip);

	cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_create(
253
		&new_reference, g_repo, name, &id, 0, NULL));
254 255

	cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(
256
		&new_reference, g_repo, name, current_head_target, 0, NULL));
257
}
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

void test_refs_create__creating_a_reference_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	test_invalid_name("refs/heads/inv@{id");
	test_invalid_name("refs/heads/back\\slash");

	test_invalid_name("refs/heads/foo ");
	test_invalid_name("refs/heads/foo /bar");
	test_invalid_name("refs/heads/com1:bar/foo");

	test_invalid_name("refs/heads/e:");
	test_invalid_name("refs/heads/c:/foo");

	test_invalid_name("refs/heads/foo.");
}

static void test_win32_name(const char *name)
{
	git_reference *new_reference = NULL;
	git_oid id;
	int ret;

	git_oid_fromstr(&id, current_master_tip);

282
	ret = git_reference_create(&new_reference, g_repo, name, &id, 0, NULL);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

#ifdef GIT_WIN32
	cl_assert_equal_i(GIT_EINVALIDSPEC, ret);
#else
	cl_git_pass(ret);
#endif

	git_reference_free(new_reference);
}

void test_refs_create__creating_a_loose_ref_with_invalid_windows_name(void)
{
	test_win32_name("refs/heads/foo./bar");

	test_win32_name("refs/heads/aux");
	test_win32_name("refs/heads/aux.foo/bar");

	test_win32_name("refs/heads/com1");
}
302

303 304 305 306 307 308 309 310 311 312 313 314
/* Creating a loose ref involves fsync'ing the reference, the
 * reflog and (on non-Windows) the containing directories.
 * Creating a packed ref involves fsync'ing the packed ref file
 * and (on non-Windows) the containing directory.
 */
#ifdef GIT_WIN32
static int expected_fsyncs_create = 2, expected_fsyncs_compress = 1;
#else
static int expected_fsyncs_create = 4, expected_fsyncs_compress = 2;
#endif

static void count_fsyncs(size_t *create_count, size_t *compress_count)
315 316
{
	git_reference *ref = NULL;
317
	git_refdb *refdb;
318 319
	git_oid id;

320 321
	p_fsync__cnt = 0;

322 323 324
	git_oid_fromstr(&id, current_master_tip);
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
	git_reference_free(ref);
325

326 327 328
	*create_count = p_fsync__cnt;
	p_fsync__cnt = 0;

329 330 331 332
	cl_git_pass(git_repository_refdb(&refdb, g_repo));
	cl_git_pass(git_refdb_compress(refdb));
	git_refdb_free(refdb);

333 334
	*compress_count = p_fsync__cnt;
	p_fsync__cnt = 0;
335 336
}

337
void test_refs_create__does_not_fsync_by_default(void)
338
{
339 340
	size_t create_count, compress_count;
	count_fsyncs(&create_count, &compress_count);
341

342 343 344 345 346 347 348
	cl_assert_equal_i(0, create_count);
	cl_assert_equal_i(0, compress_count);
}

void test_refs_create__fsyncs_when_global_opt_set(void)
{
	size_t create_count, compress_count;
349

350
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
351
	count_fsyncs(&create_count, &compress_count);
352

353 354 355
	cl_assert_equal_i(expected_fsyncs_create, create_count);
	cl_assert_equal_i(expected_fsyncs_compress, compress_count);
}
356

357 358 359
void test_refs_create__fsyncs_when_repo_config_set(void)
{
	size_t create_count, compress_count;
360

361 362 363
	cl_repo_set_bool(g_repo, "core.fsyncObjectFiles", true);

	count_fsyncs(&create_count, &compress_count);
364

365 366
	cl_assert_equal_i(expected_fsyncs_create, create_count);
	cl_assert_equal_i(expected_fsyncs_compress, compress_count);
367
}