push.c 28.2 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
static char *_remote_url = NULL;
13

14 15
static char *_remote_user = NULL;
static char *_remote_pass = NULL;
16

17 18 19
static char *_remote_ssh_key = NULL;
static char *_remote_ssh_pubkey = NULL;
static char *_remote_ssh_passphrase = NULL;
20

21
static char *_remote_default = NULL;
22
static char *_remote_expectcontinue = NULL;
23

24
static int cred_acquire_cb(git_credential **,	const char *, const char *, unsigned int, void *);
25

26 27 28 29 30 31 32 33 34 35 36
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;

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

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

54
	if (GIT_CREDENTIAL_USERNAME & allowed_types) {
55 56 57 58 59
		if (!_remote_user) {
			printf("GITTEST_REMOTE_USER must be set\n");
			return -1;
		}

60
		return git_credential_username_new(cred, _remote_user);
61 62
	}

63
	if (GIT_CREDENTIAL_DEFAULT & allowed_types) {
64 65 66 67 68
		if (!_remote_default) {
			printf("GITTEST_REMOTE_DEFAULT must be set to use NTLM/Negotiate credentials\n");
			return -1;
		}

69
		return git_credential_default_new(cred);
70 71
	}

72
	if (GIT_CREDENTIAL_SSH_KEY & allowed_types) {
73 74 75 76
		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;
		}
77

78
		return git_credential_ssh_key_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase);
79
	}
80

81
	if (GIT_CREDENTIAL_USERPASS_PLAINTEXT & allowed_types) {
82 83 84 85
		if (!_remote_user || !_remote_pass) {
			printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_PASS must be set\n");
			return -1;
		}
86

87
		return git_credential_userpass_plaintext_new(cred, _remote_user, _remote_pass);
88 89 90
	}

	return -1;
91 92 93 94 95
}

/**
 * git_push_status_foreach callback that records status entries.
 */
96
static int record_push_status_cb(const char *ref, const char *msg, void *payload)
97
{
98
	record_callbacks_data *data = (record_callbacks_data *) payload;
99 100
	push_status *s;

101 102 103
	cl_assert(s = git__calloc(1, sizeof(*s)));
	if (ref)
		cl_assert(s->ref = git__strdup(ref));
104
	s->success = (msg == NULL);
105 106
	if (msg)
		cl_assert(s->msg = git__strdup(msg));
107

108
	git_vector_insert(&data->statuses, s);
109 110 111 112

	return 0;
}

113
static void do_verify_push_status(record_callbacks_data *data, const push_status expected[], const size_t expected_len)
114
{
115
	git_vector *actual = &data->statuses;
116 117 118 119
	push_status *iter;
	bool failed = false;
	size_t i;

120
	if (expected_len != actual->length)
121 122
		failed = true;
	else
123
		git_vector_foreach(actual, i, iter)
124
			if (strcmp(expected[i].ref, iter->ref) ||
125 126
				(expected[i].success != iter->success) ||
				(expected[i].msg && (!iter->msg || strcmp(expected[i].msg, iter->msg)))) {
127 128 129 130 131 132 133 134 135 136 137 138
				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,
139
				expected[i].success ? "success" : "failed");
140 141 142 143
		}

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

144
		git_vector_foreach(actual, i, iter) {
145 146 147 148 149
			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);
		}
150 151 152

		cl_fail(git_buf_cstr(&msg));

153
		git_buf_dispose(&msg);
154 155
	}

156 157 158 159 160 161
	git_vector_foreach(actual, i, iter) {
		push_status *s = (push_status *)iter;
		git__free(s->ref);
		git__free(s->msg);
		git__free(s);
	}
162

163
	git_vector_free(actual);
164 165 166 167 168
}

/**
 * Verifies that after git_push_finish(), refs on a remote have the expected
 * names, oids, and order.
169
 *
170 171 172 173 174 175
 * @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)
{
176 177
	const git_remote_head **actual_refs;
	size_t actual_refs_len;
178

179 180
	git_remote_ls(&actual_refs, &actual_refs_len, remote);
	verify_remote_refs(actual_refs, actual_refs_len, expected_refs, expected_refs_len);
181 182
}

183 184 185 186 187 188 189 190 191 192
/**
 * 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)
{
193
	git_refspec *fetch_spec;
194 195 196 197
	size_t i, j;
	git_buf msg = GIT_BUF_INIT;
	git_buf ref_name = GIT_BUF_INIT;
	git_vector actual_refs = GIT_VECTOR_INIT;
198
	git_branch_iterator *iter;
199 200
	char *actual_ref;
	git_oid oid;
201
	int failed = 0, error;
202
	git_branch_t branch_type;
203
	git_reference *ref;
204

205
	/* Get current remote-tracking branches */
206 207 208 209 210 211
	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))));
212 213

		git_reference_free(ref);
214 215 216
	}

	cl_assert_equal_i(error, GIT_ITEROVER);
217
	git_branch_iterator_free(iter);
218 219 220 221

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

222
		/* Convert remote reference name into remote-tracking branch name.
223 224
		 * If the spec is not under refs/heads/, then skip.
		 */
225 226
		fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
		if (!fetch_spec)
227 228
			continue;

229
		cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
230 231 232

		/* Find matching remote branch */
		git_vector_foreach(&actual_refs, j, actual_ref) {
233
			if (!strcmp(git_buf_cstr(&ref_name), actual_ref))
234 235 236 237 238 239 240 241 242 243
				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 */
244
		cl_git_pass(git_reference_name_to_id(&oid, remote->repo, actual_ref));
245 246 247 248 249 250 251

		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;
		}

252
		git__free(actual_ref);
253 254 255 256 257 258 259 260 261 262 263
		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:
264
	if (failed)
265 266 267 268 269 270
		cl_fail(git_buf_cstr(&msg));

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

	git_vector_free(&actual_refs);
271 272
	git_buf_dispose(&msg);
	git_buf_dispose(&ref_name);
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
}

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;
		}

306
		if (git_oid_cmp(expected_refs[i].oid, &tip->new_oid) != 0) {
307 308 309 310 311 312 313 314 315 316
			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));

317 318
	git_buf_dispose(&ref_name);
	git_buf_dispose(&msg);
319 320
}

321
void test_online_push__initialize(void)
322 323
{
	git_vector delete_specs = GIT_VECTOR_INIT;
324
	const git_remote_head **heads;
325
	size_t heads_len;
326 327
	git_push_options push_opts = GIT_PUSH_OPTIONS_INIT;
	git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
328 329 330

	_repo = cl_git_sandbox_init("push_src");

331
	cl_git_pass(git_repository_set_ident(_repo, "Random J. Hacker", "foo@example.com"));
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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");

355 356 357 358
	git_oid_fromstr(&_tag_commit, "805c54522e614f29f70d2413a0470247d8b424ac");
	git_oid_fromstr(&_tag_tree, "ff83aa4c5e5d28e3bcba2f5c6e2adc61286a4e5e");
	git_oid_fromstr(&_tag_blob, "b483ae7ba66decee9aee971f501221dea84b1498");
	git_oid_fromstr(&_tag_lightweight, "951bbbb90e2259a4c8950db78946784fb53fcbce");
359
	git_oid_fromstr(&_tag_tag, "eea4f2705eeec2db3813f2430829afce99cd00b5");
360

361
	/* Remote URL environment variable must be set.  User and password are optional.  */
362

363 364 365
	_remote_url = cl_getenv("GITTEST_REMOTE_URL");
	_remote_user = cl_getenv("GITTEST_REMOTE_USER");
	_remote_pass = cl_getenv("GITTEST_REMOTE_PASS");
366 367 368
	_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");
369
	_remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT");
370
	_remote_expectcontinue = cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
371 372
	_remote = NULL;

Vicent Marti committed
373 374 375
	/* Skip the test if we're missing the remote URL */
	if (!_remote_url)
		cl_skip();
376

377 378 379
	if (_remote_expectcontinue)
		git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, 1);

Vicent Marti committed
380
	cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url));
381

Vicent Marti committed
382
	record_callbacks_data_clear(&_record_cbs_data);
383

384
	cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH, &_record_cbs, NULL, NULL));
385

Vicent Marti committed
386 387 388 389 390 391 392 393 394
	/* 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) {
395 396 397 398
		git_strarray arr = {
			(char **) delete_specs.contents,
			delete_specs.length,
		};
399

400 401
		memcpy(&push_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
		cl_git_pass(git_remote_upload(_remote, &arr, &push_opts));
Vicent Marti committed
402 403 404
	}

	git_remote_disconnect(_remote);
405
	git_vector_free_deep(&delete_specs);
406

Vicent Marti committed
407
	/* Now that we've deleted everything, fetch from the remote */
408 409
	memcpy(&fetch_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
	cl_git_pass(git_remote_fetch(_remote, NULL, &fetch_opts, NULL));
410 411
}

412
void test_online_push__cleanup(void)
413 414 415
{
	if (_remote)
		git_remote_free(_remote);
416 417
	_remote = NULL;

418 419 420 421 422 423 424
	git__free(_remote_url);
	git__free(_remote_user);
	git__free(_remote_pass);
	git__free(_remote_ssh_key);
	git__free(_remote_ssh_pubkey);
	git__free(_remote_ssh_passphrase);
	git__free(_remote_default);
425
	git__free(_remote_expectcontinue);
426

427 428
	/* Freed by cl_git_sandbox_cleanup */
	_repo = NULL;
429

430 431
	git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, 0);

432 433 434 435 436 437
	record_callbacks_data_clear(&_record_cbs_data);

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

438 439
static int push_pack_progress_cb(
	int stage, unsigned int current, unsigned int total, void* payload)
440
{
441
	record_callbacks_data *data = (record_callbacks_data *) payload;
442
	GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
443 444 445 446
	if (data->pack_progress_calls < 0)
		return data->pack_progress_calls;

	data->pack_progress_calls++;
447
	return 0;
448 449
}

450 451
static int push_transfer_progress_cb(
	unsigned int current, unsigned int total, size_t bytes, void* payload)
452
{
453
	record_callbacks_data *data = (record_callbacks_data *) payload;
454
	GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
455 456 457 458
	if (data->transfer_progress_calls < 0)
		return data->transfer_progress_calls;

	data->transfer_progress_calls++;
459
	return 0;
460 461
}

462 463
/**
 * Calls push and relists refs on remote to verify success.
464
 *
465 466 467 468 469
 * @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()
470
 * @param check_progress_cb Check that the push progress callbacks are called
471
 */
472 473
static void do_push(
	const char *refspecs[], size_t refspecs_len,
474
	push_status expected_statuses[], size_t expected_statuses_len,
475
	expected_ref expected_refs[], size_t expected_refs_len,
476
	int expected_ret, int check_progress_cb, int check_update_tips_cb)
477
{
478
	git_push_options opts = GIT_PUSH_OPTIONS_INIT;
479
	size_t i;
480
	int error;
481
	git_strarray specs = {0};
482
	record_callbacks_data *data;
483 484

	if (_remote) {
485 486 487
		/* Auto-detect the number of threads to use */
		opts.pb_parallelism = 0;

488 489
		memcpy(&opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
		data = opts.callbacks.payload;
490

491 492 493
		opts.callbacks.pack_progress = push_pack_progress_cb;
		opts.callbacks.push_transfer_progress = push_transfer_progress_cb;
		opts.callbacks.push_update_reference = record_push_status_cb;
494

495 496 497 498
		if (refspecs_len) {
			specs.count = refspecs_len;
			specs.strings = git__calloc(refspecs_len, sizeof(char *));
			cl_assert(specs.strings);
499
		}
500

501
		for (i = 0; i < refspecs_len; i++)
502 503 504 505 506 507
			specs.strings[i] = (char *) refspecs[i];

		/* if EUSER, then abort in transfer */
		if (check_progress_cb && expected_ret == GIT_EUSER)
			data->transfer_progress_calls = GIT_EUSER;

508
		error = git_remote_push(_remote, &specs, &opts);
509
		git__free(specs.strings);
510 511

		if (expected_ret < 0) {
512
			cl_git_fail_with(expected_ret, error);
513
		} else {
514
			cl_git_pass(error);
515 516
		}

517 518 519
		if (check_progress_cb && expected_ret == 0) {
			cl_assert(data->pack_progress_calls > 0);
			cl_assert(data->transfer_progress_calls > 0);
520 521
		}

522
		do_verify_push_status(data, expected_statuses, expected_statuses_len);
523 524

		verify_refs(_remote, expected_refs, expected_refs_len);
525 526
		verify_tracking_branches(_remote, expected_refs, expected_refs_len);

527 528 529
		if (check_update_tips_cb)
			verify_update_tips_callback(_remote, expected_refs, expected_refs_len);

530
	}
531

532 533 534
}

/* Call push_finish() without ever calling git_push_add_refspec() */
535
void test_online_push__noop(void)
536
{
537
	do_push(NULL, 0, NULL, 0, NULL, 0, 0, 0, 1);
538 539
}

540
void test_online_push__b1(void)
541 542
{
	const char *specs[] = { "refs/heads/b1:refs/heads/b1" };
543
	push_status exp_stats[] = { { "refs/heads/b1", 1 } };
544 545 546
	expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
547
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
548 549
}

550
void test_online_push__b2(void)
551 552
{
	const char *specs[] = { "refs/heads/b2:refs/heads/b2" };
553
	push_status exp_stats[] = { { "refs/heads/b2", 1 } };
554 555 556
	expected_ref exp_refs[] = { { "refs/heads/b2", &_oid_b2 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
557
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
558 559
}

560
void test_online_push__b3(void)
561 562
{
	const char *specs[] = { "refs/heads/b3:refs/heads/b3" };
563
	push_status exp_stats[] = { { "refs/heads/b3", 1 } };
564 565 566
	expected_ref exp_refs[] = { { "refs/heads/b3", &_oid_b3 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
567
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
568 569
}

570
void test_online_push__b4(void)
571 572
{
	const char *specs[] = { "refs/heads/b4:refs/heads/b4" };
573
	push_status exp_stats[] = { { "refs/heads/b4", 1 } };
574 575 576
	expected_ref exp_refs[] = { { "refs/heads/b4", &_oid_b4 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
577
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
578 579
}

580
void test_online_push__b5(void)
581 582
{
	const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
583
	push_status exp_stats[] = { { "refs/heads/b5", 1 } };
584 585 586
	expected_ref exp_refs[] = { { "refs/heads/b5", &_oid_b5 } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
587
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
588 589
}

590 591 592
void test_online_push__b5_cancel(void)
{
	const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
593
	do_push(specs, ARRAY_SIZE(specs), NULL, 0, NULL, 0, GIT_EUSER, 1, 1);
594 595
}

596
void test_online_push__multi(void)
597
{
598 599 600
	git_reflog *log;
	const git_reflog_entry *entry;

601 602 603 604 605 606 607 608
	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[] = {
609 610 611 612 613
		{ "refs/heads/b1", 1 },
		{ "refs/heads/b2", 1 },
		{ "refs/heads/b3", 1 },
		{ "refs/heads/b4", 1 },
		{ "refs/heads/b5", 1 }
614 615 616 617 618 619 620 621 622 623
	};
	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),
624
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
625 626 627

	cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1"));
	entry = git_reflog_entry_byindex(log, 0);
628
	if (entry) {
629
		cl_assert_equal_s("update by push", git_reflog_entry_message(entry));
630 631
		cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
	}
632 633

	git_reflog_free(log);
634 635
}

636
void test_online_push__implicit_tgt(void)
637
{
638
	const char *specs1[] = { "refs/heads/b1" };
639
	push_status exp_stats1[] = { { "refs/heads/b1", 1 } };
640 641
	expected_ref exp_refs1[] = { { "refs/heads/b1", &_oid_b1 } };

642
	const char *specs2[] = { "refs/heads/b2" };
643
	push_status exp_stats2[] = { { "refs/heads/b2", 1 } };
644 645 646 647 648 649 650
	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),
651
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
652 653
	do_push(specs2, ARRAY_SIZE(specs2),
		exp_stats2, ARRAY_SIZE(exp_stats2),
654
		exp_refs2, ARRAY_SIZE(exp_refs2), 0, 0, 0);
655 656
}

657
void test_online_push__fast_fwd(void)
658 659 660 661
{
	/* Fast forward b1 in tgt from _oid_b1 to _oid_b6. */

	const char *specs_init[] = { "refs/heads/b1:refs/heads/b1" };
662
	push_status exp_stats_init[] = { { "refs/heads/b1", 1 } };
663 664 665
	expected_ref exp_refs_init[] = { { "refs/heads/b1", &_oid_b1 } };

	const char *specs_ff[] = { "refs/heads/b6:refs/heads/b1" };
666
	push_status exp_stats_ff[] = { { "refs/heads/b1", 1 } };
667 668 669 670 671 672 673 674 675
	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),
676
		exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 1, 1);
677 678 679

	do_push(specs_ff, ARRAY_SIZE(specs_ff),
		exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
680
		exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
681 682 683

	do_push(specs_reset, ARRAY_SIZE(specs_reset),
		exp_stats_init, ARRAY_SIZE(exp_stats_init),
684
		exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 0, 0);
685 686 687

	do_push(specs_ff_force, ARRAY_SIZE(specs_ff_force),
		exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
688
		exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
689 690
}

691 692 693
void test_online_push__tag_commit(void)
{
	const char *specs[] = { "refs/tags/tag-commit:refs/tags/tag-commit" };
694
	push_status exp_stats[] = { { "refs/tags/tag-commit", 1 } };
695 696 697
	expected_ref exp_refs[] = { { "refs/tags/tag-commit", &_tag_commit } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
698
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
699 700 701 702 703
}

void test_online_push__tag_tree(void)
{
	const char *specs[] = { "refs/tags/tag-tree:refs/tags/tag-tree" };
704
	push_status exp_stats[] = { { "refs/tags/tag-tree", 1 } };
705 706 707
	expected_ref exp_refs[] = { { "refs/tags/tag-tree", &_tag_tree } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
708
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
709 710 711 712 713
}

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

void test_online_push__tag_lightweight(void)
{
	const char *specs[] = { "refs/tags/tag-lightweight:refs/tags/tag-lightweight" };
724
	push_status exp_stats[] = { { "refs/tags/tag-lightweight", 1 } };
725 726 727
	expected_ref exp_refs[] = { { "refs/tags/tag-lightweight", &_tag_lightweight } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
728
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
729 730
}

731 732 733
void test_online_push__tag_to_tag(void)
{
	const char *specs[] = { "refs/tags/tag-tag:refs/tags/tag-tag" };
734
	push_status exp_stats[] = { { "refs/tags/tag-tag", 1 } };
735 736 737
	expected_ref exp_refs[] = { { "refs/tags/tag-tag", &_tag_tag } };
	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
738
		exp_refs, ARRAY_SIZE(exp_refs), 0, 0, 0);
739 740
}

741
void test_online_push__force(void)
742 743
{
	const char *specs1[] = {"refs/heads/b3:refs/heads/tgt"};
744
	push_status exp_stats1[] = { { "refs/heads/tgt", 1 } };
745 746 747 748 749
	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"};
750
	push_status exp_stats2_force[] = { { "refs/heads/tgt", 1 } };
751 752 753 754
	expected_ref exp_refs2_force[] = { { "refs/heads/tgt", &_oid_b4 } };

	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
755
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
756 757 758

	do_push(specs2, ARRAY_SIZE(specs2),
		NULL, 0,
759
		exp_refs1, ARRAY_SIZE(exp_refs1), GIT_ENONFASTFORWARD, 0, 0);
760 761

	/* Non-fast-forward update with force should pass. */
762
	record_callbacks_data_clear(&_record_cbs_data);
763 764
	do_push(specs2_force, ARRAY_SIZE(specs2_force),
		exp_stats2_force, ARRAY_SIZE(exp_stats2_force),
765
		exp_refs2_force, ARRAY_SIZE(exp_refs2_force), 0, 1, 1);
766 767
}

768
void test_online_push__delete(void)
769 770 771 772 773 774
{
	const char *specs1[] = {
		"refs/heads/b1:refs/heads/tgt1",
		"refs/heads/b1:refs/heads/tgt2"
	};
	push_status exp_stats1[] = {
775 776
		{ "refs/heads/tgt1", 1 },
		{ "refs/heads/tgt2", 1 }
777 778 779 780 781 782 783 784 785
	};
	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" };
786
	push_status exp_stats_fake[] = { { "refs/heads/fake", 1 } };
787 788

	const char *specs_delete[] = { ":refs/heads/tgt1" };
789
	push_status exp_stats_delete[] = { { "refs/heads/tgt1", 1 } };
790 791 792 793 794 795
	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),
796
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
797

798 799 800 801 802
	/* 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.
803 804
	 */
	do_push(specs_del_fake, ARRAY_SIZE(specs_del_fake),
805
		exp_stats_fake, 1,
806
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
807
	do_push(specs_del_fake_force, ARRAY_SIZE(specs_del_fake_force),
808
		exp_stats_fake, 1,
809
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
810 811 812 813

	/* Delete one of the pushed branches. */
	do_push(specs_delete, ARRAY_SIZE(specs_delete),
		exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
814
		exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
815 816 817 818

	/* Re-push branches and retry delete with force. */
	do_push(specs1, ARRAY_SIZE(specs1),
		exp_stats1, ARRAY_SIZE(exp_stats1),
819
		exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
820 821
	do_push(specs_delete_force, ARRAY_SIZE(specs_delete_force),
		exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
822
		exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
823 824
}

825
void test_online_push__bad_refspecs(void)
826 827 828 829
{
	/* All classes of refspecs that should be rejected by
	 * git_push_add_refspec() should go in this test.
	 */
830 831 832 833 834 835 836
	char *specs = {
		"b6:b6",
	};
	git_strarray arr = {
		&specs,
		1,
	};
837 838

	if (_remote) {
839
		cl_git_fail(git_remote_upload(_remote, &arr, NULL));
840 841 842
	}
}

843
void test_online_push__expressions(void)
844 845 846 847 848 849 850
{
	/* TODO: Expressions in refspecs doesn't actually work yet */
	const char *specs_left_expr[] = { "refs/heads/b2~1:refs/heads/b2" };

	/* 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,
851
		NULL, 0, -1, 0, 0);
852
}
853

854
void test_online_push__notes(void)
855 856 857 858
{
	git_oid note_oid, *target_oid, expected_oid;
	git_signature *signature;
	const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
859
	push_status exp_stats[] = { { "refs/notes/commits", 1 } };
860
	expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
861 862
	const char *specs_del[] = { ":refs/notes/commits" };

863 864 865 866 867 868
	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 */
869
	cl_git_pass(git_note_create(&note_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
870 871 872

	do_push(specs, ARRAY_SIZE(specs),
		exp_stats, ARRAY_SIZE(exp_stats),
873
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
874

875 876 877 878
	/* And make sure to delete the note */

	do_push(specs_del, ARRAY_SIZE(specs_del),
		exp_stats, 1,
879
		NULL, 0, 0, 0, 0);
880

881 882
	git_signature_free(signature);
}
883 884 885 886 887

void test_online_push__configured(void)
{
	git_oid note_oid, *target_oid, expected_oid;
	git_signature *signature;
888
	git_remote *old_remote;
889 890 891 892 893 894 895 896 897
	const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
	push_status exp_stats[] = { { "refs/notes/commits", 1 } };
	expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
	const char *specs_del[] = { ":refs/notes/commits" };

	git_oid_fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb");

	target_oid = &_oid_b6;

898 899 900 901
	cl_git_pass(git_remote_add_push(_repo, git_remote_name(_remote), specs[0]));
	old_remote = _remote;
	cl_git_pass(git_remote_lookup(&_remote, _repo, git_remote_name(_remote)));
	git_remote_free(old_remote);
902 903 904

	/* 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 */
905
	cl_git_pass(git_note_create(&note_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
906 907 908 909 910 911 912 913 914 915 916 917 918

	do_push(NULL, 0,
		exp_stats, ARRAY_SIZE(exp_stats),
		exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);

	/* And make sure to delete the note */

	do_push(specs_del, ARRAY_SIZE(specs_del),
		exp_stats, 1,
		NULL, 0, 0, 0, 0);

	git_signature_free(signature);
}