]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/push.c
Merge branch 'jh/push-default-upstream-configname'
[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)
43 die("tag shorthand without <tag>");
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)
62 die("--delete only accepts plain target ref names");
8558fd9e 63 add_refspec(ref);
755225de 64 }
755225de
LT
65}
66
53c40311 67static void setup_push_upstream(void)
52153747
FAG
68{
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.");
db03b557 73 if (!branch->merge_nr || !branch->merge)
53c40311 74 die("The current branch %s has no upstream branch.",
52153747
FAG
75 branch->name);
76 if (branch->merge_nr != 1)
53c40311 77 die("The current branch %s has multiple upstream branches, "
52153747
FAG
78 "refusing to push.", branch->name);
79 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
80 add_refspec(refspec.buf);
81}
82
83static void setup_default_push_refspecs(void)
84{
52153747 85 switch (push_default) {
bba0fd22 86 default:
52153747
FAG
87 case PUSH_DEFAULT_MATCHING:
88 add_refspec(":");
89 break;
90
53c40311
JH
91 case PUSH_DEFAULT_UPSTREAM:
92 setup_push_upstream();
52153747
FAG
93 break;
94
95 case PUSH_DEFAULT_CURRENT:
96 add_refspec("HEAD");
97 break;
98
99 case PUSH_DEFAULT_NOTHING:
100 die("You didn't specify any refspecs to push, and "
101 "push.default is \"nothing\".");
102 break;
103 }
104}
105
fb0cc87e
DB
106static int push_with_options(struct transport *transport, int flags)
107{
108 int err;
109 int nonfastforward;
8afd8dc0 110
78381069 111 transport_set_verbosity(transport, verbosity, progress);
8afd8dc0 112
fb0cc87e
DB
113 if (receivepack)
114 transport_set_option(transport,
115 TRANS_OPT_RECEIVEPACK, receivepack);
116 if (thin)
117 transport_set_option(transport, TRANS_OPT_THIN, "yes");
118
8afd8dc0 119 if (verbosity > 0)
fb0cc87e
DB
120 fprintf(stderr, "Pushing to %s\n", transport->url);
121 err = transport_push(transport, refspec_nr, refspec, flags,
122 &nonfastforward);
53970b92
TRC
123 if (err != 0)
124 error("failed to push some refs to '%s'", transport->url);
125
fb0cc87e
DB
126 err |= transport_disconnect(transport);
127
128 if (!err)
129 return 0;
130
fb0cc87e 131 if (nonfastforward && advice_push_nonfastforward) {
011fe981 132 fprintf(stderr, "To prevent you from losing history, non-fast-forward updates were rejected\n"
452c6d50
MM
133 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
134 "'Note about fast-forwards' section of 'git push --help' for details.\n");
fb0cc87e
DB
135 }
136
137 return 1;
138}
139
9b288516 140static int do_push(const char *repo, int flags)
755225de 141{
5751f490 142 int i, errs;
5751f490 143 struct remote *remote = remote_get(repo);
20346234
MG
144 const char **url;
145 int url_nr;
755225de 146
fa685bdf
DB
147 if (!remote) {
148 if (repo)
149 die("bad repository '%s'", repo);
150 die("No destination configured to push to.");
151 }
755225de 152
84bb2dfd
PB
153 if (remote->mirror)
154 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
155
b259f09b
MZ
156 if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
157 if (!strcmp(*refspec, "refs/tags/*"))
158 return error("--all and --tags are incompatible");
159 return error("--all can't be combined with refspecs");
160 }
161
162 if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
163 if (!strcmp(*refspec, "refs/tags/*"))
164 return error("--mirror and --tags are incompatible");
165 return error("--mirror can't be combined with refspecs");
166 }
84bb2dfd
PB
167
168 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
169 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
170 return error("--all and --mirror are incompatible");
171 }
172
52153747
FAG
173 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
174 if (remote->push_refspec_nr) {
175 refspec = remote->push_refspec;
176 refspec_nr = remote->push_refspec_nr;
177 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
178 setup_default_push_refspecs();
5751f490 179 }
fd1d1b05 180 errs = 0;
20346234
MG
181 if (remote->pushurl_nr) {
182 url = remote->pushurl;
183 url_nr = remote->pushurl_nr;
184 } else {
185 url = remote->url;
186 url_nr = remote->url_nr;
187 }
fb0cc87e
DB
188 if (url_nr) {
189 for (i = 0; i < url_nr; i++) {
190 struct transport *transport =
191 transport_get(remote, url[i]);
192 if (push_with_options(transport, flags))
193 errs++;
07436e43 194 }
fb0cc87e
DB
195 } else {
196 struct transport *transport =
197 transport_get(remote, NULL);
198
199 if (push_with_options(transport, flags))
200 errs++;
755225de 201 }
fd1d1b05 202 return !!errs;
755225de
LT
203}
204
a633fca0 205int cmd_push(int argc, const char **argv, const char *prefix)
755225de 206{
9b288516 207 int flags = 0;
378c4832 208 int tags = 0;
84bb2dfd 209 int rc;
5751f490 210 const char *repo = NULL; /* default repository */
378c4832 211 struct option options[] = {
8afd8dc0 212 OPT__VERBOSITY(&verbosity),
378c4832 213 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
c29c1b40
MB
214 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
215 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
216 (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
f517f1f2 217 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
f740cc25 218 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
9f67fee2 219 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
1965ff74 220 OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
c29c1b40 221 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
378c4832
DB
222 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
223 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
224 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
e9fcd1e2
IL
225 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
226 TRANSPORT_PUSH_SET_UPSTREAM),
78381069 227 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
378c4832
DB
228 OPT_END()
229 };
755225de 230
2aae905f 231 git_config(git_default_config, NULL);
37782920 232 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
378c4832 233
f517f1f2
JK
234 if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
235 die("--delete is incompatible with --all, --mirror and --tags");
236 if (deleterefs && argc < 2)
237 die("--delete doesn't make sense without any refs");
238
378c4832
DB
239 if (tags)
240 add_refspec("refs/tags/*");
378c4832
DB
241
242 if (argc > 0) {
243 repo = argv[0];
244 set_refspecs(argv + 1, argc - 1);
755225de 245 }
8558fd9e 246
84bb2dfd
PB
247 rc = do_push(repo, flags);
248 if (rc == -1)
94c89ba6 249 usage_with_options(push_usage, options);
84bb2dfd
PB
250 else
251 return rc;
755225de 252}