transport.c 28.8 KB
Newer Older
1 2 3
#include "cache.h"
#include "transport.h"
#include "run-command.h"
4 5
#include "pkt-line.h"
#include "fetch-pack.h"
6 7
#include "remote.h"
#include "connect.h"
D
Daniel Barkalow 已提交
8
#include "send-pack.h"
9
#include "walker.h"
J
Johannes Schindelin 已提交
10
#include "bundle.h"
11 12
#include "dir.h"
#include "refs.h"
I
Ilari Liusvaara 已提交
13
#include "branch.h"
J
Jeff King 已提交
14
#include "url.h"
15
#include "submodule.h"
16
#include "string-list.h"
17
#include "sha1-array.h"
18
#include "sigchain.h"
19

I
Ilari Liusvaara 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
static void set_upstreams(struct transport *transport, struct ref *refs,
	int pretend)
{
	struct ref *ref;
	for (ref = refs; ref; ref = ref->next) {
		const char *localname;
		const char *tmp;
		const char *remotename;
		unsigned char sha[20];
		int flag = 0;
		/*
		 * Check suitability for tracking. Must be successful /
		 * already up-to-date ref create/modify (not delete).
		 */
		if (ref->status != REF_STATUS_OK &&
			ref->status != REF_STATUS_UPTODATE)
			continue;
		if (!ref->peer_ref)
			continue;
39
		if (is_null_oid(&ref->new_oid))
I
Ilari Liusvaara 已提交
40 41 42 43 44
			continue;

		/* Follow symbolic refs (mainly for HEAD). */
		localname = ref->peer_ref->name;
		remotename = ref->name;
45 46
		tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
					 sha, &flag);
I
Ilari Liusvaara 已提交
47
		if (tmp && flag & REF_ISSYMREF &&
48
			starts_with(tmp, "refs/heads/"))
I
Ilari Liusvaara 已提交
49 50 51
			localname = tmp;

		/* Both source and destination must be local branches. */
52
		if (!localname || !starts_with(localname, "refs/heads/"))
I
Ilari Liusvaara 已提交
53
			continue;
54
		if (!remotename || !starts_with(remotename, "refs/heads/"))
I
Ilari Liusvaara 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
			continue;

		if (!pretend)
			install_branch_config(BRANCH_CONFIG_VERBOSE,
				localname + 11, transport->remote->name,
				remotename);
		else
			printf("Would set upstream of '%s' to '%s' of '%s'\n",
				localname + 11, remotename + 11,
				transport->remote->name);
	}
}

J
Johannes Schindelin 已提交
68 69 70 71 72
struct bundle_transport_data {
	int fd;
	struct bundle_header header;
};

73
static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
J
Johannes Schindelin 已提交
74 75 76 77 78
{
	struct bundle_transport_data *data = transport->data;
	struct ref *result = NULL;
	int i;

79 80 81
	if (for_push)
		return NULL;

J
Johannes Schindelin 已提交
82 83 84 85 86 87 88
	if (data->fd > 0)
		close(data->fd);
	data->fd = read_bundle_header(transport->url, &data->header);
	if (data->fd < 0)
		die ("Could not read bundle '%s'.", transport->url);
	for (i = 0; i < data->header.references.nr; i++) {
		struct ref_list_entry *e = data->header.references.list + i;
89
		struct ref *ref = alloc_ref(e->name);
90
		hashcpy(ref->old_oid.hash, e->sha1);
J
Johannes Schindelin 已提交
91 92 93 94 95 96
		ref->next = result;
		result = ref;
	}
	return result;
}

97
static int fetch_refs_from_bundle(struct transport *transport,
D
Daniel Barkalow 已提交
98
			       int nr_heads, struct ref **to_fetch)
J
Johannes Schindelin 已提交
99 100
{
	struct bundle_transport_data *data = transport->data;
101 102
	return unbundle(&data->header, data->fd,
			transport->progress ? BUNDLE_VERBOSE : 0);
J
Johannes Schindelin 已提交
103 104 105 106 107 108 109
}

static int close_bundle(struct transport *transport)
{
	struct bundle_transport_data *data = transport->data;
	if (data->fd > 0)
		close(data->fd);
110
	free(data);
J
Johannes Schindelin 已提交
111 112 113
	return 0;
}

114
struct git_transport_data {
115
	struct git_transport_options options;
116 117
	struct child_process *conn;
	int fd[2];
I
Ilari Liusvaara 已提交
118
	unsigned got_remote_heads : 1;
119
	struct sha1_array extra_have;
120
	struct sha1_array shallow;
121 122
};

123
static int set_git_option(struct git_transport_options *opts,
124 125
			  const char *name, const char *value)
{
126
	if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
127
		opts->uploadpack = value;
128 129
		return 0;
	} else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
130
		opts->receivepack = value;
131 132
		return 0;
	} else if (!strcmp(name, TRANS_OPT_THIN)) {
133
		opts->thin = !!value;
134
		return 0;
135
	} else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
136
		opts->followtags = !!value;
137
		return 0;
138
	} else if (!strcmp(name, TRANS_OPT_KEEP)) {
139
		opts->keep = !!value;
140
		return 0;
141 142 143
	} else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
		opts->update_shallow = !!value;
		return 0;
144 145
	} else if (!strcmp(name, TRANS_OPT_DEPTH)) {
		if (!value)
146
			opts->depth = 0;
147 148 149 150 151 152
		else {
			char *end;
			opts->depth = strtol(value, &end, 0);
			if (*end)
				die("transport: invalid depth option '%s'", value);
		}
153
		return 0;
154 155 156 157
	}
	return 1;
}

158
static int connect_setup(struct transport *transport, int for_push)
159 160
{
	struct git_transport_data *data = transport->data;
161
	int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
I
Ilari Liusvaara 已提交
162 163 164 165

	if (data->conn)
		return 0;

166 167 168 169 170 171
	switch (transport->family) {
	case TRANSPORT_FAMILY_ALL: break;
	case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
	case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
	}

172 173 174
	data->conn = git_connect(data->fd, transport->url,
				 for_push ? data->options.receivepack :
				 data->options.uploadpack,
175
				 flags);
I
Ilari Liusvaara 已提交
176

177 178 179
	return 0;
}

180
static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
181 182 183 184
{
	struct git_transport_data *data = transport->data;
	struct ref *refs;

185
	connect_setup(transport, for_push);
186
	get_remote_heads(data->fd[0], NULL, 0, &refs,
187 188
			 for_push ? REF_NORMAL : 0,
			 &data->extra_have,
189
			 &data->shallow);
I
Ilari Liusvaara 已提交
190
	data->got_remote_heads = 1;
191 192 193 194

	return refs;
}

195
static int fetch_refs_via_pack(struct transport *transport,
D
Daniel Barkalow 已提交
196
			       int nr_heads, struct ref **to_fetch)
197 198
{
	struct git_transport_data *data = transport->data;
199
	struct ref *refs;
200 201
	char *dest = xstrdup(transport->url);
	struct fetch_pack_args args;
202
	struct ref *refs_tmp = NULL;
203

204
	memset(&args, 0, sizeof(args));
205 206
	args.uploadpack = data->options.uploadpack;
	args.keep_pack = data->options.keep;
207
	args.lock_pack = 1;
208 209
	args.use_thin_pack = data->options.thin;
	args.include_tag = data->options.followtags;
J
Jeff King 已提交
210
	args.verbose = (transport->verbose > 1);
211
	args.quiet = (transport->verbose < 0);
212
	args.no_progress = !transport->progress;
213
	args.depth = data->options.depth;
214 215
	args.check_self_contained_and_connected =
		data->options.check_self_contained_and_connected;
216
	args.cloning = transport->cloning;
217
	args.update_shallow = data->options.update_shallow;
218

I
Ilari Liusvaara 已提交
219
	if (!data->got_remote_heads) {
220
		connect_setup(transport, 0);
221
		get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
222
				 NULL, &data->shallow);
I
Ilari Liusvaara 已提交
223
		data->got_remote_heads = 1;
224 225
	}

226 227
	refs = fetch_pack(&args, data->fd, data->conn,
			  refs_tmp ? refs_tmp : transport->remote_refs,
228
			  dest, to_fetch, nr_heads, &data->shallow,
229
			  &transport->pack_lockfile);
230 231
	close(data->fd[0]);
	close(data->fd[1]);
232 233
	if (finish_connect(data->conn)) {
		free_refs(refs);
234
		refs = NULL;
235
	}
236
	data->conn = NULL;
I
Ilari Liusvaara 已提交
237
	data->got_remote_heads = 0;
238 239
	data->options.self_contained_and_connected =
		args.self_contained_and_connected;
240

241
	free_refs(refs_tmp);
242
	free_refs(refs);
243
	free(dest);
244
	return (refs ? 0 : -1);
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
static int push_had_errors(struct ref *ref)
{
	for (; ref; ref = ref->next) {
		switch (ref->status) {
		case REF_STATUS_NONE:
		case REF_STATUS_UPTODATE:
		case REF_STATUS_OK:
			break;
		default:
			return 1;
		}
	}
	return 0;
}

262
int transport_refs_pushed(struct ref *ref)
263 264 265 266 267 268 269 270 271 272 273 274 275
{
	for (; ref; ref = ref->next) {
		switch(ref->status) {
		case REF_STATUS_NONE:
		case REF_STATUS_UPTODATE:
			break;
		default:
			return 1;
		}
	}
	return 0;
}

276
void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
	struct refspec rs;

	if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
		return;

	rs.src = ref->name;
	rs.dst = NULL;

	if (!remote_find_tracking(remote, &rs)) {
		if (verbose)
			fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
		if (ref->deletion) {
			delete_ref(rs.dst, NULL, 0);
		} else
			update_ref("update by push", rs.dst,
293
					ref->new_oid.hash, NULL, 0, 0);
294 295 296 297
		free(rs.dst);
	}
}

298
static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
299
{
300 301 302 303 304 305 306 307 308 309
	if (porcelain) {
		if (from)
			fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
		else
			fprintf(stdout, "%c\t:%s\t", flag, to->name);
		if (msg)
			fprintf(stdout, "%s (%s)\n", summary, msg);
		else
			fprintf(stdout, "%s\n", summary);
	} else {
310
		fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
311 312 313 314 315 316 317 318 319 320
		if (from)
			fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
		else
			fputs(prettify_refname(to->name), stderr);
		if (msg) {
			fputs(" (", stderr);
			fputs(msg, stderr);
			fputc(')', stderr);
		}
		fputc('\n', stderr);
321 322 323 324 325 326 327 328
	}
}

static const char *status_abbrev(unsigned char sha1[20])
{
	return find_unique_abbrev(sha1, DEFAULT_ABBREV);
}

329
static void print_ok_ref_status(struct ref *ref, int porcelain)
330 331
{
	if (ref->deletion)
332
		print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
333
	else if (is_null_oid(&ref->old_oid))
334
		print_ref_status('*',
335
			(starts_with(ref->name, "refs/tags/") ? "[new tag]" :
336 337
			"[new branch]"),
			ref, ref->peer_ref, NULL, porcelain);
338
	else {
339
		struct strbuf quickref = STRBUF_INIT;
340 341 342
		char type;
		const char *msg;

343
		strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
344
		if (ref->forced_update) {
345
			strbuf_addstr(&quickref, "...");
346 347 348
			type = '+';
			msg = "forced update";
		} else {
349
			strbuf_addstr(&quickref, "..");
350 351 352
			type = ' ';
			msg = NULL;
		}
353
		strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
354

355 356
		print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
		strbuf_release(&quickref);
357 358 359
	}
}

360
static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
361 362
{
	if (!count)
363
		fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
364 365 366

	switch(ref->status) {
	case REF_STATUS_NONE:
367
		print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
368 369 370
		break;
	case REF_STATUS_REJECT_NODELETE:
		print_ref_status('!', "[rejected]", ref, NULL,
371
						 "remote does not support deleting refs", porcelain);
372 373 374
		break;
	case REF_STATUS_UPTODATE:
		print_ref_status('=', "[up to date]", ref,
375
						 ref->peer_ref, NULL, porcelain);
376 377 378
		break;
	case REF_STATUS_REJECT_NONFASTFORWARD:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
379
						 "non-fast-forward", porcelain);
380
		break;
381 382 383 384
	case REF_STATUS_REJECT_ALREADY_EXISTS:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "already exists", porcelain);
		break;
385 386 387 388 389 390 391 392
	case REF_STATUS_REJECT_FETCH_FIRST:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "fetch first", porcelain);
		break;
	case REF_STATUS_REJECT_NEEDS_FORCE:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "needs force", porcelain);
		break;
393 394 395 396
	case REF_STATUS_REJECT_STALE:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "stale info", porcelain);
		break;
397 398 399 400
	case REF_STATUS_REJECT_SHALLOW:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "new shallow roots not allowed", porcelain);
		break;
401 402
	case REF_STATUS_REMOTE_REJECT:
		print_ref_status('!', "[remote rejected]", ref,
403 404
						 ref->deletion ? NULL : ref->peer_ref,
						 ref->remote_status, porcelain);
405 406 407
		break;
	case REF_STATUS_EXPECTING_REPORT:
		print_ref_status('!', "[remote failure]", ref,
408 409
						 ref->deletion ? NULL : ref->peer_ref,
						 "remote failed to report status", porcelain);
410
		break;
411 412 413 414
	case REF_STATUS_ATOMIC_PUSH_FAILED:
		print_ref_status('!', "[rejected]", ref, ref->peer_ref,
						 "atomic push failed", porcelain);
		break;
415
	case REF_STATUS_OK:
416
		print_ok_ref_status(ref, porcelain);
417 418 419 420 421 422
		break;
	}

	return 1;
}

423
void transport_print_push_status(const char *dest, struct ref *refs,
424
				  int verbose, int porcelain, unsigned int *reject_reasons)
425 426 427
{
	struct ref *ref;
	int n = 0;
428 429 430
	unsigned char head_sha1[20];
	char *head;

431
	head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
432 433 434 435

	if (verbose) {
		for (ref = refs; ref; ref = ref->next)
			if (ref->status == REF_STATUS_UPTODATE)
436
				n += print_one_push_status(ref, dest, n, porcelain);
437 438 439 440
	}

	for (ref = refs; ref; ref = ref->next)
		if (ref->status == REF_STATUS_OK)
441
			n += print_one_push_status(ref, dest, n, porcelain);
442

443
	*reject_reasons = 0;
444 445 446 447
	for (ref = refs; ref; ref = ref->next) {
		if (ref->status != REF_STATUS_NONE &&
		    ref->status != REF_STATUS_UPTODATE &&
		    ref->status != REF_STATUS_OK)
448
			n += print_one_push_status(ref, dest, n, porcelain);
449
		if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
450
			if (head != NULL && !strcmp(head, ref->name))
451
				*reject_reasons |= REJECT_NON_FF_HEAD;
452
			else
453
				*reject_reasons |= REJECT_NON_FF_OTHER;
454 455
		} else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
			*reject_reasons |= REJECT_ALREADY_EXISTS;
456 457 458 459
		} else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
			*reject_reasons |= REJECT_FETCH_FIRST;
		} else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
			*reject_reasons |= REJECT_NEEDS_FORCE;
460
		}
461
	}
462
	free(head);
463 464
}

465
void transport_verify_remote_names(int nr_heads, const char **heads)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
{
	int i;

	for (i = 0; i < nr_heads; i++) {
		const char *local = heads[i];
		const char *remote = strrchr(heads[i], ':');

		if (*local == '+')
			local++;

		/* A matching refspec is okay.  */
		if (remote == local && remote[1] == '\0')
			continue;

		remote = remote ? (remote + 1) : local;
481 482 483 484
		if (check_refname_format(remote,
				REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
			die("remote part of refspec is not a valid name in %s",
				heads[i]);
485 486 487 488
	}
}

static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
489
{
490
	struct git_transport_data *data = transport->data;
D
Daniel Barkalow 已提交
491
	struct send_pack_args args;
492 493
	int ret;

I
Ilari Liusvaara 已提交
494
	if (!data->got_remote_heads) {
495
		struct ref *tmp_refs;
496
		connect_setup(transport, 1);
497

498 499
		get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
				 NULL, &data->shallow);
I
Ilari Liusvaara 已提交
500
		data->got_remote_heads = 1;
501
	}
502

503
	memset(&args, 0, sizeof(args));
A
Andy Whitcroft 已提交
504
	args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
D
Daniel Barkalow 已提交
505
	args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
506
	args.use_thin_pack = data->options.thin;
507 508
	args.verbose = (transport->verbose > 0);
	args.quiet = (transport->verbose < 0);
509
	args.progress = transport->progress;
D
Daniel Barkalow 已提交
510
	args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
511
	args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
512
	args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
513
	args.url = transport->url;
D
Daniel Barkalow 已提交
514

515 516 517 518 519 520 521
	if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
		args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
	else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
		args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
	else
		args.push_cert = SEND_PACK_PUSH_CERT_NEVER;

522 523 524 525 526 527 528
	ret = send_pack(&args, data->fd, data->conn, remote_refs,
			&data->extra_have);

	close(data->fd[1]);
	close(data->fd[0]);
	ret |= finish_connect(data->conn);
	data->conn = NULL;
I
Ilari Liusvaara 已提交
529
	data->got_remote_heads = 0;
530 531

	return ret;
532 533
}

534 535 536 537 538 539 540 541 542 543 544
static int connect_git(struct transport *transport, const char *name,
		       const char *executable, int fd[2])
{
	struct git_transport_data *data = transport->data;
	data->conn = git_connect(data->fd, transport->url,
				 executable, 0);
	fd[0] = data->fd[0];
	fd[1] = data->fd[1];
	return 0;
}

545 546
static int disconnect_git(struct transport *transport)
{
547 548
	struct git_transport_data *data = transport->data;
	if (data->conn) {
I
Ilari Liusvaara 已提交
549 550
		if (data->got_remote_heads)
			packet_flush(data->fd[1]);
551 552 553 554 555 556
		close(data->fd[0]);
		close(data->fd[1]);
		finish_connect(data->conn);
	}

	free(data);
557 558 559
	return 0;
}

I
Ilari Liusvaara 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
void transport_take_over(struct transport *transport,
			 struct child_process *child)
{
	struct git_transport_data *data;

	if (!transport->smart_options)
		die("Bug detected: Taking over transport requires non-NULL "
		    "smart_options field.");

	data = xcalloc(1, sizeof(*data));
	data->options = *transport->smart_options;
	data->conn = child;
	data->fd[0] = data->conn->out;
	data->fd[1] = data->conn->in;
	data->got_remote_heads = 0;
	transport->data = data;

	transport->set_option = NULL;
	transport->get_refs_list = get_refs_via_connect;
	transport->fetch = fetch_refs_via_pack;
	transport->push = NULL;
	transport->push_refs = git_transport_push;
	transport->disconnect = disconnect_git;
	transport->smart_options = &(data->options);
584 585

	transport->cannot_reuse = 1;
I
Ilari Liusvaara 已提交
586 587
}

588 589 590 591 592 593 594 595
static int is_file(const char *url)
{
	struct stat buf;
	if (stat(url, &buf))
		return 0;
	return S_ISREG(buf.st_mode);
}

596 597 598 599 600
static int external_specification_len(const char *url)
{
	return strchr(url, ':') - url;
}

601
static const struct string_list *protocol_whitelist(void)
602
{
603 604
	static int enabled = -1;
	static struct string_list allowed = STRING_LIST_INIT_DUP;
605

606 607 608 609 610 611 612 613 614 615 616 617 618
	if (enabled < 0) {
		const char *v = getenv("GIT_ALLOW_PROTOCOL");
		if (v) {
			string_list_split(&allowed, v, ':', -1);
			string_list_sort(&allowed);
			enabled = 1;
		} else {
			enabled = 0;
		}
	}

	return enabled ? &allowed : NULL;
}
619

620 621 622 623 624 625 626 627 628
int is_transport_allowed(const char *type)
{
	const struct string_list *allowed = protocol_whitelist();
	return !allowed || string_list_has_string(allowed, type);
}

void transport_check_allowed(const char *type)
{
	if (!is_transport_allowed(type))
629
		die("transport '%s' not allowed", type);
630 631 632 633 634
}

int transport_restrict_protocols(void)
{
	return !!protocol_whitelist();
635 636
}

637
struct transport *transport_get(struct remote *remote, const char *url)
638
{
I
Ilari Liusvaara 已提交
639
	const char *helper;
640 641
	struct transport *ret = xcalloc(1, sizeof(*ret));

642 643
	ret->progress = isatty(2);

644 645 646
	if (!remote)
		die("No remote provided to transport_get()");

647
	ret->got_remote_refs = 0;
648
	ret->remote = remote;
I
Ilari Liusvaara 已提交
649
	helper = remote->foreign_vcs;
650

651
	if (!url && remote->url)
652
		url = remote->url[0];
653 654
	ret->url = url;

655 656 657 658
	/* maybe it is a foreign URL? */
	if (url) {
		const char *p = url;

J
Jeff King 已提交
659
		while (is_urlschemechar(p == url, *p))
660
			p++;
661
		if (starts_with(p, "::"))
I
Ilari Liusvaara 已提交
662
			helper = xstrndup(url, p - url);
663 664
	}

I
Ilari Liusvaara 已提交
665 666
	if (helper) {
		transport_helper_init(ret, helper);
667
	} else if (starts_with(url, "rsync:")) {
668
		die("git-over-rsync is no longer supported");
669
	} else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
J
Johannes Schindelin 已提交
670
		struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
671
		transport_check_allowed("file");
J
Johannes Schindelin 已提交
672
		ret->data = data;
673 674 675
		ret->get_refs_list = get_refs_from_bundle;
		ret->fetch = fetch_refs_from_bundle;
		ret->disconnect = close_bundle;
676
		ret->smart_options = NULL;
677
	} else if (!is_url(url)
678 679 680
		|| starts_with(url, "file://")
		|| starts_with(url, "git://")
		|| starts_with(url, "ssh://")
681 682 683
		|| starts_with(url, "git+ssh://") /* deprecated - do not use */
		|| starts_with(url, "ssh+git://") /* deprecated - do not use */
		) {
684 685 686 687
		/*
		 * These are builtin smart transports; "allowed" transports
		 * will be checked individually in git_connect.
		 */
688 689
		struct git_transport_data *data = xcalloc(1, sizeof(*data));
		ret->data = data;
690
		ret->set_option = NULL;
691 692
		ret->get_refs_list = get_refs_via_connect;
		ret->fetch = fetch_refs_via_pack;
693
		ret->push_refs = git_transport_push;
694
		ret->connect = connect_git;
695
		ret->disconnect = disconnect_git;
696
		ret->smart_options = &(data->options);
697

698
		data->conn = NULL;
I
Ilari Liusvaara 已提交
699
		data->got_remote_heads = 0;
700 701 702
	} else {
		/* Unknown protocol in URL. Pass to external handler. */
		int len = external_specification_len(url);
703
		char *handler = xmemdupz(url, len);
704
		transport_helper_init(ret, handler);
705
	}
706

707 708 709 710 711 712 713 714 715 716
	if (ret->smart_options) {
		ret->smart_options->thin = 1;
		ret->smart_options->uploadpack = "git-upload-pack";
		if (remote->uploadpack)
			ret->smart_options->uploadpack = remote->uploadpack;
		ret->smart_options->receivepack = "git-receive-pack";
		if (remote->receivepack)
			ret->smart_options->receivepack = remote->receivepack;
	}

717 718 719 720 721 722
	return ret;
}

int transport_set_option(struct transport *transport,
			 const char *name, const char *value)
{
723 724 725 726 727 728
	int git_reports = 1, protocol_reports = 1;

	if (transport->smart_options)
		git_reports = set_git_option(transport->smart_options,
					     name, value);

729
	if (transport->set_option)
730 731 732 733 734 735 736 737 738 739
		protocol_reports = transport->set_option(transport, name,
							value);

	/* If either report is 0, report 0 (success). */
	if (!git_reports || !protocol_reports)
		return 0;
	/* If either reports -1 (invalid value), report -1. */
	if ((git_reports == -1) || (protocol_reports == -1))
		return -1;
	/* Otherwise if both report unknown, report unknown. */
740
	return 1;
741 742
}

743 744
void transport_set_verbosity(struct transport *transport, int verbosity,
	int force_progress)
745
{
J
Jeff King 已提交
746
	if (verbosity >= 1)
747 748 749
		transport->verbose = verbosity <= 3 ? verbosity : 3;
	if (verbosity < 0)
		transport->verbose = -1;
750 751 752 753 754

	/**
	 * Rules used to determine whether to report progress (processing aborts
	 * when a rule is satisfied):
	 *
755 756 757 758
	 *   . Report progress, if force_progress is 1 (ie. --progress).
	 *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
	 *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
	 *   . Report progress if isatty(2) is 1.
759
	 **/
760 761 762 763
	if (force_progress >= 0)
		transport->progress = !!force_progress;
	else
		transport->progress = verbosity >= 0 && isatty(2);
764 765
}

766 767 768 769 770 771 772 773
static void die_with_unpushed_submodules(struct string_list *needs_pushing)
{
	int i;

	fprintf(stderr, "The following submodule paths contain changes that can\n"
			"not be found on any remote:\n");
	for (i = 0; i < needs_pushing->nr; i++)
		printf("  %s\n", needs_pushing->items[i].string);
774 775 776 777 778
	fprintf(stderr, "\nPlease try\n\n"
			"	git push --recurse-submodules=on-demand\n\n"
			"or cd to the path and use\n\n"
			"	git push\n\n"
			"to push them to a remote.\n\n");
779 780 781 782 783 784

	string_list_clear(needs_pushing, 0);

	die("Aborting.");
}

785 786 787 788 789
static int run_pre_push_hook(struct transport *transport,
			     struct ref *remote_refs)
{
	int ret = 0, x;
	struct ref *r;
790
	struct child_process proc = CHILD_PROCESS_INIT;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	struct strbuf buf;
	const char *argv[4];

	if (!(argv[0] = find_hook("pre-push")))
		return 0;

	argv[1] = transport->remote->name;
	argv[2] = transport->url;
	argv[3] = NULL;

	proc.argv = argv;
	proc.in = -1;

	if (start_command(&proc)) {
		finish_command(&proc);
		return -1;
	}

809 810
	sigchain_push(SIGPIPE, SIG_IGN);

811 812 813 814 815
	strbuf_init(&buf, 256);

	for (r = remote_refs; r; r = r->next) {
		if (!r->peer_ref) continue;
		if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
816
		if (r->status == REF_STATUS_REJECT_STALE) continue;
817 818 819 820
		if (r->status == REF_STATUS_UPTODATE) continue;

		strbuf_reset(&buf);
		strbuf_addf( &buf, "%s %s %s %s\n",
821 822
			 r->peer_ref->name, oid_to_hex(&r->new_oid),
			 r->name, oid_to_hex(&r->old_oid));
823

824 825 826 827
		if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
			/* We do not mind if a hook does not read all refs. */
			if (errno != EPIPE)
				ret = -1;
828 829 830 831 832 833 834 835 836 837
			break;
		}
	}

	strbuf_release(&buf);

	x = close(proc.in);
	if (!ret)
		ret = x;

838 839
	sigchain_pop(SIGPIPE);

840 841 842 843 844 845 846
	x = finish_command(&proc);
	if (!ret)
		ret = x;

	return ret;
}

847
int transport_push(struct transport *transport,
848
		   int refspec_nr, const char **refspec, int flags,
849
		   unsigned int *reject_reasons)
850
{
851
	*reject_reasons = 0;
852
	transport_verify_remote_names(refspec_nr, refspec);
853

I
Ilari Liusvaara 已提交
854
	if (transport->push) {
I
Ilari Liusvaara 已提交
855 856 857 858
		/* Maybe FIXME. But no important transport uses this case. */
		if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
			die("This transport does not support using --set-upstream");

859
		return transport->push(transport, refspec_nr, refspec, flags);
I
Ilari Liusvaara 已提交
860
	} else if (transport->push_refs) {
861
		struct ref *remote_refs;
862 863
		struct ref *local_refs = get_local_heads();
		int match_flags = MATCH_REFS_NONE;
864 865
		int verbose = (transport->verbose > 0);
		int quiet = (transport->verbose < 0);
866
		int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
I
Ilari Liusvaara 已提交
867
		int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
868
		int push_ret, ret, err;
869

870 871 872 873 874
		if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
			return -1;

		remote_refs = transport->get_refs_list(transport, 1);

875 876 877 878
		if (flags & TRANSPORT_PUSH_ALL)
			match_flags |= MATCH_REFS_ALL;
		if (flags & TRANSPORT_PUSH_MIRROR)
			match_flags |= MATCH_REFS_MIRROR;
F
Felipe Contreras 已提交
879 880
		if (flags & TRANSPORT_PUSH_PRUNE)
			match_flags |= MATCH_REFS_PRUNE;
J
Junio C Hamano 已提交
881 882
		if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
			match_flags |= MATCH_REFS_FOLLOW_TAGS;
883

884 885
		if (match_push_refs(local_refs, &remote_refs,
				    refspec_nr, refspec, match_flags)) {
886 887 888
			return -1;
		}

889 890 891 892 893 894
		if (transport->smart_options &&
		    transport->smart_options->cas &&
		    !is_empty_cas(transport->smart_options->cas))
			apply_push_cas(transport->smart_options->cas,
				       transport->remote, remote_refs);

895 896 897 898
		set_ref_status_for_push(remote_refs,
			flags & TRANSPORT_PUSH_MIRROR,
			flags & TRANSPORT_PUSH_FORCE);

899 900 901 902
		if (!(flags & TRANSPORT_PUSH_NO_HOOK))
			if (run_pre_push_hook(transport, remote_refs))
				return -1;

903
		if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
904 905
			struct ref *ref = remote_refs;
			for (; ref; ref = ref->next)
906 907
				if (!is_null_oid(&ref->new_oid) &&
				    !push_unpushed_submodules(ref->new_oid.hash,
908 909 910 911 912 913
					    transport->remote->name))
				    die ("Failed to push all needed submodules!");
		}

		if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
			      TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
914
			struct ref *ref = remote_refs;
915
			struct string_list needs_pushing = STRING_LIST_INIT_DUP;
916

917
			for (; ref; ref = ref->next)
918 919
				if (!is_null_oid(&ref->new_oid) &&
				    find_unpushed_submodules(ref->new_oid.hash,
920 921
					    transport->remote->name, &needs_pushing))
					die_with_unpushed_submodules(&needs_pushing);
922 923
		}

924
		push_ret = transport->push_refs(transport, remote_refs, flags);
925
		err = push_had_errors(remote_refs);
926
		ret = push_ret | err;
927

928
		if (!quiet || err)
929
			transport_print_push_status(transport->url, remote_refs,
J
Junio C Hamano 已提交
930
					verbose | porcelain, porcelain,
931
					reject_reasons);
932

I
Ilari Liusvaara 已提交
933 934 935
		if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
			set_upstreams(transport, remote_refs, pretend);

936 937 938
		if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
			struct ref *ref;
			for (ref = remote_refs; ref; ref = ref->next)
939
				transport_update_tracking_ref(transport->remote, ref, verbose);
940 941
		}

942 943
		if (porcelain && !push_ret)
			puts("Done");
944
		else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
945
			fprintf(stderr, "Everything up-to-date\n");
946

947 948 949
		return ret;
	}
	return 1;
950 951
}

952
const struct ref *transport_get_remote_refs(struct transport *transport)
953
{
954
	if (!transport->got_remote_refs) {
955
		transport->remote_refs = transport->get_refs_list(transport, 0);
956 957
		transport->got_remote_refs = 1;
	}
I
Ilari Liusvaara 已提交
958

959 960 961
	return transport->remote_refs;
}

D
Daniel Barkalow 已提交
962
int transport_fetch_refs(struct transport *transport, struct ref *refs)
963
{
964
	int rc;
N
Nicolas Pitre 已提交
965
	int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
D
Daniel Barkalow 已提交
966 967
	struct ref **heads = NULL;
	struct ref *rm;
968 969

	for (rm = refs; rm; rm = rm->next) {
N
Nicolas Pitre 已提交
970
		nr_refs++;
971
		if (rm->peer_ref &&
972 973
		    !is_null_oid(&rm->old_oid) &&
		    !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
974
			continue;
975
		ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
976
		heads[nr_heads++] = rm;
977 978
	}

N
Nicolas Pitre 已提交
979 980 981 982 983 984
	if (!nr_heads) {
		/*
		 * When deepening of a shallow repository is requested,
		 * then local and remote refs are likely to still be equal.
		 * Just feed them all to the fetch method in that case.
		 * This condition shouldn't be met in a non-deepening fetch
985
		 * (see builtin/fetch.c:quickfetch()).
N
Nicolas Pitre 已提交
986
		 */
J
Jeff King 已提交
987
		ALLOC_ARRAY(heads, nr_refs);
N
Nicolas Pitre 已提交
988 989 990 991
		for (rm = refs; rm; rm = rm->next)
			heads[nr_heads++] = rm;
	}

992
	rc = transport->fetch(transport, nr_heads, heads);
I
Ilari Liusvaara 已提交
993

994
	free(heads);
995
	return rc;
996 997
}

998 999 1000
void transport_unlock_pack(struct transport *transport)
{
	if (transport->pack_lockfile) {
1001
		unlink_or_warn(transport->pack_lockfile);
1002 1003 1004 1005 1006
		free(transport->pack_lockfile);
		transport->pack_lockfile = NULL;
	}
}

1007 1008 1009 1010 1011 1012 1013 1014 1015
int transport_connect(struct transport *transport, const char *name,
		      const char *exec, int fd[2])
{
	if (transport->connect)
		return transport->connect(transport, name, exec, fd);
	else
		die("Operation not supported by protocol");
}

1016 1017 1018
int transport_disconnect(struct transport *transport)
{
	int ret = 0;
1019 1020
	if (transport->disconnect)
		ret = transport->disconnect(transport);
1021 1022 1023
	free(transport);
	return ret;
}
1024 1025

/*
J
Jim Meyering 已提交
1026
 * Strip username (and password) from a URL and return
1027 1028 1029 1030
 * it in a newly allocated string.
 */
char *transport_anonymize_url(const char *url)
{
J
Jeff King 已提交
1031
	char *scheme_prefix, *anon_part;
1032 1033 1034
	size_t anon_len, prefix_len = 0;

	anon_part = strchr(url, '@');
1035
	if (url_is_local_not_ssh(url) || !anon_part)
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
		goto literal_copy;

	anon_len = strlen(++anon_part);
	scheme_prefix = strstr(url, "://");
	if (!scheme_prefix) {
		if (!strchr(anon_part, ':'))
			/* cannot be "me@there:/path/name" */
			goto literal_copy;
	} else {
		const char *cp;
		/* make sure scheme is reasonable */
		for (cp = url; cp < scheme_prefix; cp++) {
			switch (*cp) {
				/* RFC 1738 2.1 */
			case '+': case '.': case '-':
				break; /* ok */
			default:
				if (isalnum(*cp))
					break;
				/* it isn't */
				goto literal_copy;
			}
		}
		/* @ past the first slash does not count */
		cp = strchr(scheme_prefix + 3, '/');
		if (cp && cp < anon_part)
			goto literal_copy;
		prefix_len = scheme_prefix - url + 3;
	}
J
Jeff King 已提交
1065 1066
	return xstrfmt("%.*s%.*s", (int)prefix_len, url,
		       (int)anon_len, anon_part);
1067 1068 1069
literal_copy:
	return xstrdup(url);
}
1070

1071 1072 1073 1074 1075 1076 1077
struct alternate_refs_data {
	alternate_ref_fn *fn;
	void *data;
};

static int refs_from_alternate_cb(struct alternate_object_database *e,
				  void *data)
1078 1079 1080 1081 1082 1083
{
	char *other;
	size_t len;
	struct remote *remote;
	struct transport *transport;
	const struct ref *extra;
1084
	struct alternate_refs_data *cb = data;
1085 1086

	e->name[-1] = '\0';
1087
	other = xstrdup(real_path(e->base));
1088 1089 1090 1091 1092 1093
	e->name[-1] = '/';
	len = strlen(other);

	while (other[len-1] == '/')
		other[--len] = '\0';
	if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1094
		goto out;
1095 1096 1097
	/* Is this a git repository with refs? */
	memcpy(other + len - 8, "/refs", 6);
	if (!is_directory(other))
1098
		goto out;
1099 1100 1101 1102 1103 1104
	other[len - 8] = '\0';
	remote = remote_get(other);
	transport = transport_get(remote, other);
	for (extra = transport_get_remote_refs(transport);
	     extra;
	     extra = extra->next)
1105
		cb->fn(extra, cb->data);
1106
	transport_disconnect(transport);
1107
out:
1108 1109 1110
	free(other);
	return 0;
}
1111 1112 1113 1114 1115 1116 1117 1118

void for_each_alternate_ref(alternate_ref_fn fn, void *data)
{
	struct alternate_refs_data cb;
	cb.fn = fn;
	cb.data = data;
	foreach_alt_odb(refs_from_alternate_cb, &cb);
}