Merge branch 'rs/pretty-use-prefixcmp' into next
[git/mjg.git] / builtin / push.c
blob0126914b4794a9d5597e980b63c027bb6c061f56
1 /*
2 * "git push"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
8 #include "remote.h"
9 #include "transport.h"
10 #include "parse-options.h"
11 #include "submodule.h"
13 static const char * const push_usage[] = {
14 N_("git push [<options>] [<repository> [<refspec>...]]"),
15 NULL,
18 static int thin;
19 static int deleterefs;
20 static const char *receivepack;
21 static int verbosity;
22 static int progress = -1;
24 static const char **refspec;
25 static int refspec_nr;
26 static int refspec_alloc;
28 static void add_refspec(const char *ref)
30 refspec_nr++;
31 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
32 refspec[refspec_nr-1] = ref;
35 static void set_refspecs(const char **refs, int nr)
37 int i;
38 for (i = 0; i < nr; i++) {
39 const char *ref = refs[i];
40 if (!strcmp("tag", ref)) {
41 char *tag;
42 int len;
43 if (nr <= ++i)
44 die(_("tag shorthand without <tag>"));
45 len = strlen(refs[i]) + 11;
46 if (deleterefs) {
47 tag = xmalloc(len+1);
48 strcpy(tag, ":refs/tags/");
49 } else {
50 tag = xmalloc(len);
51 strcpy(tag, "refs/tags/");
53 strcat(tag, refs[i]);
54 ref = tag;
55 } else if (deleterefs && !strchr(ref, ':')) {
56 char *delref;
57 int len = strlen(ref)+1;
58 delref = xmalloc(len+1);
59 strcpy(delref, ":");
60 strcat(delref, ref);
61 ref = delref;
62 } else if (deleterefs)
63 die(_("--delete only accepts plain target ref names"));
64 add_refspec(ref);
68 static int push_url_of_remote(struct remote *remote, const char ***url_p)
70 if (remote->pushurl_nr) {
71 *url_p = remote->pushurl;
72 return remote->pushurl_nr;
74 *url_p = remote->url;
75 return remote->url_nr;
78 static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
80 * There's no point in using shorten_unambiguous_ref here,
81 * as the ambiguity would be on the remote side, not what
82 * we have locally. Plus, this is supposed to be the simple
83 * mode. If the user is doing something crazy like setting
84 * upstream to a non-branch, we should probably be showing
85 * them the big ugly fully qualified ref.
87 const char *advice_maybe = "";
88 const char *short_upstream =
89 skip_prefix(branch->merge[0]->src, "refs/heads/");
91 if (!short_upstream)
92 short_upstream = branch->merge[0]->src;
94 * Don't show advice for people who explicitely set
95 * push.default.
97 if (push_default == PUSH_DEFAULT_UNSPECIFIED)
98 advice_maybe = _("\n"
99 "To choose either option permanently, "
100 "see push.default in 'git help config'.");
101 die(_("The upstream branch of your current branch does not match\n"
102 "the name of your current branch. To push to the upstream branch\n"
103 "on the remote, use\n"
104 "\n"
105 " git push %s HEAD:%s\n"
106 "\n"
107 "To push to the branch of the same name on the remote, use\n"
108 "\n"
109 " git push %s %s\n"
110 "%s"),
111 remote->name, short_upstream,
112 remote->name, branch->name, advice_maybe);
115 static void setup_push_upstream(struct remote *remote, int simple)
117 struct strbuf refspec = STRBUF_INIT;
118 struct branch *branch = branch_get(NULL);
119 if (!branch)
120 die(_("You are not currently on a branch.\n"
121 "To push the history leading to the current (detached HEAD)\n"
122 "state now, use\n"
123 "\n"
124 " git push %s HEAD:<name-of-remote-branch>\n"),
125 remote->name);
126 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
127 die(_("The current branch %s has no upstream branch.\n"
128 "To push the current branch and set the remote as upstream, use\n"
129 "\n"
130 " git push --set-upstream %s %s\n"),
131 branch->name,
132 remote->name,
133 branch->name);
134 if (branch->merge_nr != 1)
135 die(_("The current branch %s has multiple upstream branches, "
136 "refusing to push."), branch->name);
137 if (strcmp(branch->remote_name, remote->name))
138 die(_("You are pushing to remote '%s', which is not the upstream of\n"
139 "your current branch '%s', without telling me what to push\n"
140 "to update which remote branch."),
141 remote->name, branch->name);
142 if (simple && strcmp(branch->refname, branch->merge[0]->src))
143 die_push_simple(branch, remote);
145 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
146 add_refspec(refspec.buf);
149 static char warn_unspecified_push_default_msg[] =
150 N_("push.default is unset; its implicit value has changed in\n"
151 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
152 "and maintain the traditional behavior, use:\n"
153 "\n"
154 " git config --global push.default matching\n"
155 "\n"
156 "To squelch this message and adopt the new behavior now, use:\n"
157 "\n"
158 " git config --global push.default simple\n"
159 "\n"
160 "See 'git help config' and search for 'push.default' for further information.\n"
161 "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
162 "'current' instead of 'simple' if you sometimes use older versions of Git)");
164 static void warn_unspecified_push_default_configuration(void)
166 static int warn_once;
168 if (warn_once++)
169 return;
170 warning("%s\n", _(warn_unspecified_push_default_msg));
173 static void setup_default_push_refspecs(struct remote *remote)
175 switch (push_default) {
176 default:
177 case PUSH_DEFAULT_MATCHING:
178 add_refspec(":");
179 break;
181 case PUSH_DEFAULT_UNSPECIFIED:
182 warn_unspecified_push_default_configuration();
183 /* fallthru */
185 case PUSH_DEFAULT_SIMPLE:
186 setup_push_upstream(remote, 1);
187 break;
189 case PUSH_DEFAULT_UPSTREAM:
190 setup_push_upstream(remote, 0);
191 break;
193 case PUSH_DEFAULT_CURRENT:
194 add_refspec("HEAD");
195 break;
197 case PUSH_DEFAULT_NOTHING:
198 die(_("You didn't specify any refspecs to push, and "
199 "push.default is \"nothing\"."));
200 break;
204 static const char message_advice_pull_before_push[] =
205 N_("Updates were rejected because the tip of your current branch is behind\n"
206 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
207 "before pushing again.\n"
208 "See the 'Note about fast-forwards' in 'git push --help' for details.");
210 static const char message_advice_checkout_pull_push[] =
211 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
212 "counterpart. Check out this branch and merge the remote changes\n"
213 "(e.g. 'git pull') before pushing again.\n"
214 "See the 'Note about fast-forwards' in 'git push --help' for details.");
216 static const char message_advice_ref_already_exists[] =
217 N_("Updates were rejected because the destination reference already exists\n"
218 "in the remote.");
220 static void advise_pull_before_push(void)
222 if (!advice_push_non_ff_current || !advice_push_update_rejected)
223 return;
224 advise(_(message_advice_pull_before_push));
227 static void advise_checkout_pull_push(void)
229 if (!advice_push_non_ff_matching || !advice_push_update_rejected)
230 return;
231 advise(_(message_advice_checkout_pull_push));
234 static void advise_ref_already_exists(void)
236 if (!advice_push_already_exists || !advice_push_update_rejected)
237 return;
238 advise(_(message_advice_ref_already_exists));
241 static int push_with_options(struct transport *transport, int flags)
243 int err;
244 unsigned int reject_reasons;
246 transport_set_verbosity(transport, verbosity, progress);
248 if (receivepack)
249 transport_set_option(transport,
250 TRANS_OPT_RECEIVEPACK, receivepack);
251 if (thin)
252 transport_set_option(transport, TRANS_OPT_THIN, "yes");
254 if (verbosity > 0)
255 fprintf(stderr, _("Pushing to %s\n"), transport->url);
256 err = transport_push(transport, refspec_nr, refspec, flags,
257 &reject_reasons);
258 if (err != 0)
259 error(_("failed to push some refs to '%s'"), transport->url);
261 err |= transport_disconnect(transport);
262 if (!err)
263 return 0;
265 if (reject_reasons & REJECT_NON_FF_HEAD) {
266 advise_pull_before_push();
267 } else if (reject_reasons & REJECT_NON_FF_OTHER) {
268 advise_checkout_pull_push();
269 } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
270 advise_ref_already_exists();
273 return 1;
276 static int do_push(const char *repo, int flags)
278 int i, errs;
279 struct remote *remote = remote_get(repo);
280 const char **url;
281 int url_nr;
283 if (!remote) {
284 if (repo)
285 die(_("bad repository '%s'"), repo);
286 die(_("No configured push destination.\n"
287 "Either specify the URL from the command-line or configure a remote repository using\n"
288 "\n"
289 " git remote add <name> <url>\n"
290 "\n"
291 "and then push using the remote name\n"
292 "\n"
293 " git push <name>\n"));
296 if (remote->mirror)
297 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
299 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
300 if (!strcmp(*refspec, "refs/tags/*"))
301 return error(_("--all and --tags are incompatible"));
302 return error(_("--all can't be combined with refspecs"));
305 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
306 if (!strcmp(*refspec, "refs/tags/*"))
307 return error(_("--mirror and --tags are incompatible"));
308 return error(_("--mirror can't be combined with refspecs"));
311 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
312 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
313 return error(_("--all and --mirror are incompatible"));
316 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
317 if (remote->push_refspec_nr) {
318 refspec = remote->push_refspec;
319 refspec_nr = remote->push_refspec_nr;
320 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
321 setup_default_push_refspecs(remote);
323 errs = 0;
324 url_nr = push_url_of_remote(remote, &url);
325 if (url_nr) {
326 for (i = 0; i < url_nr; i++) {
327 struct transport *transport =
328 transport_get(remote, url[i]);
329 if (push_with_options(transport, flags))
330 errs++;
332 } else {
333 struct transport *transport =
334 transport_get(remote, NULL);
336 if (push_with_options(transport, flags))
337 errs++;
339 return !!errs;
342 static int option_parse_recurse_submodules(const struct option *opt,
343 const char *arg, int unset)
345 int *flags = opt->value;
347 if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
348 TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
349 die("%s can only be used once.", opt->long_name);
351 if (arg) {
352 if (!strcmp(arg, "check"))
353 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
354 else if (!strcmp(arg, "on-demand"))
355 *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
356 else
357 die("bad %s argument: %s", opt->long_name, arg);
358 } else
359 die("option %s needs an argument (check|on-demand)",
360 opt->long_name);
362 return 0;
365 int cmd_push(int argc, const char **argv, const char *prefix)
367 int flags = 0;
368 int tags = 0;
369 int rc;
370 const char *repo = NULL; /* default repository */
371 struct option options[] = {
372 OPT__VERBOSITY(&verbosity),
373 OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
374 OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
375 OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
376 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
377 OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")),
378 OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
379 OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
380 OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
381 OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
382 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
383 N_("control recursive pushing of submodules"),
384 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
385 OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")),
386 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
387 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
388 OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
389 TRANSPORT_PUSH_SET_UPSTREAM),
390 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
391 OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
392 TRANSPORT_PUSH_PRUNE),
393 OPT_END()
396 packet_trace_identity("push");
397 git_config(git_default_config, NULL);
398 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
400 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
401 die(_("--delete is incompatible with --all, --mirror and --tags"));
402 if (deleterefs && argc < 2)
403 die(_("--delete doesn't make sense without any refs"));
405 if (tags)
406 add_refspec("refs/tags/*");
408 if (argc > 0) {
409 repo = argv[0];
410 set_refspecs(argv + 1, argc - 1);
413 rc = do_push(repo, flags);
414 if (rc == -1)
415 usage_with_options(push_usage, options);
416 else
417 return rc;