t5800: clarify skip message
[git/dscho.git] / builtin / push.c
blob43758c604790a13fd37cb6eb18ba4edb4c40c59f
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 "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;
27 static int default_matching_used;
29 static void add_refspec(const char *ref)
31 refspec_nr++;
32 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
33 refspec[refspec_nr-1] = ref;
36 static void set_refspecs(const char **refs, int nr)
38 int i;
39 for (i = 0; i < nr; i++) {
40 const char *ref = refs[i];
41 if (!strcmp("tag", ref)) {
42 char *tag;
43 int len;
44 if (nr <= ++i)
45 die(_("tag shorthand without <tag>"));
46 len = strlen(refs[i]) + 11;
47 if (deleterefs) {
48 tag = xmalloc(len+1);
49 strcpy(tag, ":refs/tags/");
50 } else {
51 tag = xmalloc(len);
52 strcpy(tag, "refs/tags/");
54 strcat(tag, refs[i]);
55 ref = tag;
56 } else if (deleterefs && !strchr(ref, ':')) {
57 char *delref;
58 int len = strlen(ref)+1;
59 delref = xmalloc(len+1);
60 strcpy(delref, ":");
61 strcat(delref, ref);
62 ref = delref;
63 } else if (deleterefs)
64 die(_("--delete only accepts plain target ref names"));
65 add_refspec(ref);
69 static void setup_push_upstream(struct remote *remote)
71 struct strbuf refspec = STRBUF_INIT;
72 struct branch *branch = branch_get(NULL);
73 if (!branch)
74 die(_("You are not currently on a branch.\n"
75 "To push the history leading to the current (detached HEAD)\n"
76 "state now, use\n"
77 "\n"
78 " git push %s HEAD:<name-of-remote-branch>\n"),
79 remote->name);
80 if (!branch->merge_nr || !branch->merge)
81 die(_("The current branch %s has no upstream branch.\n"
82 "To push the current branch and set the remote as upstream, use\n"
83 "\n"
84 " git push --set-upstream %s %s\n"),
85 branch->name,
86 remote->name,
87 branch->name);
88 if (branch->merge_nr != 1)
89 die(_("The current branch %s has multiple upstream branches, "
90 "refusing to push."), branch->name);
91 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
92 add_refspec(refspec.buf);
95 static char warn_unspecified_push_default_msg[] =
96 N_("push.default is unset; its implicit value is changing in\n"
97 "Git 2.0 from 'matching' to 'upstream'. To squelch this message\n"
98 "and maintain the current behavior after the default changes, use:\n"
99 "\n"
100 " git config --global push.default matching\n"
101 "\n"
102 "To squelch this message and adopt the new behavior now, use:\n"
103 "\n"
104 " git config --global push.default upstream\n"
105 "\n"
106 "See 'git help config' and search for 'push.default' for further information.");
108 static void warn_unspecified_push_default_configuration(void)
110 static int warn_once;
112 if (warn_once++)
113 return;
114 warning("%s\n", _(warn_unspecified_push_default_msg));
117 static void setup_default_push_refspecs(struct remote *remote)
119 switch (push_default) {
120 default:
121 case PUSH_DEFAULT_UNSPECIFIED:
122 default_matching_used = 1;
123 warn_unspecified_push_default_configuration();
124 /* fallthru */
125 case PUSH_DEFAULT_MATCHING:
126 add_refspec(":");
127 break;
129 case PUSH_DEFAULT_UPSTREAM:
130 setup_push_upstream(remote);
131 break;
133 case PUSH_DEFAULT_CURRENT:
134 add_refspec("HEAD");
135 break;
137 case PUSH_DEFAULT_NOTHING:
138 die(_("You didn't specify any refspecs to push, and "
139 "push.default is \"nothing\"."));
140 break;
144 static const char message_advice_pull_before_push[] =
145 N_("Updates were rejected because the tip of your current branch is behind\n"
146 "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
147 "before pushing again.\n"
148 "See the 'Note about fast-forwards' in 'git push --help' for details.");
150 static const char message_advice_use_upstream[] =
151 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
152 "counterpart. If you did not intend to push that branch, you may want to\n"
153 "specify branches to push or set the 'push.default' configuration\n"
154 "variable to 'current' or 'upstream' to push only the current branch.");
156 static const char message_advice_checkout_pull_push[] =
157 N_("Updates were rejected because a pushed branch tip is behind its remote\n"
158 "counterpart. Check out this branch and merge the remote changes\n"
159 "(e.g. 'git pull') before pushing again.\n"
160 "See the 'Note about fast-forwards' in 'git push --help' for details.");
162 static void advise_pull_before_push(void)
164 if (!advice_push_non_ff_current || !advice_push_nonfastforward)
165 return;
166 advise(_(message_advice_pull_before_push));
169 static void advise_use_upstream(void)
171 if (!advice_push_non_ff_default || !advice_push_nonfastforward)
172 return;
173 advise(_(message_advice_use_upstream));
176 static void advise_checkout_pull_push(void)
178 if (!advice_push_non_ff_matching || !advice_push_nonfastforward)
179 return;
180 advise(_(message_advice_checkout_pull_push));
183 static int push_with_options(struct transport *transport, int flags)
185 int err;
186 int nonfastforward;
188 transport_set_verbosity(transport, verbosity, progress);
190 if (receivepack)
191 transport_set_option(transport,
192 TRANS_OPT_RECEIVEPACK, receivepack);
193 if (thin)
194 transport_set_option(transport, TRANS_OPT_THIN, "yes");
196 if (verbosity > 0)
197 fprintf(stderr, _("Pushing to %s\n"), transport->url);
198 err = transport_push(transport, refspec_nr, refspec, flags,
199 &nonfastforward);
200 if (err != 0)
201 error(_("failed to push some refs to '%s'"), transport->url);
203 err |= transport_disconnect(transport);
204 if (!err)
205 return 0;
207 switch (nonfastforward) {
208 default:
209 break;
210 case NON_FF_HEAD:
211 advise_pull_before_push();
212 break;
213 case NON_FF_OTHER:
214 if (default_matching_used)
215 advise_use_upstream();
216 else
217 advise_checkout_pull_push();
218 break;
221 return 1;
224 static int do_push(const char *repo, int flags)
226 int i, errs;
227 struct remote *remote = remote_get(repo);
228 const char **url;
229 int url_nr;
231 if (!remote) {
232 if (repo)
233 die(_("bad repository '%s'"), repo);
234 die(_("No configured push destination.\n"
235 "Either specify the URL from the command-line or configure a remote repository using\n"
236 "\n"
237 " git remote add <name> <url>\n"
238 "\n"
239 "and then push using the remote name\n"
240 "\n"
241 " git push <name>\n"));
244 if (remote->mirror)
245 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
247 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
248 if (!strcmp(*refspec, "refs/tags/*"))
249 return error(_("--all and --tags are incompatible"));
250 return error(_("--all can't be combined with refspecs"));
253 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
254 if (!strcmp(*refspec, "refs/tags/*"))
255 return error(_("--mirror and --tags are incompatible"));
256 return error(_("--mirror can't be combined with refspecs"));
259 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
260 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
261 return error(_("--all and --mirror are incompatible"));
264 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
265 if (remote->push_refspec_nr) {
266 refspec = remote->push_refspec;
267 refspec_nr = remote->push_refspec_nr;
268 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
269 setup_default_push_refspecs(remote);
271 errs = 0;
272 if (remote->pushurl_nr) {
273 url = remote->pushurl;
274 url_nr = remote->pushurl_nr;
275 } else {
276 url = remote->url;
277 url_nr = remote->url_nr;
279 if (url_nr) {
280 for (i = 0; i < url_nr; i++) {
281 struct transport *transport =
282 transport_get(remote, url[i]);
283 if (push_with_options(transport, flags))
284 errs++;
286 } else {
287 struct transport *transport =
288 transport_get(remote, NULL);
290 if (push_with_options(transport, flags))
291 errs++;
293 return !!errs;
296 static int option_parse_recurse_submodules(const struct option *opt,
297 const char *arg, int unset)
299 int *flags = opt->value;
300 if (arg) {
301 if (!strcmp(arg, "check"))
302 *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
303 else
304 die("bad %s argument: %s", opt->long_name, arg);
305 } else
306 die("option %s needs an argument (check)", opt->long_name);
308 return 0;
311 int cmd_push(int argc, const char **argv, const char *prefix)
313 int flags = 0;
314 int tags = 0;
315 int rc;
316 const char *repo = NULL; /* default repository */
317 struct option options[] = {
318 OPT__VERBOSITY(&verbosity),
319 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
320 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
321 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
322 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
323 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
324 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
325 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
326 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
327 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
328 { OPTION_CALLBACK, 0, "recurse-submodules", &flags, "check",
329 "controls recursive pushing of submodules",
330 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
331 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
332 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
333 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
334 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
335 TRANSPORT_PUSH_SET_UPSTREAM),
336 OPT_BOOL(0, "progress", &progress, "force progress reporting"),
337 OPT_BIT(0, "prune", &flags, "prune locally removed refs",
338 TRANSPORT_PUSH_PRUNE),
339 OPT_END()
342 packet_trace_identity("push");
343 git_config(git_default_config, NULL);
344 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
346 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
347 die(_("--delete is incompatible with --all, --mirror and --tags"));
348 if (deleterefs && argc < 2)
349 die(_("--delete doesn't make sense without any refs"));
351 if (tags)
352 add_refspec("refs/tags/*");
354 if (argc > 0) {
355 repo = argv[0];
356 set_refspecs(argv + 1, argc - 1);
359 rc = do_push(repo, flags);
360 if (rc == -1)
361 usage_with_options(push_usage, options);
362 else
363 return rc;