reflog.c 14.5 KB
Newer Older
Ben Straub committed
1 2
#include "clar_libgit2.h"

3
#include "futils.h"
Ben Straub committed
4 5 6 7 8
#include "git2/reflog.h"
#include "reflog.h"

static const char *new_ref = "refs/heads/test-reflog";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
9
#define commit_msg "commit: bla bla"
Ben Straub committed
10 11 12 13

static git_repository *g_repo;


14
/* helpers */
15
static void assert_signature(const git_signature *expected, const git_signature *actual)
Ben Straub committed
16
{
17
	cl_assert(actual);
Vicent Martí committed
18 19
	cl_assert_equal_s(expected->name, actual->name);
	cl_assert_equal_s(expected->email, actual->email);
20 21
	cl_assert(expected->when.offset == actual->when.offset);
	cl_assert(expected->when.time == actual->when.time);
Ben Straub committed
22 23 24
}


25
/* Fixture setup and teardown */
26
void test_refs_reflog_reflog__initialize(void)
Ben Straub committed
27
{
28
	g_repo = cl_git_sandbox_init("testrepo.git");
Ben Straub committed
29 30
}

31
void test_refs_reflog_reflog__cleanup(void)
Ben Straub committed
32
{
33
	cl_git_sandbox_cleanup();
Ben Straub committed
34 35
}

36
static void assert_appends(const git_signature *committer, const git_oid *oid)
Ben Straub committed
37
{
38
	git_repository *repo2;
39
	git_reference *lookedup_ref;
Ben Straub committed
40
	git_reflog *reflog;
41
	const git_reflog_entry *entry;
Ben Straub committed
42 43

	/* Reopen a new instance of the repository */
44
	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
Ben Straub committed
45

46
	/* Lookup the previously created branch */
Ben Straub committed
47 48 49
	cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref));

	/* Read and parse the reflog for this branch */
50
	cl_git_pass(git_reflog_read(&reflog, repo2, new_ref));
51 52 53 54 55
	cl_assert_equal_i(3, (int)git_reflog_entrycount(reflog));

	/* The first one was the creation of the branch */
	entry = git_reflog_entry_byindex(reflog, 2);
	cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
Ben Straub committed
56

57
	entry = git_reflog_entry_byindex(reflog, 1);
58
	assert_signature(committer, entry->committer);
59
	cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0);
60
	cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
Ben Straub committed
61 62
	cl_assert(entry->msg == NULL);

63
	entry = git_reflog_entry_byindex(reflog, 0);
64
	assert_signature(committer, entry->committer);
65
	cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
Vicent Martí committed
66
	cl_assert_equal_s(commit_msg, entry->msg);
Ben Straub committed
67 68 69 70 71 72 73

	git_reflog_free(reflog);
	git_repository_free(repo2);

	git_reference_free(lookedup_ref);
}

74 75 76 77 78 79 80 81 82 83
void test_refs_reflog_reflog__append_then_read(void)
{
	/* write a reflog for a given reference and ensure it can be read back */
	git_reference *ref;
	git_oid oid;
	git_signature *committer;
	git_reflog *reflog;

	/* Create a new branch pointing at the HEAD */
	git_oid_fromstr(&oid, current_master_tip);
84
	cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0, NULL));
85 86 87 88 89 90 91 92 93 94 95
	git_reference_free(ref);

	cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));

	cl_git_pass(git_reflog_read(&reflog, g_repo, new_ref));
	cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
	cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
	cl_git_pass(git_reflog_write(reflog));

	assert_appends(committer, &oid);

96
	git_reflog_free(reflog);
97 98 99
	git_signature_free(committer);
}

100
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
101
{
102
	git_reference *master, *new_master;
103 104 105 106 107 108 109 110 111 112 113
	git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;

	git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
	git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path));
	git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master");
	git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved");

	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path)));
	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path)));

	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
114
	cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0, NULL));
115
	git_reference_free(master);
116 117 118 119

	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path)));

120
	git_reference_free(new_master);
121 122
	git_buf_dispose(&moved_log_path);
	git_buf_dispose(&master_log_path);
123
}
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void test_refs_reflog_reflog__deleting_the_reference_deletes_the_reflog(void)
{
	git_reference *master;
	git_buf master_log_path = GIT_BUF_INIT;

	git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
	git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master");

	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path)));

	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
	cl_git_pass(git_reference_delete(master));
	git_reference_free(master);

	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path)));
140
	git_buf_dispose(&master_log_path);
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
void test_refs_reflog_reflog__removes_empty_reflog_dir(void)
{
	git_reference *ref;
	git_buf log_path = GIT_BUF_INIT;
	git_oid id;

	/* Create a new branch pointing at the HEAD */
	git_oid_fromstr(&id, current_master_tip);
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir/new-head", &id, 0, NULL));

	git_buf_joinpath(&log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
	git_buf_joinpath(&log_path, git_buf_cstr(&log_path), "refs/heads/new-dir/new-head");

	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path)));

	cl_git_pass(git_reference_delete(ref));
	git_reference_free(ref);

	/* new ref creation should succeed since new-dir is empty */
	git_oid_fromstr(&id, current_master_tip);
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir", &id, 0, NULL));
	git_reference_free(ref);

166
	git_buf_dispose(&log_path);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

void test_refs_reflog_reflog__fails_gracefully_on_nonempty_reflog_dir(void)
{
	git_reference *ref;
	git_buf log_path = GIT_BUF_INIT;
	git_oid id;

	/* Create a new branch pointing at the HEAD */
	git_oid_fromstr(&id, current_master_tip);
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir/new-head", &id, 0, NULL));
	git_reference_free(ref);

	git_buf_joinpath(&log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
	git_buf_joinpath(&log_path, git_buf_cstr(&log_path), "refs/heads/new-dir/new-head");

	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path)));

	/* delete the ref manually, leave the reflog */
	cl_must_pass(p_unlink("testrepo.git/refs/heads/new-dir/new-head"));

	/* new ref creation should fail since new-dir contains reflogs still */
	git_oid_fromstr(&id, current_master_tip);
	cl_git_fail_with(GIT_EDIRECTORY, git_reference_create(&ref, g_repo, "refs/heads/new-dir", &id, 0, NULL));
	git_reference_free(ref);

193
	git_buf_dispose(&log_path);
194 195
}

196 197
static void assert_has_reflog(bool expected_result, const char *name)
{
198
	cl_assert_equal_i(expected_result, git_reference_has_log(g_repo, name));
199 200
}

201
void test_refs_reflog_reflog__reference_has_reflog(void)
202 203 204 205 206
{
	assert_has_reflog(true, "HEAD");
	assert_has_reflog(true, "refs/heads/master");
	assert_has_reflog(false, "refs/heads/subtrees");
}
207 208 209 210

void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one(void)
{
	git_reflog *reflog;
211
	const char *refname = "refs/heads/subtrees";
212 213
	git_buf subtrees_log_path = GIT_BUF_INIT;

214
	git_buf_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, refname);
215 216
	cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&subtrees_log_path)));

217
	cl_git_pass(git_reflog_read(&reflog, g_repo, refname));
218

219
	cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog));
220 221

	git_reflog_free(reflog);
222
	git_buf_dispose(&subtrees_log_path);
223
}
224

225
void test_refs_reflog_reflog__reading_a_reflog_with_invalid_format_succeeds(void)
226 227 228 229 230
{
	git_reflog *reflog;
	const char *refname = "refs/heads/newline";
	const char *refmessage =
		"Reflog*message with a newline and enough content after it to pass the GIT_REFLOG_SIZE_MIN check inside reflog_parse.";
231
	const git_reflog_entry *entry;
232 233 234 235 236
	git_reference *ref;
	git_oid id;
	git_buf logpath = GIT_BUF_INIT, logcontents = GIT_BUF_INIT;
	char *star;

237 238
	/* Create a new branch. */
	cl_git_pass(git_oid_fromstr(&id, current_master_tip));
239 240
	cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, refmessage));

241 242 243 244 245
	/*
	 * Corrupt the branch reflog by introducing a newline inside the reflog message.
	 * We do this by replacing '*' with '\n'
	 */
	cl_git_pass(git_buf_join_n(&logpath, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, refname));
246 247 248 249 250
	cl_git_pass(git_futils_readbuffer(&logcontents, git_buf_cstr(&logpath)));
	cl_assert((star = strchr(git_buf_cstr(&logcontents), '*')) != NULL);
	*star = '\n';
	cl_git_rewritefile(git_buf_cstr(&logpath), git_buf_cstr(&logcontents));

251 252 253 254
	/*
	 * Confirm that the file was rewritten successfully
	 * and now contains a '\n' in the expected location
	 */
255 256 257
	cl_git_pass(git_futils_readbuffer(&logcontents, git_buf_cstr(&logpath)));
	cl_assert(strstr(git_buf_cstr(&logcontents), "Reflog\nmessage") != NULL);

258 259 260
	cl_git_pass(git_reflog_read(&reflog, g_repo, refname));
	cl_assert(entry = git_reflog_entry_byindex(reflog, 0));
	cl_assert_equal_s(git_reflog_entry_message(entry), "Reflog");
261 262

	git_reference_free(ref);
263
	git_reflog_free(reflog);
264 265
	git_buf_dispose(&logpath);
	git_buf_dispose(&logcontents);
266 267
}

268 269
void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
{
270
	git_reference *master, *new_master;
271 272 273 274
	git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT;
	git_reflog *reflog;

	cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
275
	cl_git_pass(git_reflog_read(&reflog, g_repo, "refs/heads/master"));
276 277

	cl_git_pass(git_reflog_write(reflog));
278

279
	cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0, NULL));
280
	git_reference_free(master);
281 282 283 284

	cl_git_fail(git_reflog_write(reflog));

	git_reflog_free(reflog);
285
	git_reference_free(new_master);
286 287
	git_buf_dispose(&moved_log_path);
	git_buf_dispose(&master_log_path);
288
}
289 290 291 292

void test_refs_reflog_reflog__renaming_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	cl_assert_equal_i(GIT_EINVALIDSPEC,
293
			  git_reflog_rename(g_repo, "refs/heads/master", "refs/heads/Inv@{id"));
294
}
295 296 297 298 299 300 301 302

void test_refs_reflog_reflog__write_only_std_locations(void)
{
	git_reference *ref;
	git_oid id;

	git_oid_fromstr(&id, current_master_tip);

303
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/foo", &id, 1, NULL));
304
	git_reference_free(ref);
305
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/tags/foo", &id, 1, NULL));
306
	git_reference_free(ref);
307
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/notes/foo", &id, 1, NULL));
308 309 310 311 312 313 314 315 316 317 318 319 320 321
	git_reference_free(ref);

	assert_has_reflog(true, "refs/heads/foo");
	assert_has_reflog(false, "refs/tags/foo");
	assert_has_reflog(true, "refs/notes/foo");

}

void test_refs_reflog_reflog__write_when_explicitly_active(void)
{
	git_reference *ref;
	git_oid id;

	git_oid_fromstr(&id, current_master_tip);
322
	git_reference_ensure_log(g_repo, "refs/tags/foo");
323

324
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/tags/foo", &id, 1, NULL));
325 326 327
	git_reference_free(ref);
	assert_has_reflog(true, "refs/tags/foo");
}
328 329 330 331 332 333 334 335 336 337 338 339 340 341

void test_refs_reflog_reflog__append_to_HEAD_when_changing_current_branch(void)
{
	size_t nlogs, nlogs_after;
	git_reference *ref;
	git_reflog *log;
	git_oid id;

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs = git_reflog_entrycount(log);
	git_reflog_free(log);

	/* Move it back */
	git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
342
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/master", &id, 1, NULL));
343 344 345 346 347 348 349 350
	git_reference_free(ref);

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nlogs_after, nlogs + 1);
}
351 352 353 354 355 356 357 358 359 360 361 362 363

void test_refs_reflog_reflog__do_not_append_when_no_update(void)
{
	size_t nlogs, nlogs_after;
	git_reference *ref, *ref2;
	git_reflog *log;

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
	cl_git_pass(git_reference_create(&ref2, g_repo, "refs/heads/master",
364
					 git_reference_target(ref), 1, NULL));
365 366 367 368 369 370 371 372 373 374

	git_reference_free(ref);
	git_reference_free(ref2);

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nlogs_after, nlogs);
}
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

static void assert_no_reflog_update(void)
{
	size_t nlogs, nlogs_after;
	size_t nlogs_master, nlogs_master_after;
	git_reference *ref;
	git_reflog *log;
	git_oid id;

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
	nlogs_master = git_reflog_entrycount(log);
	git_reflog_free(log);

	/* Move it back */
	git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
394
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/master", &id, 1, NULL));
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	git_reference_free(ref);

	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
	nlogs_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nlogs_after, nlogs);

	cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
	nlogs_master_after = git_reflog_entrycount(log);
	git_reflog_free(log);

	cl_assert_equal_i(nlogs_after, nlogs);
	cl_assert_equal_i(nlogs_master_after, nlogs_master);

}

void test_refs_reflog_reflog__logallrefupdates_bare_set_false(void)
{
	git_config *config;

	cl_git_pass(git_repository_config(&config, g_repo));
	cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
	git_config_free(config);

	assert_no_reflog_update();
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
void test_refs_reflog_reflog__logallrefupdates_bare_set_always(void)
{
	git_config *config;
	git_reference *ref;
	git_reflog *log;
	git_oid id;

	cl_git_pass(git_repository_config(&config, g_repo));
	cl_git_pass(git_config_set_string(config, "core.logallrefupdates", "always"));
	git_config_free(config);

	git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	cl_git_pass(git_reference_create(&ref, g_repo, "refs/bork", &id, 1, "message"));

	cl_git_pass(git_reflog_read(&log, g_repo, "refs/bork"));
	cl_assert_equal_i(1, git_reflog_entrycount(log));
	cl_assert_equal_s("message", git_reflog_entry_byindex(log, 0)->msg);

	git_reflog_free(log);
	git_reference_free(ref);
}

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
void test_refs_reflog_reflog__logallrefupdates_bare_unset(void)
{
	git_config *config;

	cl_git_pass(git_repository_config(&config, g_repo));
	cl_git_pass(git_config_delete_entry(config, "core.logallrefupdates"));
	git_config_free(config);

	assert_no_reflog_update();
}

void test_refs_reflog_reflog__logallrefupdates_nonbare_set_false(void)
{
	git_config *config;

	cl_git_sandbox_cleanup();
	g_repo = cl_git_sandbox_init("testrepo");


	cl_git_pass(git_repository_config(&config, g_repo));
	cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
	git_config_free(config);

	assert_no_reflog_update();
}