push.c 26.5 KB
Newer Older
1 2 3 4 5 6
#include "clar_libgit2.h"
#include "buffer.h"
#include "posix.h"
#include "vector.h"
#include "../submodule/submodule_helpers.h"
#include "push_util.h"
7 8
#include "refspec.h"
#include "remote.h"
9 10 11

static git_repository *_repo;

12 13
static char *_remote_url;

14 15 16 17
static char *_remote_ssh_key;
static char *_remote_ssh_pubkey;
static char *_remote_ssh_passphrase;

18 19 20
static char *_remote_user;
static char *_remote_pass;

21 22
static char *_remote_default;

23 24
static int cred_acquire_cb(git_cred **,	const char *, const char *, unsigned int, void *);

25 26 27 28 29 30 31 32 33 34 35
static git_remote *_remote;
static record_callbacks_data _record_cbs_data = {{ 0 }};
static git_remote_callbacks _record_cbs = RECORD_CALLBACKS_INIT(&_record_cbs_data);

static git_oid _oid_b6;
static git_oid _oid_b5;
static git_oid _oid_b4;
static git_oid _oid_b3;
static git_oid _oid_b2;
static git_oid _oid_b1;

36 37 38 39
static git_oid _tag_commit;
static git_oid _tag_tree;
static git_oid _tag_blob;
static git_oid _tag_lightweight;
40
static git_oid _tag_tag;
41

42
static int cred_acquire_cb(
43 44 45 46 47
	git_cred **cred,
	const char *url,
	const char *user_from_url,
	unsigned int allowed_types,
	void *payload)
48 49
{
	GIT_UNUSED(url);
50
	GIT_UNUSED(user_from_url);
51
	GIT_UNUSED(payload);
52

53 54 55 56 57 58 59 60 61
	if (GIT_CREDTYPE_DEFAULT & allowed_types) {
		if (!_remote_default) {
			printf("GITTEST_REMOTE_DEFAULT must be set to use NTLM/Negotiate credentials\n");
			return -1;
		}

		return git_cred_default_new(cred);
	}

62
	if (GIT_CREDTYPE_SSH_KEY & allowed_types) {
63 64 65 66
		if (!_remote_user || !_remote_ssh_pubkey || !_remote_ssh_key || !_remote_ssh_passphrase) {
			printf("GITTEST_REMOTE_USER, GITTEST_REMOTE_SSH_PUBKEY, GITTEST_REMOTE_SSH_KEY and GITTEST_REMOTE_SSH_PASSPHRASE must be set\n");
			return -1;
		}
67

68
		return git_cred_ssh_key_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase);
69
	}
70

71 72 73 74 75
	if (GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) {
		if (!_remote_user || !_remote_pass) {
			printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_PASS must be set\n");
			return -1;
		}
76

77 78 79 80
		return git_cred_userpass_plaintext_new(cred, _remote_user, _remote_pass);
	}

	return -1;
81 82
}

83 84
/* the results of a push status.  when used for expected values, msg may be NULL
 * to indicate that it should not be matched. */
85 86
typedef struct {
	const char *ref;
87
	int success;
88 89 90 91 92 93 94 95 96 97 98 99 100 101
	const char *msg;
} push_status;

/**
 * git_push_status_foreach callback that records status entries.
 * @param data (git_vector *) of push_status instances
 */
static int record_push_status_cb(const char *ref, const char *msg, void *data)
{
	git_vector *statuses = (git_vector *)data;
	push_status *s;

	cl_assert(s = git__malloc(sizeof(*s)));
	s->ref = ref;
102
	s->success = (msg == NULL);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	s->msg = msg;

	git_vector_insert(statuses, s);

	return 0;
}

static void do_verify_push_status(git_push *push, const push_status expected[], const size_t expected_len)
{
	git_vector actual = GIT_VECTOR_INIT;
	push_status *iter;
	bool failed = false;
	size_t i;

	git_push_status_foreach(push, record_push_status_cb, &actual);

	if (expected_len != actual.length)
		failed = true;
	else
		git_vector_foreach(&actual, i, iter)
			if (strcmp(expected[i].ref, iter->ref) ||
124 125
				(expected[i].success != iter->success) ||
				(expected[i].msg && (!iter->msg || strcmp(expected[i].msg, iter->msg)))) {
126 127 128 129 130 131 132 133 134 135 136 137
				failed = true;
				break;
			}

	if (failed) {
		git_buf msg = GIT_BUF_INIT;

		git_buf_puts(&msg, "Expected and actual push statuses differ:\nEXPECTED:\n");

		for(i = 0; i < expected_len; i++) {
			git_buf_printf(&msg, "%s: %s\n",
				expected[i].ref,
138
				expected[i].success ? "success" : "failed");
139 140 141 142
		}

		git_buf_puts(&msg, "\nACTUAL:\n");

143 144 145 146 147 148
		git_vector_foreach(&actual, i, iter) {
			if (iter->success)
				git_buf_printf(&msg, "%s: success\n", iter->ref);
			else
				git_buf_printf(&msg, "%s: failed with message: %s", iter->ref, iter->msg);
		}
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

		cl_fail(git_buf_cstr(&msg));

		git_buf_free(&msg);
	}

	git_vector_foreach(&actual, i, iter)
		git__free(iter);

	git_vector_free(&actual);
}

/**
 * Verifies that after git_push_finish(), refs on a remote have the expected
 * names, oids, and order.
164
 *
165 166 167 168 169 170
 * @param remote remote to verify
 * @param expected_refs expected remote refs after push
 * @param expected_refs_len length of expected_refs
 */
static void verify_refs(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
171 172
	const git_remote_head **actual_refs;
	size_t actual_refs_len;
173

174 175
	git_remote_ls(&actual_refs, &actual_refs_len, remote);
	verify_remote_refs(actual_refs, actual_refs_len, expected_refs, expected_refs_len);
176 177
}

178 179 180 181 182 183 184 185 186 187
/**
 * Verifies that after git_push_update_tips(), remote tracking branches have the expected
 * names and oids.
 *
 * @param remote remote to verify
 * @param expected_refs expected remote refs after push
 * @param expected_refs_len length of expected_refs
 */
static void verify_tracking_branches(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
188
	git_refspec *fetch_spec;
189 190 191 192
	size_t i, j;
	git_buf msg = GIT_BUF_INIT;
	git_buf ref_name = GIT_BUF_INIT;
	git_vector actual_refs = GIT_VECTOR_INIT;
193
	git_branch_iterator *iter;
194 195
	char *actual_ref;
	git_oid oid;
196
	int failed = 0, error;
197
	git_branch_t branch_type;
198
	git_reference *ref;
199 200

	/* Get current remote branches */
201 202 203 204 205 206 207 208 209
	cl_git_pass(git_branch_iterator_new(&iter, remote->repo, GIT_BRANCH_REMOTE));

	while ((error = git_branch_next(&ref, &branch_type, iter)) == 0) {
		cl_assert_equal_i(branch_type, GIT_BRANCH_REMOTE);

		cl_git_pass(git_vector_insert(&actual_refs, git__strdup(git_reference_name(ref))));
	}

	cl_assert_equal_i(error, GIT_ITEROVER);
210
	git_branch_iterator_free(iter);
211 212 213 214 215 216 217

	/* Loop through expected refs, make sure they exist */
	for (i = 0; i < expected_refs_len; i++) {

		/* Convert remote reference name into tracking branch name.
		 * If the spec is not under refs/heads/, then skip.
		 */
218 219
		fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
		if (!fetch_spec)
220 221
			continue;

222
		cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
223 224 225

		/* Find matching remote branch */
		git_vector_foreach(&actual_refs, j, actual_ref) {
226
			if (!strcmp(git_buf_cstr(&ref_name), actual_ref))
227 228 229 230 231 232 233 234 235 236
				break;
		}

		if (j == actual_refs.length) {
			git_buf_printf(&msg, "Did not find expected tracking branch '%s'.", git_buf_cstr(&ref_name));
			failed = 1;
			goto failed;
		}

		/* Make sure tracking branch is at expected commit ID */
237
		cl_git_pass(git_reference_name_to_id(&oid, remote->repo, actual_ref));
238 239 240 241 242 243 244

		if (git_oid_cmp(expected_refs[i].oid, &oid) != 0) {
			git_buf_puts(&msg, "Tracking branch commit does not match expected ID.");
			failed = 1;
			goto failed;
		}

245
		git__free(actual_ref);
246 247 248 249 250 251 252 253 254 255 256
		cl_git_pass(git_vector_remove(&actual_refs, j));
	}

	/* Make sure there are no extra branches */
	if (actual_refs.length > 0) {
		git_buf_puts(&msg, "Unexpected remote tracking branches exist.");
		failed = 1;
		goto failed;
	}

failed:
257
	if (failed)
258 259 260 261 262 263 264
		cl_fail(git_buf_cstr(&msg));

	git_vector_foreach(&actual_refs, i, actual_ref)
		git__free(actual_ref);

	git_vector_free(&actual_refs);
	git_buf_free(&msg);
265
	git_buf_free(&ref_name);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}

static void verify_update_tips_callback(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
	git_refspec *fetch_spec;
	git_buf msg = GIT_BUF_INIT;
	git_buf ref_name = GIT_BUF_INIT;
	updated_tip *tip = NULL;
	size_t i, j;
	int failed = 0;

	for (i = 0; i < expected_refs_len; ++i) {
		/* Convert remote reference name into tracking branch name.
		 * If the spec is not under refs/heads/, then skip.
		 */
		fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
		if (!fetch_spec)
			continue;

		cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));

		/* Find matching update_tip entry */
		git_vector_foreach(&_record_cbs_data.updated_tips, j, tip) {
			if (!strcmp(git_buf_cstr(&ref_name), tip->name))
				break;
		}

		if (j == _record_cbs_data.updated_tips.length) {
			git_buf_printf(&msg, "Did not find expected updated tip entry for branch '%s'.", git_buf_cstr(&ref_name));
			failed = 1;
			goto failed;
		}

		if (git_oid_cmp(expected_refs[i].oid, tip->new_oid) != 0) {
			git_buf_printf(&msg, "Updated tip ID does not match expected ID");
			failed = 1;
			goto failed;
		}
	}

failed:
	if (failed)
		cl_fail(git_buf_cstr(&msg));

	git_buf_free(&ref_name);
	git_buf_free(&msg);
312 313
}

314
void test_online_push__initialize(void)
315 316
{
	git_vector delete_specs = GIT_VECTOR_INIT;
317 318
	const git_remote_head **heads;
	size_t i, heads_len;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	char *curr_del_spec;

	_repo = cl_git_sandbox_init("push_src");

	cl_fixture_sandbox("testrepo.git");
	cl_rename("push_src/submodule/.gitted", "push_src/submodule/.git");

	rewrite_gitmodules(git_repository_workdir(_repo));

	/* git log --format=oneline --decorate --graph
	 * *-.   951bbbb90e2259a4c8950db78946784fb53fcbce (HEAD, b6) merge b3, b4, and b5 to b6
	 * |\ \
	 * | | * fa38b91f199934685819bea316186d8b008c52a2 (b5) added submodule named 'submodule' pointing to '../testrepo.git'
	 * | * | 27b7ce66243eb1403862d05f958c002312df173d (b4) edited fold\b.txt
	 * | |/
	 * * | d9b63a88223d8367516f50bd131a5f7349b7f3e4 (b3) edited a.txt
	 * |/
	 * * a78705c3b2725f931d3ee05348d83cc26700f247 (b2, b1) added fold and fold/b.txt
	 * * 5c0bb3d1b9449d1cc69d7519fd05166f01840915 added a.txt
	 */
	git_oid_fromstr(&_oid_b6, "951bbbb90e2259a4c8950db78946784fb53fcbce");
	git_oid_fromstr(&_oid_b5, "fa38b91f199934685819bea316186d8b008c52a2");
	git_oid_fromstr(&_oid_b4, "27b7ce66243eb1403862d05f958c002312df173d");
	git_oid_fromstr(&_oid_b3, "d9b63a88223d8367516f50bd131a5f7349b7f3e4");
	git_oid_fromstr(&_oid_b2, "a78705c3b2725f931d3ee05348d83cc26700f247");
	git_oid_fromstr(&_oid_b1, "a78705c3b2725f931d3ee05348d83cc26700f247");

346 347 348 349
	git_oid_fromstr(&_tag_commit, "805c54522e614f29f70d2413a0470247d8b424ac");
	git_oid_fromstr(&_tag_tree, "ff83aa4c5e5d28e3bcba2f5c6e2adc61286a4e5e");
	git_oid_fromstr(&_tag_blob, "b483ae7ba66decee9aee971f501221dea84b1498");
	git_oid_fromstr(&_tag_lightweight, "951bbbb90e2259a4c8950db78946784fb53fcbce");
350
	git_oid_fromstr(&_tag_tag, "eea4f2705eeec2db3813f2430829afce99cd00b5");
351

352 353 354 355
	/* Remote URL environment variable must be set.  User and password are optional.  */
	_remote_url = cl_getenv("GITTEST_REMOTE_URL");
	_remote_user = cl_getenv("GITTEST_REMOTE_USER");
	_remote_pass = cl_getenv("GITTEST_REMOTE_PASS");
356 357 358
	_remote_ssh_key = cl_getenv("GITTEST_REMOTE_SSH_KEY");
	_remote_ssh_pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
	_remote_ssh_passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
359
	_remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT");
360 361
	_remote = NULL;

Vicent Marti committed
362 363 364
	/* Skip the test if we're missing the remote URL */
	if (!_remote_url)
		cl_skip();
365

Vicent Marti committed
366
	cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url));
367

Vicent Marti committed
368 369
	record_callbacks_data_clear(&_record_cbs_data);
	git_remote_set_callbacks(_remote, &_record_cbs);
370

Vicent Marti committed
371
	cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH));
372

Vicent Marti committed
373 374 375 376 377 378 379 380 381 382
	/* Clean up previously pushed branches.  Fails if receive.denyDeletes is
	 * set on the remote.  Also, on Git 1.7.0 and newer, you must run
	 * 'git config receive.denyDeleteCurrent ignore' in the remote repo in
	 * order to delete the remote branch pointed to by HEAD (usually master).
	 * See: https://raw.github.com/git/git/master/Documentation/RelNotes/1.7.0.txt
	 */
	cl_git_pass(git_remote_ls(&heads, &heads_len, _remote));
	cl_git_pass(create_deletion_refspecs(&delete_specs, heads, heads_len));
	if (delete_specs.length) {
		git_push *push;
383

Vicent Marti committed
384
		cl_git_pass(git_push_new(&push, _remote));
385

Vicent Marti committed
386 387 388
		git_vector_foreach(&delete_specs, i, curr_del_spec) {
			git_push_add_refspec(push, curr_del_spec);
			git__free(curr_del_spec);
389 390
		}

Vicent Marti committed
391 392 393 394 395 396
		cl_git_pass(git_push_finish(push));
		git_push_free(push);
	}

	git_remote_disconnect(_remote);
	git_vector_free(&delete_specs);
397

Vicent Marti committed
398 399 400 401 402
	/* Now that we've deleted everything, fetch from the remote */
	cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH));
	cl_git_pass(git_remote_download(_remote));
	cl_git_pass(git_remote_update_tips(_remote, NULL, NULL));
	git_remote_disconnect(_remote);
403 404
}

405
void test_online_push__cleanup(void)
406 407 408
{
	if (_remote)
		git_remote_free(_remote);
409 410 411 412
	_remote = NULL;

	/* Freed by cl_git_sandbox_cleanup */
	_repo = NULL;
413 414 415 416 417 418 419

	record_callbacks_data_clear(&_record_cbs_data);

	cl_fixture_cleanup("testrepo.git");
	cl_git_sandbox_cleanup();
}

420 421
static int push_pack_progress_cb(
	int stage, unsigned int current, unsigned int total, void* payload)
422
{
423
	int *calls = (int *)payload;
424
	GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
425 426 427
	if (*calls < 0)
		return *calls;
	(*calls)++;
428
	return 0;
429 430
}

431 432
static int push_transfer_progress_cb(
	unsigned int current, unsigned int total, size_t bytes, void* payload)
433
{
434
	int *calls = (int *)payload;
435
	GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
436 437 438
	if (*calls < 0)
		return *calls;
	(*calls)++;
439
	return 0;
440 441
}

442 443
/**
 * Calls push and relists refs on remote to verify success.
444
 *
445 446 447 448 449
 * @param refspecs refspecs to push
 * @param refspecs_len length of refspecs
 * @param expected_refs expected remote refs after push
 * @param expected_refs_len length of expected_refs
 * @param expected_ret expected return value from git_push_finish()
450
 * @param check_progress_cb Check that the push progress callbacks are called
451
 */
452 453
static void do_push(
	const char *refspecs[], size_t refspecs_len,
454
	push_status expected_statuses[], size_t expected_statuses_len,
455
	expected_ref expected_refs[], size_t expected_refs_len,
456
	int expected_ret, int check_progress_cb, int check_update_tips_cb)
457 458
{
	git_push *push;
459
	git_push_options opts = GIT_PUSH_OPTIONS_INIT;
460
	size_t i;
461
	int pack_progress_calls = 0, transfer_progress_calls = 0;
462
	git_signature *pusher;
463 464

	if (_remote) {
465 466 467
		/* Auto-detect the number of threads to use */
		opts.pb_parallelism = 0;

468
		cl_git_pass(git_signature_now(&pusher, "Foo Bar", "foo@example.com"));
469 470 471
		cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH));

		cl_git_pass(git_push_new(&push, _remote));
472
		cl_git_pass(git_push_set_options(push, &opts));
473

474 475 476 477 478 479 480 481 482 483
		if (check_progress_cb) {
			/* if EUSER, then abort in transfer */
			if (expected_ret == GIT_EUSER)
				transfer_progress_calls = GIT_EUSER;

			cl_git_pass(
				git_push_set_callbacks(
					push, push_pack_progress_cb, &pack_progress_calls,
					push_transfer_progress_cb, &transfer_progress_calls));
		}
484

485 486 487 488
		for (i = 0; i < refspecs_len; i++)
			cl_git_pass(git_push_add_refspec(push, refspecs[i]));

		if (expected_ret < 0) {
489
			cl_git_fail_with(git_push_finish(push), expected_ret);
490
			cl_assert_equal_i(0, git_push_unpack_ok(push));
491 492
		} else {
			cl_git_pass(git_push_finish(push));
493 494 495
			cl_assert_equal_i(1, git_push_unpack_ok(push));
		}

496 497 498
		if (check_progress_cb && !expected_ret) {
			cl_assert(pack_progress_calls > 0);
			cl_assert(transfer_progress_calls > 0);
499 500
		}

501 502 503 504
		do_verify_push_status(push, expected_statuses, expected_statuses_len);

		verify_refs(_remote, expected_refs, expected_refs_len);

505
		cl_git_pass(git_push_update_tips(push, pusher, "test push"));
506 507
		verify_tracking_branches(_remote, expected_refs, expected_refs_len);

508 509 510
		if (check_update_tips_cb)
			verify_update_tips_callback(_remote, expected_refs, expected_refs_len);

511
		git_push_free(push);
512 513

		git_remote_disconnect(_remote);
514
		git_signature_free(pusher);
515
	}
516

517 518 519
}

/* Call push_finish() without ever calling git_push_add_refspec() */
520
void test_online_push__noop(void)
521
{
522
	do_push(NULL, 0, NULL, 0, NULL, 0, 0, 0, 1);
523 524
}

525
void test_online_push__b1(void)
526 527
{
	const char *specs[] = { "refs/heads/b1:refs/heads/b1" };
528
	push_status exp_stats[] = { { "refs/heads/b1", 1 } };
529 530 531
	expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
532
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
533 534
}

535
void test_online_push__b2(void)
536 537
{
	const char *specs[] = { "refs/heads/b2:refs/heads/b2" };
538
	push_status exp_stats[] = { { "refs/heads/b2", 1 } };
539 540 541
	expected_ref exp_refs[] = { { "refs/heads/b2", &_oid_b2 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
542
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
543 544
}

545
void test_online_push__b3(void)
546 547
{
	const char *specs[] = { "refs/heads/b3:refs/heads/b3" };
548
	push_status exp_stats[] = { { "refs/heads/b3", 1 } };
549 550 551
	expected_ref exp_refs[] = { { "refs/heads/b3", &_oid_b3 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
552
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
553 554
}

555
void test_online_push__b4(void)
556 557
{
	const char *specs[] = { "refs/heads/b4:refs/heads/b4" };
558
	push_status exp_stats[] = { { "refs/heads/b4", 1 } };
559 560 561
	expected_ref exp_refs[] = { { "refs/heads/b4", &_oid_b4 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
562
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
563 564
}

565
void test_online_push__b5(void)
566 567
{
	const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
568
	push_status exp_stats[] = { { "refs/heads/b5", 1 } };
569 570 571
	expected_ref exp_refs[] = { { "refs/heads/b5", &_oid_b5 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
572
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
573 574
}

575 576 577
void test_online_push__b5_cancel(void)
{
	const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
578
	do_push(specs, ARRAY_SIZE(specs), NULL, 0, NULL, 0, GIT_EUSER, 1, 1);
579 580
}

581
void test_online_push__multi(void)
582
{
583 584 585
	git_reflog *log;
	const git_reflog_entry *entry;

586 587 588 589 590 591 592 593
	const char *specs[] = {
		"refs/heads/b1:refs/heads/b1",
		"refs/heads/b2:refs/heads/b2",
		"refs/heads/b3:refs/heads/b3",
		"refs/heads/b4:refs/heads/b4",
		"refs/heads/b5:refs/heads/b5"
	};
	push_status exp_stats[] = {
594 595 596 597 598
		{ "refs/heads/b1", 1 },
		{ "refs/heads/b2", 1 },
		{ "refs/heads/b3", 1 },
		{ "refs/heads/b4", 1 },
		{ "refs/heads/b5", 1 }
599 600 601 602 603 604 605 606 607 608
	};
	expected_ref exp_refs[] = {
		{ "refs/heads/b1", &_oid_b1 },
		{ "refs/heads/b2", &_oid_b2 },
		{ "refs/heads/b3", &_oid_b3 },
		{ "refs/heads/b4", &_oid_b4 },
		{ "refs/heads/b5", &_oid_b5 }
	};
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
609
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
610 611 612

	cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1"));
	entry = git_reflog_entry_byindex(log, 0);
613 614 615 616
	if (entry) {
		cl_assert_equal_s("test push", git_reflog_entry_message(entry));
		cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
	}
617 618

	git_reflog_free(log);
619 620
}

621
void test_online_push__implicit_tgt(void)
622 623
{
	const char *specs1[] = { "refs/heads/b1:" };
624
	push_status exp_stats1[] = { { "refs/heads/b1", 1 } };
625 626 627
	expected_ref exp_refs1[] = { { "refs/heads/b1", &_oid_b1 } };

	const char *specs2[] = { "refs/heads/b2:" };
628
	push_status exp_stats2[] = { { "refs/heads/b2", 1 } };
629 630 631 632 633 634 635
	expected_ref exp_refs2[] = {
	{ "refs/heads/b1", &_oid_b1 },
	{ "refs/heads/b2", &_oid_b2 }
	};

	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
636
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
637 638
	do_push(specs2, ARRAY_SIZE(specs2),
		exp_stats2, ARRAY_SIZE(exp_stats2),
639
		exp_refs2, ARRAY_SIZE(exp_refs2), 0, 0, 0);
640 641
}

642
void test_online_push__fast_fwd(void)
643 644 645 646
{
	/* Fast forward b1 in tgt from _oid_b1 to _oid_b6. */

	const char *specs_init[] = { "refs/heads/b1:refs/heads/b1" };
647
	push_status exp_stats_init[] = { { "refs/heads/b1", 1 } };
648 649 650
	expected_ref exp_refs_init[] = { { "refs/heads/b1", &_oid_b1 } };

	const char *specs_ff[] = { "refs/heads/b6:refs/heads/b1" };
651
	push_status exp_stats_ff[] = { { "refs/heads/b1", 1 } };
652 653 654 655 656 657 658 659 660
	expected_ref exp_refs_ff[] = { { "refs/heads/b1", &_oid_b6 } };

	/* Do a force push to reset b1 in target back to _oid_b1 */
	const char *specs_reset[] = { "+refs/heads/b1:refs/heads/b1" };
	/* Force should have no effect on a fast forward push */
	const char *specs_ff_force[] = { "+refs/heads/b6:refs/heads/b1" };

	do_push(specs_init, ARRAY_SIZE(specs_init),
		exp_stats_init, ARRAY_SIZE(exp_stats_init),
661
		exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 1, 1);
662 663 664

	do_push(specs_ff, ARRAY_SIZE(specs_ff),
		exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
665
		exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
666 667 668

	do_push(specs_reset, ARRAY_SIZE(specs_reset),
		exp_stats_init, ARRAY_SIZE(exp_stats_init),
669
		exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 0, 0);
670 671 672

	do_push(specs_ff_force, ARRAY_SIZE(specs_ff_force),
		exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
673
		exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
674 675
}

676 677 678
void test_online_push__tag_commit(void)
{
	const char *specs[] = { "refs/tags/tag-commit:refs/tags/tag-commit" };
679
	push_status exp_stats[] = { { "refs/tags/tag-commit", 1 } };
680 681 682
	expected_ref exp_refs[] = { { "refs/tags/tag-commit", &_tag_commit } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
683
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
684 685 686 687 688
}

void test_online_push__tag_tree(void)
{
	const char *specs[] = { "refs/tags/tag-tree:refs/tags/tag-tree" };
689
	push_status exp_stats[] = { { "refs/tags/tag-tree", 1 } };
690 691 692
	expected_ref exp_refs[] = { { "refs/tags/tag-tree", &_tag_tree } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
693
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
694 695 696 697 698
}

void test_online_push__tag_blob(void)
{
	const char *specs[] = { "refs/tags/tag-blob:refs/tags/tag-blob" };
699
	push_status exp_stats[] = { { "refs/tags/tag-blob", 1 } };
700 701 702
	expected_ref exp_refs[] = { { "refs/tags/tag-blob", &_tag_blob } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
703
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
704 705 706 707 708
}

void test_online_push__tag_lightweight(void)
{
	const char *specs[] = { "refs/tags/tag-lightweight:refs/tags/tag-lightweight" };
709
	push_status exp_stats[] = { { "refs/tags/tag-lightweight", 1 } };
710 711 712
	expected_ref exp_refs[] = { { "refs/tags/tag-lightweight", &_tag_lightweight } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
713
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
714 715
}

716 717 718
void test_online_push__tag_to_tag(void)
{
	const char *specs[] = { "refs/tags/tag-tag:refs/tags/tag-tag" };
719
	push_status exp_stats[] = { { "refs/tags/tag-tag", 1 } };
720 721 722
	expected_ref exp_refs[] = { { "refs/tags/tag-tag", &_tag_tag } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
723
		exp_refs, ARRAY_SIZE(exp_refs), 0, 0, 0);
724 725
}

726
void test_online_push__force(void)
727 728
{
	const char *specs1[] = {"refs/heads/b3:refs/heads/tgt"};
729
	push_status exp_stats1[] = { { "refs/heads/tgt", 1 } };
730 731 732 733 734
	expected_ref exp_refs1[] = { { "refs/heads/tgt", &_oid_b3 } };

	const char *specs2[] = {"refs/heads/b4:refs/heads/tgt"};

	const char *specs2_force[] = {"+refs/heads/b4:refs/heads/tgt"};
735
	push_status exp_stats2_force[] = { { "refs/heads/tgt", 1 } };
736 737 738 739
	expected_ref exp_refs2_force[] = { { "refs/heads/tgt", &_oid_b4 } };

	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
740
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
741 742 743

	do_push(specs2, ARRAY_SIZE(specs2),
		NULL, 0,
744
		exp_refs1, ARRAY_SIZE(exp_refs1), GIT_ENONFASTFORWARD, 0, 0);
745 746

	/* Non-fast-forward update with force should pass. */
747
	record_callbacks_data_clear(&_record_cbs_data);
748 749
	do_push(specs2_force, ARRAY_SIZE(specs2_force),
		exp_stats2_force, ARRAY_SIZE(exp_stats2_force),
750
		exp_refs2_force, ARRAY_SIZE(exp_refs2_force), 0, 1, 1);
751 752
}

753
void test_online_push__delete(void)
754 755 756 757 758 759
{
	const char *specs1[] = {
		"refs/heads/b1:refs/heads/tgt1",
		"refs/heads/b1:refs/heads/tgt2"
	};
	push_status exp_stats1[] = {
760 761
		{ "refs/heads/tgt1", 1 },
		{ "refs/heads/tgt2", 1 }
762 763 764 765 766 767 768 769 770
	};
	expected_ref exp_refs1[] = {
		{ "refs/heads/tgt1", &_oid_b1 },
		{ "refs/heads/tgt2", &_oid_b1 }
	};

	const char *specs_del_fake[] = { ":refs/heads/fake" };
	/* Force has no effect for delete. */
	const char *specs_del_fake_force[] = { "+:refs/heads/fake" };
771
	push_status exp_stats_fake[] = { { "refs/heads/fake", 1 } };
772 773

	const char *specs_delete[] = { ":refs/heads/tgt1" };
774
	push_status exp_stats_delete[] = { { "refs/heads/tgt1", 1 } };
775 776 777 778 779 780
	expected_ref exp_refs_delete[] = { { "refs/heads/tgt2", &_oid_b1 } };
	/* Force has no effect for delete. */
	const char *specs_delete_force[] = { "+:refs/heads/tgt1" };

	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
781
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
782

783 784 785 786 787
	/* When deleting a non-existent branch, the git client sends zero for both
	 * the old and new commit id.  This should succeed on the server with the
	 * same status report as if the branch were actually deleted.  The server
	 * returns a warning on the side-band iff the side-band is supported.
	 *  Since libgit2 doesn't support the side-band yet, there are no warnings.
788 789
	 */
	do_push(specs_del_fake, ARRAY_SIZE(specs_del_fake),
790
		exp_stats_fake, 1,
791
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
792
	do_push(specs_del_fake_force, ARRAY_SIZE(specs_del_fake_force),
793
		exp_stats_fake, 1,
794
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
795 796 797 798

	/* Delete one of the pushed branches. */
	do_push(specs_delete, ARRAY_SIZE(specs_delete),
		exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
799
		exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
800 801 802 803

	/* Re-push branches and retry delete with force. */
	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
804
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
805 806
	do_push(specs_delete_force, ARRAY_SIZE(specs_delete_force),
		exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
807
		exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
808 809
}

810
void test_online_push__bad_refspecs(void)
811 812 813 814 815 816 817
{
	/* All classes of refspecs that should be rejected by
	 * git_push_add_refspec() should go in this test.
	 */
	git_push *push;

	if (_remote) {
818
/*		cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH)); */
819 820 821 822 823 824 825 826 827
		cl_git_pass(git_push_new(&push, _remote));

		/* Unexpanded branch names not supported */
		cl_git_fail(git_push_add_refspec(push, "b6:b6"));

		git_push_free(push);
	}
}

828
void test_online_push__expressions(void)
829 830 831 832
{
	/* TODO: Expressions in refspecs doesn't actually work yet */
	const char *specs_left_expr[] = { "refs/heads/b2~1:refs/heads/b2" };

833 834
	/* expect not NULL to indicate failure (core git replies "funny refname",
	 * other servers may be less pithy. */
835
	const char *specs_right_expr[] = { "refs/heads/b2:refs/heads/b2~1" };
836
	push_status exp_stats_right_expr[] = { { "refs/heads/b2~1", 0 } };
837 838 839 840

	/* TODO: Find a more precise way of checking errors than a exit code of -1. */
	do_push(specs_left_expr, ARRAY_SIZE(specs_left_expr),
		NULL, 0,
841
		NULL, 0, -1, 0, 0);
842 843 844

	do_push(specs_right_expr, ARRAY_SIZE(specs_right_expr),
		exp_stats_right_expr, ARRAY_SIZE(exp_stats_right_expr),
845
		NULL, 0, 0, 1, 1);
846
}
847

848
void test_online_push__notes(void)
849 850 851 852
{
	git_oid note_oid, *target_oid, expected_oid;
	git_signature *signature;
	const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
853
	push_status exp_stats[] = { { "refs/notes/commits", 1 } };
854 855 856 857 858 859 860
	expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
	git_oid_fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb");

	target_oid = &_oid_b6;

	/* Create note to push */
	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
861
	cl_git_pass(git_note_create(&note_oid, _repo, signature, signature, NULL, target_oid, "hello world\n", 0));
862 863 864

	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
865
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
866 867 868

	git_signature_free(signature);
}