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