transport->progress: use flag authoritatively
[git/git.git] / builtin-push.c
CommitLineData
755225de
LT
1/*
2 * "git push"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "run-command.h"
7#include "builtin.h"
5751f490 8#include "remote.h"
9b288516 9#include "transport.h"
378c4832 10#include "parse-options.h"
755225de 11
378c4832 12static const char * const push_usage[] = {
fcb044ee 13 "git push [<options>] [<repository> <refspec>...]",
378c4832
DB
14 NULL,
15};
755225de 16
c29c1b40 17static int thin;
f517f1f2 18static int deleterefs;
d23842fd 19static const char *receivepack;
8afd8dc0 20static int verbosity;
755225de 21
96f1e58f
DR
22static const char **refspec;
23static int refspec_nr;
755225de
LT
24
25static void add_refspec(const char *ref)
26{
27 int nr = refspec_nr + 1;
28 refspec = xrealloc(refspec, nr * sizeof(char *));
29 refspec[nr-1] = ref;
30 refspec_nr = nr;
31}
32
755225de
LT
33static void set_refspecs(const char **refs, int nr)
34{
8558fd9e
DB
35 int i;
36 for (i = 0; i < nr; i++) {
37 const char *ref = refs[i];
38 if (!strcmp("tag", ref)) {
39 char *tag;
40 int len;
41 if (nr <= ++i)
42 die("tag shorthand without <tag>");
43 len = strlen(refs[i]) + 11;
f517f1f2
JK
44 if (deleterefs) {
45 tag = xmalloc(len+1);
46 strcpy(tag, ":refs/tags/");
47 } else {
48 tag = xmalloc(len);
49 strcpy(tag, "refs/tags/");
50 }
8558fd9e
DB
51 strcat(tag, refs[i]);
52 ref = tag;
f517f1f2
JK
53 } else if (deleterefs && !strchr(ref, ':')) {
54 char *delref;
55 int len = strlen(ref)+1;
7b48c170 56 delref = xmalloc(len+1);
f517f1f2
JK
57 strcpy(delref, ":");
58 strcat(delref, ref);
59 ref = delref;
60 } else if (deleterefs)
61 die("--delete only accepts plain target ref names");
8558fd9e 62 add_refspec(ref);
755225de 63 }
755225de
LT
64}
65
52153747
FAG
66static void setup_push_tracking(void)
67{
68 struct strbuf refspec = STRBUF_INIT;
69 struct branch *branch = branch_get(NULL);
70 if (!branch)
71 die("You are not currently on a branch.");
72 if (!branch->merge_nr)
73 die("The current branch %s is not tracking anything.",
74 branch->name);
75 if (branch->merge_nr != 1)
76 die("The current branch %s is tracking multiple branches, "
77 "refusing to push.", branch->name);
78 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
79 add_refspec(refspec.buf);
80}
81
82static void setup_default_push_refspecs(void)
83{
52153747 84 switch (push_default) {
bba0fd22 85 default:
52153747
FAG
86 case PUSH_DEFAULT_MATCHING:
87 add_refspec(":");
88 break;
89
90 case PUSH_DEFAULT_TRACKING:
91 setup_push_tracking();
92 break;
93
94 case PUSH_DEFAULT_CURRENT:
95 add_refspec("HEAD");
96 break;
97
98 case PUSH_DEFAULT_NOTHING:
99 die("You didn't specify any refspecs to push, and "
100 "push.default is \"nothing\".");
101 break;
102 }
103}
104
fb0cc87e
DB
105static int push_with_options(struct transport *transport, int flags)
106{
107 int err;
108 int nonfastforward;
8afd8dc0 109
d01b3c02 110 transport_set_verbosity(transport, verbosity, 0);
8afd8dc0 111
fb0cc87e
DB
112 if (receivepack)
113 transport_set_option(transport,
114 TRANS_OPT_RECEIVEPACK, receivepack);
115 if (thin)
116 transport_set_option(transport, TRANS_OPT_THIN, "yes");
117
8afd8dc0 118 if (verbosity > 0)
fb0cc87e
DB
119 fprintf(stderr, "Pushing to %s\n", transport->url);
120 err = transport_push(transport, refspec_nr, refspec, flags,
121 &nonfastforward);
53970b92
TRC
122 if (err != 0)
123 error("failed to push some refs to '%s'", transport->url);
124
fb0cc87e
DB
125 err |= transport_disconnect(transport);
126
127 if (!err)
128 return 0;
129
fb0cc87e
DB
130 if (nonfastforward && advice_push_nonfastforward) {
131 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
c0eb6043
MM
132 "Merge the remote changes before pushing again. See the 'Note about\n"
133 "fast-forwards' section of 'git push --help' for details.\n");
fb0cc87e
DB
134 }
135
136 return 1;
137}
138
9b288516 139static int do_push(const char *repo, int flags)
755225de 140{
5751f490 141 int i, errs;
5751f490 142 struct remote *remote = remote_get(repo);
20346234
MG
143 const char **url;
144 int url_nr;
755225de 145
fa685bdf
DB
146 if (!remote) {
147 if (repo)
148 die("bad repository '%s'", repo);
149 die("No destination configured to push to.");
150 }
755225de 151
84bb2dfd
PB
152 if (remote->mirror)
153 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
154
b259f09b
MZ
155 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
156 if (!strcmp(*refspec, "refs/tags/*"))
157 return error("--all and --tags are incompatible");
158 return error("--all can't be combined with refspecs");
159 }
160
161 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
162 if (!strcmp(*refspec, "refs/tags/*"))
163 return error("--mirror and --tags are incompatible");
164 return error("--mirror can't be combined with refspecs");
165 }
84bb2dfd
PB
166
167 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
168 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
169 return error("--all and --mirror are incompatible");
170 }
171
52153747
FAG
172 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
173 if (remote->push_refspec_nr) {
174 refspec = remote->push_refspec;
175 refspec_nr = remote->push_refspec_nr;
176 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
177 setup_default_push_refspecs();
5751f490 178 }
fd1d1b05 179 errs = 0;
20346234
MG
180 if (remote->pushurl_nr) {
181 url = remote->pushurl;
182 url_nr = remote->pushurl_nr;
183 } else {
184 url = remote->url;
185 url_nr = remote->url_nr;
186 }
fb0cc87e
DB
187 if (url_nr) {
188 for (i = 0; i < url_nr; i++) {
189 struct transport *transport =
190 transport_get(remote, url[i]);
191 if (push_with_options(transport, flags))
192 errs++;
07436e43 193 }
fb0cc87e
DB
194 } else {
195 struct transport *transport =
196 transport_get(remote, NULL);
197
198 if (push_with_options(transport, flags))
199 errs++;
755225de 200 }
fd1d1b05 201 return !!errs;
755225de
LT
202}
203
a633fca0 204int cmd_push(int argc, const char **argv, const char *prefix)
755225de 205{
9b288516 206 int flags = 0;
378c4832 207 int tags = 0;
84bb2dfd 208 int rc;
5751f490 209 const char *repo = NULL; /* default repository */
378c4832 210 struct option options[] = {
8afd8dc0 211 OPT__VERBOSITY(&verbosity),
378c4832 212 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
c29c1b40
MB
213 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
214 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
215 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
f517f1f2 216 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
f740cc25 217 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
9f67fee2 218 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
1965ff74 219 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
c29c1b40 220 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
378c4832
DB
221 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
222 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
223 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
e9fcd1e2
IL
224 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
225 TRANSPORT_PUSH_SET_UPSTREAM),
378c4832
DB
226 OPT_END()
227 };
755225de 228
2aae905f 229 git_config(git_default_config, NULL);
37782920 230 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
378c4832 231
f517f1f2
JK
232 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
233 die("--delete is incompatible with --all, --mirror and --tags");
234 if (deleterefs && argc < 2)
235 die("--delete doesn't make sense without any refs");
236
378c4832
DB
237 if (tags)
238 add_refspec("refs/tags/*");
378c4832
DB
239
240 if (argc > 0) {
241 repo = argv[0];
242 set_refspecs(argv + 1, argc - 1);
755225de 243 }
8558fd9e 244
84bb2dfd
PB
245 rc = do_push(repo, flags);
246 if (rc == -1)
94c89ba6 247 usage_with_options(push_usage, options);
84bb2dfd
PB
248 else
249 return rc;
755225de 250}