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