push.c 14.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * "git push"
 */
#include "cache.h"
#include "refs.h"
#include "run-command.h"
#include "builtin.h"
8
#include "remote.h"
9
#include "transport.h"
D
Daniel Barkalow 已提交
10
#include "parse-options.h"
11
#include "submodule.h"
L
Linus Torvalds 已提交
12

D
Daniel Barkalow 已提交
13
static const char * const push_usage[] = {
14
	N_("git push [<options>] [<repository> [<refspec>...]]"),
D
Daniel Barkalow 已提交
15 16
	NULL,
};
L
Linus Torvalds 已提交
17

18
static int thin;
19
static int deleterefs;
20
static const char *receivepack;
21
static int verbosity;
22
static int progress = -1;
L
Linus Torvalds 已提交
23

24 25
static const char **refspec;
static int refspec_nr;
26
static int refspec_alloc;
27
static int default_matching_used;
L
Linus Torvalds 已提交
28 29 30

static void add_refspec(const char *ref)
{
31 32 33
	refspec_nr++;
	ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
	refspec[refspec_nr-1] = ref;
L
Linus Torvalds 已提交
34 35 36 37
}

static void set_refspecs(const char **refs, int nr)
{
38 39 40 41 42 43 44
	int i;
	for (i = 0; i < nr; i++) {
		const char *ref = refs[i];
		if (!strcmp("tag", ref)) {
			char *tag;
			int len;
			if (nr <= ++i)
45
				die(_("tag shorthand without <tag>"));
46
			len = strlen(refs[i]) + 11;
47 48 49 50 51 52 53
			if (deleterefs) {
				tag = xmalloc(len+1);
				strcpy(tag, ":refs/tags/");
			} else {
				tag = xmalloc(len);
				strcpy(tag, "refs/tags/");
			}
54 55
			strcat(tag, refs[i]);
			ref = tag;
56 57 58
		} else if (deleterefs && !strchr(ref, ':')) {
			char *delref;
			int len = strlen(ref)+1;
J
Jeff King 已提交
59
			delref = xmalloc(len+1);
60 61 62 63
			strcpy(delref, ":");
			strcat(delref, ref);
			ref = delref;
		} else if (deleterefs)
64
			die(_("--delete only accepts plain target ref names"));
65
		add_refspec(ref);
L
Linus Torvalds 已提交
66 67 68
	}
}

69 70 71 72 73 74 75 76 77 78
static int push_url_of_remote(struct remote *remote, const char ***url_p)
{
	if (remote->pushurl_nr) {
		*url_p = remote->pushurl;
		return remote->pushurl_nr;
	}
	*url_p = remote->url;
	return remote->url_nr;
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
	/*
	 * There's no point in using shorten_unambiguous_ref here,
	 * as the ambiguity would be on the remote side, not what
	 * we have locally. Plus, this is supposed to be the simple
	 * mode. If the user is doing something crazy like setting
	 * upstream to a non-branch, we should probably be showing
	 * them the big ugly fully qualified ref.
	 */
	const char *advice_maybe = "";
	const char *short_upstream =
		skip_prefix(branch->merge[0]->src, "refs/heads/");

	if (!short_upstream)
		short_upstream = branch->merge[0]->src;
	/*
	 * Don't show advice for people who explicitely set
	 * push.default.
	 */
	if (push_default == PUSH_DEFAULT_UNSPECIFIED)
		advice_maybe = _("\n"
				 "To choose either option permanently, "
				 "see push.default in 'git help config'.");
	die(_("The upstream branch of your current branch does not match\n"
	      "the name of your current branch.  To push to the upstream branch\n"
	      "on the remote, use\n"
	      "\n"
	      "    git push %s HEAD:%s\n"
	      "\n"
	      "To push to the branch of the same name on the remote, use\n"
	      "\n"
	      "    git push %s %s\n"
	      "%s"),
	    remote->name, short_upstream,
	    remote->name, branch->name, advice_maybe);
}

116 117 118 119 120 121 122
static const char message_detached_head_die[] =
	N_("You are not currently on a branch.\n"
	   "To push the history leading to the current (detached HEAD)\n"
	   "state now, use\n"
	   "\n"
	   "    git push %s HEAD:<name-of-remote-branch>\n");

123
static void setup_push_upstream(struct remote *remote, int simple)
124 125 126 127
{
	struct strbuf refspec = STRBUF_INIT;
	struct branch *branch = branch_get(NULL);
	if (!branch)
128
		die(_(message_detached_head_die), remote->name);
129
	if (!branch->merge_nr || !branch->merge || !branch->remote_name)
J
Junio C Hamano 已提交
130
		die(_("The current branch %s has no upstream branch.\n"
131 132
		    "To push the current branch and set the remote as upstream, use\n"
		    "\n"
J
Junio C Hamano 已提交
133
		    "    git push --set-upstream %s %s\n"),
134 135
		    branch->name,
		    remote->name,
136 137
		    branch->name);
	if (branch->merge_nr != 1)
J
Junio C Hamano 已提交
138
		die(_("The current branch %s has multiple upstream branches, "
139
		    "refusing to push."), branch->name);
140 141 142 143 144
	if (strcmp(branch->remote_name, remote->name))
		die(_("You are pushing to remote '%s', which is not the upstream of\n"
		      "your current branch '%s', without telling me what to push\n"
		      "to update which remote branch."),
		    remote->name, branch->name);
145 146
	if (simple && strcmp(branch->refname, branch->merge[0]->src))
		die_push_simple(branch, remote);
147

148 149 150 151
	strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
	add_refspec(refspec.buf);
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static char warn_unspecified_push_default_msg[] =
N_("push.default is unset; its implicit value is changing in\n"
   "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
   "and maintain the current behavior after the default changes, use:\n"
   "\n"
   "  git config --global push.default matching\n"
   "\n"
   "To squelch this message and adopt the new behavior now, use:\n"
   "\n"
   "  git config --global push.default simple\n"
   "\n"
   "See 'git help config' and search for 'push.default' for further information.\n"
   "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
   "'current' instead of 'simple' if you sometimes use older versions of Git)");

static void warn_unspecified_push_default_configuration(void)
{
	static int warn_once;

	if (warn_once++)
		return;
	warning("%s\n", _(warn_unspecified_push_default_msg));
}

176
static void setup_default_push_refspecs(struct remote *remote)
177
{
178 179
	struct branch *branch;

180
	switch (push_default) {
181
	default:
182 183
	case PUSH_DEFAULT_UNSPECIFIED:
		default_matching_used = 1;
184
		warn_unspecified_push_default_configuration();
185
		/* fallthru */
186 187 188 189
	case PUSH_DEFAULT_MATCHING:
		add_refspec(":");
		break;

190 191 192 193
	case PUSH_DEFAULT_SIMPLE:
		setup_push_upstream(remote, 1);
		break;

194
	case PUSH_DEFAULT_UPSTREAM:
195
		setup_push_upstream(remote, 0);
196 197 198
		break;

	case PUSH_DEFAULT_CURRENT:
199 200 201
		branch = branch_get(NULL);
		if (!branch)
			die(_(message_detached_head_die), remote->name);
202
		add_refspec(branch->name);
203 204 205
		break;

	case PUSH_DEFAULT_NOTHING:
206 207
		die(_("You didn't specify any refspecs to push, and "
		    "push.default is \"nothing\"."));
208 209 210 211
		break;
	}
}

212 213 214 215 216 217 218 219 220
static const char message_advice_pull_before_push[] =
	N_("Updates were rejected because the tip of your current branch is behind\n"
	   "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
	   "before pushing again.\n"
	   "See the 'Note about fast-forwards' in 'git push --help' for details.");

static const char message_advice_use_upstream[] =
	N_("Updates were rejected because a pushed branch tip is behind its remote\n"
	   "counterpart. If you did not intend to push that branch, you may want to\n"
221 222
	   "specify branches to push or set the 'push.default' configuration variable\n"
	   "to 'simple', 'current' or 'upstream' to push only the current branch.");
223 224 225 226 227 228 229

static const char message_advice_checkout_pull_push[] =
	N_("Updates were rejected because a pushed branch tip is behind its remote\n"
	   "counterpart. Check out this branch and merge the remote changes\n"
	   "(e.g. 'git pull') before pushing again.\n"
	   "See the 'Note about fast-forwards' in 'git push --help' for details.");

230 231 232 233 234 235 236
static const char message_advice_ref_fetch_first[] =
	N_("Updates were rejected because the remote contains work that you do\n"
	   "not have locally. This is usually caused by another repository pushing\n"
	   "to the same ref. You may want to first merge the remote changes (e.g.,\n"
	   "'git pull') before pushing again.\n"
	   "See the 'Note about fast-forwards' in 'git push --help' for details.");

237
static const char message_advice_ref_already_exists[] =
238
	N_("Updates were rejected because the tag already exists in the remote.");
239

240 241 242 243
static const char message_advice_ref_needs_force[] =
	N_("You cannot update a remote ref that points at a non-commit object,\n"
	   "or update a remote ref to make it point at a non-commit object,\n"
	   "without using the '--force' option.\n");
244

245 246
static void advise_pull_before_push(void)
{
247
	if (!advice_push_non_ff_current || !advice_push_update_rejected)
248 249 250 251 252 253
		return;
	advise(_(message_advice_pull_before_push));
}

static void advise_use_upstream(void)
{
254
	if (!advice_push_non_ff_default || !advice_push_update_rejected)
255 256 257 258 259 260
		return;
	advise(_(message_advice_use_upstream));
}

static void advise_checkout_pull_push(void)
{
261
	if (!advice_push_non_ff_matching || !advice_push_update_rejected)
262 263 264 265
		return;
	advise(_(message_advice_checkout_pull_push));
}

266 267
static void advise_ref_already_exists(void)
{
268 269
	if (!advice_push_already_exists || !advice_push_update_rejected)
		return;
270 271 272
	advise(_(message_advice_ref_already_exists));
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286
static void advise_ref_fetch_first(void)
{
	if (!advice_push_fetch_first || !advice_push_update_rejected)
		return;
	advise(_(message_advice_ref_fetch_first));
}

static void advise_ref_needs_force(void)
{
	if (!advice_push_needs_force || !advice_push_update_rejected)
		return;
	advise(_(message_advice_ref_needs_force));
}

287 288 289
static int push_with_options(struct transport *transport, int flags)
{
	int err;
290
	unsigned int reject_reasons;
291

T
Tay Ray Chuan 已提交
292
	transport_set_verbosity(transport, verbosity, progress);
293

294 295 296 297 298 299
	if (receivepack)
		transport_set_option(transport,
				     TRANS_OPT_RECEIVEPACK, receivepack);
	if (thin)
		transport_set_option(transport, TRANS_OPT_THIN, "yes");

300
	if (verbosity > 0)
301
		fprintf(stderr, _("Pushing to %s\n"), transport->url);
302
	err = transport_push(transport, refspec_nr, refspec, flags,
303
			     &reject_reasons);
304
	if (err != 0)
305
		error(_("failed to push some refs to '%s'"), transport->url);
306

307 308 309 310
	err |= transport_disconnect(transport);
	if (!err)
		return 0;

311
	if (reject_reasons & REJECT_NON_FF_HEAD) {
312
		advise_pull_before_push();
313
	} else if (reject_reasons & REJECT_NON_FF_OTHER) {
314 315 316 317
		if (default_matching_used)
			advise_use_upstream();
		else
			advise_checkout_pull_push();
318 319
	} else if (reject_reasons & REJECT_ALREADY_EXISTS) {
		advise_ref_already_exists();
320 321 322 323
	} else if (reject_reasons & REJECT_FETCH_FIRST) {
		advise_ref_fetch_first();
	} else if (reject_reasons & REJECT_NEEDS_FORCE) {
		advise_ref_needs_force();
324 325 326 327 328
	}

	return 1;
}

329
static int do_push(const char *repo, int flags)
L
Linus Torvalds 已提交
330
{
331
	int i, errs;
332
	struct remote *remote = pushremote_get(repo);
333 334
	const char **url;
	int url_nr;
L
Linus Torvalds 已提交
335

336 337
	if (!remote) {
		if (repo)
338
			die(_("bad repository '%s'"), repo);
J
Junio C Hamano 已提交
339
		die(_("No configured push destination.\n"
340 341 342 343 344 345
		    "Either specify the URL from the command-line or configure a remote repository using\n"
		    "\n"
		    "    git remote add <name> <url>\n"
		    "\n"
		    "and then push using the remote name\n"
		    "\n"
J
Junio C Hamano 已提交
346
		    "    git push <name>\n"));
347
	}
L
Linus Torvalds 已提交
348

349 350 351
	if (remote->mirror)
		flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);

352 353
	if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
		if (!strcmp(*refspec, "refs/tags/*"))
354 355
			return error(_("--all and --tags are incompatible"));
		return error(_("--all can't be combined with refspecs"));
356 357 358 359
	}

	if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
		if (!strcmp(*refspec, "refs/tags/*"))
360 361
			return error(_("--mirror and --tags are incompatible"));
		return error(_("--mirror can't be combined with refspecs"));
362
	}
363 364 365

	if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
				(TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
366
		return error(_("--all and --mirror are incompatible"));
367 368
	}

369 370 371 372 373
	if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
		if (remote->push_refspec_nr) {
			refspec = remote->push_refspec;
			refspec_nr = remote->push_refspec_nr;
		} else if (!(flags & TRANSPORT_PUSH_MIRROR))
374
			setup_default_push_refspecs(remote);
375
	}
376
	errs = 0;
377
	url_nr = push_url_of_remote(remote, &url);
378 379 380 381 382 383
	if (url_nr) {
		for (i = 0; i < url_nr; i++) {
			struct transport *transport =
				transport_get(remote, url[i]);
			if (push_with_options(transport, flags))
				errs++;
384
		}
385 386 387 388 389 390
	} else {
		struct transport *transport =
			transport_get(remote, NULL);

		if (push_with_options(transport, flags))
			errs++;
L
Linus Torvalds 已提交
391
	}
392
	return !!errs;
L
Linus Torvalds 已提交
393 394
}

395 396 397 398
static int option_parse_recurse_submodules(const struct option *opt,
				   const char *arg, int unset)
{
	int *flags = opt->value;
399 400 401 402 403

	if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
		      TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
		die("%s can only be used once.", opt->long_name);

404 405 406
	if (arg) {
		if (!strcmp(arg, "check"))
			*flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
407 408
		else if (!strcmp(arg, "on-demand"))
			*flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
409 410 411
		else
			die("bad %s argument: %s", opt->long_name, arg);
	} else
412 413
		die("option %s needs an argument (check|on-demand)",
				opt->long_name);
414 415 416 417

	return 0;
}

418
int cmd_push(int argc, const char **argv, const char *prefix)
L
Linus Torvalds 已提交
419
{
420
	int flags = 0;
D
Daniel Barkalow 已提交
421
	int tags = 0;
422
	int rc;
423
	const char *repo = NULL;	/* default repository */
D
Daniel Barkalow 已提交
424
	struct option options[] = {
425
		OPT__VERBOSITY(&verbosity),
426 427 428
		OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
		OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
		OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
429
			    (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
430 431
		OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
		OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
432 433 434 435
		OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
		OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
		OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
		{ OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
436
			N_("control recursive pushing of submodules"),
437
			PARSE_OPT_OPTARG, option_parse_recurse_submodules },
438
		OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
439 440 441
		OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
		OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
		OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
I
Ilari Liusvaara 已提交
442
			TRANSPORT_PUSH_SET_UPSTREAM),
443 444
		OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
		OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
F
Felipe Contreras 已提交
445
			TRANSPORT_PUSH_PRUNE),
446
		OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
J
Junio C Hamano 已提交
447 448
		OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
			TRANSPORT_PUSH_FOLLOW_TAGS),
D
Daniel Barkalow 已提交
449 450
		OPT_END()
	};
L
Linus Torvalds 已提交
451

J
Jeff King 已提交
452
	packet_trace_identity("push");
J
Jeff King 已提交
453
	git_config(git_default_config, NULL);
454
	argc = parse_options(argc, argv, prefix, options, push_usage, 0);
D
Daniel Barkalow 已提交
455

456
	if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
457
		die(_("--delete is incompatible with --all, --mirror and --tags"));
458
	if (deleterefs && argc < 2)
459
		die(_("--delete doesn't make sense without any refs"));
460

D
Daniel Barkalow 已提交
461 462 463 464 465 466
	if (tags)
		add_refspec("refs/tags/*");

	if (argc > 0) {
		repo = argv[0];
		set_refspecs(argv + 1, argc - 1);
L
Linus Torvalds 已提交
467
	}
468

469 470
	rc = do_push(repo, flags);
	if (rc == -1)
A
Andy Whitcroft 已提交
471
		usage_with_options(push_usage, options);
472 473
	else
		return rc;
L
Linus Torvalds 已提交
474
}