push: better error messages when push.default = tracking
[git.git] / builtin / push.c
blob1b493fb5a96219ddaa05bd6286a1d643f8b7d58d
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"
12 static const char * const push_usage[] = {
13 "git push [<options>] [<repository> [<refspec>...]]",
14 NULL,
17 static int thin;
18 static int deleterefs;
19 static const char *receivepack;
20 static int verbosity;
21 static int progress;
23 static const char **refspec;
24 static int refspec_nr;
25 static int refspec_alloc;
27 static void add_refspec(const char *ref)
29 refspec_nr++;
30 ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
31 refspec[refspec_nr-1] = ref;
34 static void set_refspecs(const char **refs, int nr)
36 int i;
37 for (i = 0; i < nr; i++) {
38 const char *ref = refs[i];
39 if (!strcmp("tag", ref)) {
40 char *tag;
41 int len;
42 if (nr <= ++i)
43 die("tag shorthand without <tag>");
44 len = strlen(refs[i]) + 11;
45 if (deleterefs) {
46 tag = xmalloc(len+1);
47 strcpy(tag, ":refs/tags/");
48 } else {
49 tag = xmalloc(len);
50 strcpy(tag, "refs/tags/");
52 strcat(tag, refs[i]);
53 ref = tag;
54 } else if (deleterefs && !strchr(ref, ':')) {
55 char *delref;
56 int len = strlen(ref)+1;
57 delref = xmalloc(len+1);
58 strcpy(delref, ":");
59 strcat(delref, ref);
60 ref = delref;
61 } else if (deleterefs)
62 die("--delete only accepts plain target ref names");
63 add_refspec(ref);
67 static void setup_push_upstream(struct remote *remote)
69 struct strbuf refspec = STRBUF_INIT;
70 struct branch *branch = branch_get(NULL);
71 if (!branch)
72 die("You are not currently on a branch.\n"
73 "To push the history leading to the current (detached HEAD)\n"
74 "state now, use\n"
75 "\n"
76 " git push %s HEAD:<name-of-remote-branch>\n",
77 remote->name);
78 if (!branch->merge_nr || !branch->merge)
79 die("The current branch %s has no upstream branch.\n"
80 "To push the current branch and set the remote as upstream, use\n"
81 "\n"
82 " git push --set-upstream %s %s\n",
83 branch->name,
84 remote->name,
85 branch->name);
86 if (branch->merge_nr != 1)
87 die("The current branch %s has multiple upstream branches, "
88 "refusing to push.", branch->name);
89 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
90 add_refspec(refspec.buf);
93 static void setup_default_push_refspecs(struct remote *remote)
95 switch (push_default) {
96 default:
97 case PUSH_DEFAULT_MATCHING:
98 add_refspec(":");
99 break;
101 case PUSH_DEFAULT_UPSTREAM:
102 setup_push_upstream(remote);
103 break;
105 case PUSH_DEFAULT_CURRENT:
106 add_refspec("HEAD");
107 break;
109 case PUSH_DEFAULT_NOTHING:
110 die("You didn't specify any refspecs to push, and "
111 "push.default is \"nothing\".");
112 break;
116 static int push_with_options(struct transport *transport, int flags)
118 int err;
119 int nonfastforward;
121 transport_set_verbosity(transport, verbosity, progress);
123 if (receivepack)
124 transport_set_option(transport,
125 TRANS_OPT_RECEIVEPACK, receivepack);
126 if (thin)
127 transport_set_option(transport, TRANS_OPT_THIN, "yes");
129 if (verbosity > 0)
130 fprintf(stderr, "Pushing to %s\n", transport->url);
131 err = transport_push(transport, refspec_nr, refspec, flags,
132 &nonfastforward);
133 if (err != 0)
134 error("failed to push some refs to '%s'", transport->url);
136 err |= transport_disconnect(transport);
138 if (!err)
139 return 0;
141 if (nonfastforward && advice_push_nonfastforward) {
142 fprintf(stderr, "To prevent you from losing history, non-fast-forward updates were rejected\n"
143 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
144 "'Note about fast-forwards' section of 'git push --help' for details.\n");
147 return 1;
150 static int do_push(const char *repo, int flags)
152 int i, errs;
153 struct remote *remote = remote_get(repo);
154 const char **url;
155 int url_nr;
157 if (!remote) {
158 if (repo)
159 die("bad repository '%s'", repo);
160 die("No destination configured to push to.");
163 if (remote->mirror)
164 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
166 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
167 if (!strcmp(*refspec, "refs/tags/*"))
168 return error("--all and --tags are incompatible");
169 return error("--all can't be combined with refspecs");
172 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
173 if (!strcmp(*refspec, "refs/tags/*"))
174 return error("--mirror and --tags are incompatible");
175 return error("--mirror can't be combined with refspecs");
178 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
179 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
180 return error("--all and --mirror are incompatible");
183 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
184 if (remote->push_refspec_nr) {
185 refspec = remote->push_refspec;
186 refspec_nr = remote->push_refspec_nr;
187 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
188 setup_default_push_refspecs(remote);
190 errs = 0;
191 if (remote->pushurl_nr) {
192 url = remote->pushurl;
193 url_nr = remote->pushurl_nr;
194 } else {
195 url = remote->url;
196 url_nr = remote->url_nr;
198 if (url_nr) {
199 for (i = 0; i < url_nr; i++) {
200 struct transport *transport =
201 transport_get(remote, url[i]);
202 if (push_with_options(transport, flags))
203 errs++;
205 } else {
206 struct transport *transport =
207 transport_get(remote, NULL);
209 if (push_with_options(transport, flags))
210 errs++;
212 return !!errs;
215 int cmd_push(int argc, const char **argv, const char *prefix)
217 int flags = 0;
218 int tags = 0;
219 int rc;
220 const char *repo = NULL; /* default repository */
221 struct option options[] = {
222 OPT__VERBOSITY(&verbosity),
223 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
224 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
225 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
226 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
227 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
228 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
229 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
230 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
231 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
232 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
233 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
234 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
235 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
236 TRANSPORT_PUSH_SET_UPSTREAM),
237 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
238 OPT_END()
241 git_config(git_default_config, NULL);
242 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
244 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
245 die("--delete is incompatible with --all, --mirror and --tags");
246 if (deleterefs && argc < 2)
247 die("--delete doesn't make sense without any refs");
249 if (tags)
250 add_refspec("refs/tags/*");
252 if (argc > 0) {
253 repo = argv[0];
254 set_refspecs(argv + 1, argc - 1);
257 rc = do_push(repo, flags);
258 if (rc == -1)
259 usage_with_options(push_usage, options);
260 else
261 return rc;