6 #include "run-command.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage[] = {
14 "git push [<options>] [<repository> [<refspec>...]]",
19 static int deleterefs;
20 static const char *receivepack;
22 static int progress = -1;
24 static const char **refspec;
25 static int refspec_nr;
26 static int refspec_alloc;
27 static int default_matching_used;
29 static void add_refspec(const char *ref)
32 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
33 refspec[refspec_nr-1] = ref;
36 static void set_refspecs(const char **refs, int nr)
39 for (i = 0; i < nr; i++) {
40 const char *ref = refs[i];
41 if (!strcmp("tag", ref)) {
45 die(_("tag shorthand without <tag>"));
46 len = strlen(refs[i]) + 11;
49 strcpy(tag, ":refs/tags/");
52 strcpy(tag, "refs/tags/");
56 } else if (deleterefs && !strchr(ref, ':')) {
58 int len = strlen(ref)+1;
59 delref = xmalloc(len+1);
63 } else if (deleterefs)
64 die(_("--delete only accepts plain target ref names"));
69 static int push_url_of_remote(struct remote *remote, const char ***url_p)
71 if (remote->pushurl_nr) {
72 *url_p = remote->pushurl;
73 return remote->pushurl_nr;
76 return remote->url_nr;
79 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
81 * There's no point in using shorten_unambiguous_ref here,
82 * as the ambiguity would be on the remote side, not what
83 * we have locally. Plus, this is supposed to be the simple
84 * mode. If the user is doing something crazy like setting
85 * upstream to a non-branch, we should probably be showing
86 * them the big ugly fully qualified ref.
88 const char *advice_maybe = "";
89 const char *short_upstream =
90 skip_prefix(branch->merge[0]->src, "refs/heads/");
93 short_upstream = branch->merge[0]->src;
95 * Don't show advice for people who explicitely set
98 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
100 "To choose either option permanently, "
101 "see push.default in 'git help config'.");
102 die(_("The upstream branch of your current branch does not match\n"
103 "the name of your current branch. To push to the upstream branch\n"
104 "on the remote, use\n"
106 " git push %s HEAD:%s\n"
108 "To push to the branch of the same name on the remote, use\n"
112 remote->name, short_upstream,
113 remote->name, branch->name, advice_maybe);
116 static void setup_push_upstream(struct remote *remote, int simple)
118 struct strbuf refspec = STRBUF_INIT;
119 struct branch *branch = branch_get(NULL);
121 die(_("You are not currently on a branch.\n"
122 "To push the history leading to the current (detached HEAD)\n"
125 " git push %s HEAD:<name-of-remote-branch>\n"),
127 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
128 die(_("The current branch %s has no upstream branch.\n"
129 "To push the current branch and set the remote as upstream, use\n"
131 " git push --set-upstream %s %s\n"),
135 if (branch->merge_nr != 1)
136 die(_("The current branch %s has multiple upstream branches, "
137 "refusing to push."), branch->name);
138 if (strcmp(branch->remote_name, remote->name))
139 die(_("You are pushing to remote '%s', which is not the upstream of\n"
140 "your current branch '%s', without telling me what to push\n"
141 "to update which remote branch."),
142 remote->name, branch->name);
143 if (simple && strcmp(branch->refname, branch->merge[0]->src))
144 die_push_simple(branch, remote);
146 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
147 add_refspec(refspec.buf);
150 static void setup_default_push_refspecs(struct remote *remote)
152 switch (push_default) {
154 case PUSH_DEFAULT_UNSPECIFIED:
155 default_matching_used = 1;
157 case PUSH_DEFAULT_MATCHING:
161 case PUSH_DEFAULT_SIMPLE:
162 setup_push_upstream(remote, 1);
165 case PUSH_DEFAULT_UPSTREAM:
166 setup_push_upstream(remote, 0);
169 case PUSH_DEFAULT_CURRENT:
173 case PUSH_DEFAULT_NOTHING:
174 die(_("You didn't specify any refspecs to push, and "
175 "push.default is \"nothing\"."));
180 static const char message_advice_pull_before_push[] =
181 N_("Updates were rejected because the tip of your current branch is behind\n"
182 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
183 "before pushing again.\n"
184 "See the 'Note about fast-forwards' in 'git push --help' for details.");
186 static const char message_advice_use_upstream[] =
187 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
188 "counterpart. If you did not intend to push that branch, you may want to\n"
189 "specify branches to push or set the 'push.default' configuration\n"
190 "variable to 'current' or 'upstream' to push only the current branch.");
192 static const char message_advice_checkout_pull_push[] =
193 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
194 "counterpart. Check out this branch and merge the remote changes\n"
195 "(e.g. 'git pull') before pushing again.\n"
196 "See the 'Note about fast-forwards' in 'git push --help' for details.");
198 static void advise_pull_before_push(void)
200 if (!advice_push_non_ff_current || !advice_push_nonfastforward)
202 advise(_(message_advice_pull_before_push));
205 static void advise_use_upstream(void)
207 if (!advice_push_non_ff_default || !advice_push_nonfastforward)
209 advise(_(message_advice_use_upstream));
212 static void advise_checkout_pull_push(void)
214 if (!advice_push_non_ff_matching || !advice_push_nonfastforward)
216 advise(_(message_advice_checkout_pull_push));
219 static int push_with_options(struct transport *transport, int flags)
224 transport_set_verbosity(transport, verbosity, progress);
227 transport_set_option(transport,
228 TRANS_OPT_RECEIVEPACK, receivepack);
230 transport_set_option(transport, TRANS_OPT_THIN, "yes");
233 fprintf(stderr, _("Pushing to %s\n"), transport->url);
234 err = transport_push(transport, refspec_nr, refspec, flags,
237 error(_("failed to push some refs to '%s'"), transport->url);
239 err |= transport_disconnect(transport);
243 switch (nonfastforward) {
247 advise_pull_before_push();
250 if (default_matching_used)
251 advise_use_upstream();
253 advise_checkout_pull_push();
260 static int do_push(const char *repo, int flags)
263 struct remote *remote = remote_get(repo);
269 die(_("bad repository '%s'"), repo);
270 die(_("No configured push destination.\n"
271 "Either specify the URL from the command-line or configure a remote repository using\n"
273 " git remote add <name> <url>\n"
275 "and then push using the remote name\n"
277 " git push <name>\n"));
281 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
283 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
284 if (!strcmp(*refspec, "refs/tags/*"))
285 return error(_("--all and --tags are incompatible"));
286 return error(_("--all can't be combined with refspecs"));
289 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
290 if (!strcmp(*refspec, "refs/tags/*"))
291 return error(_("--mirror and --tags are incompatible"));
292 return error(_("--mirror can't be combined with refspecs"));
295 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
296 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
297 return error(_("--all and --mirror are incompatible"));
300 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
301 if (remote->push_refspec_nr) {
302 refspec = remote->push_refspec;
303 refspec_nr = remote->push_refspec_nr;
304 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
305 setup_default_push_refspecs(remote);
308 url_nr = push_url_of_remote(remote, &url);
310 for (i = 0; i < url_nr; i++) {
311 struct transport *transport =
312 transport_get(remote, url[i]);
313 if (push_with_options(transport, flags))
317 struct transport *transport =
318 transport_get(remote, NULL);
320 if (push_with_options(transport, flags))
326 static int option_parse_recurse_submodules(const struct option *opt,
327 const char *arg, int unset)
329 int *flags = opt->value;
331 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
332 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
333 die("%s can only be used once.", opt->long_name);
336 if (!strcmp(arg, "check"))
337 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
338 else if (!strcmp(arg, "on-demand"))
339 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
341 die("bad %s argument: %s", opt->long_name, arg);
343 die("option %s needs an argument (check|on-demand)",
349 int cmd_push(int argc, const char **argv, const char *prefix)
354 const char *repo = NULL; /* default repository */
355 struct option options[] = {
356 OPT__VERBOSITY(&verbosity),
357 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
358 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
359 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
360 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
361 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
362 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
363 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
364 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
365 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
366 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check",
367 "controls recursive pushing of submodules",
368 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
369 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
370 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
371 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
372 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
373 TRANSPORT_PUSH_SET_UPSTREAM),
374 OPT_BOOL(0, "progress", &progress, "force progress reporting"),
375 OPT_BIT(0, "prune", &flags, "prune locally removed refs",
376 TRANSPORT_PUSH_PRUNE),
380 packet_trace_identity("push");
381 git_config(git_default_config, NULL);
382 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
384 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
385 die(_("--delete is incompatible with --all, --mirror and --tags"));
386 if (deleterefs && argc < 2)
387 die(_("--delete doesn't make sense without any refs"));
390 add_refspec("refs/tags/*");
394 set_refspecs(argv + 1, argc - 1);
397 rc = do_push(repo, flags);
399 usage_with_options(push_usage, options);