]>
Commit | Line | Data |
---|---|---|
755225de LT |
1 | /* |
2 | * "git push" | |
3 | */ | |
4 | #include "cache.h" | |
6c6ddf92 | 5 | #include "advice.h" |
bdaf1dfa | 6 | #include "branch.h" |
b2141fc1 | 7 | #include "config.h" |
32a8f510 | 8 | #include "environment.h" |
f394e093 | 9 | #include "gettext.h" |
755225de | 10 | #include "refs.h" |
ec0cb496 | 11 | #include "refspec.h" |
755225de LT |
12 | #include "run-command.h" |
13 | #include "builtin.h" | |
5751f490 | 14 | #include "remote.h" |
9b288516 | 15 | #include "transport.h" |
378c4832 | 16 | #include "parse-options.h" |
b388633c | 17 | #include "pkt-line.h" |
d1cbe1e6 | 18 | #include "repository.h" |
d2b17b32 | 19 | #include "submodule.h" |
b33a15b0 | 20 | #include "submodule-config.h" |
30261094 | 21 | #include "send-pack.h" |
74ea5c95 | 22 | #include "trace2.h" |
960786e7 | 23 | #include "color.h" |
755225de | 24 | |
378c4832 | 25 | static const char * const push_usage[] = { |
78dafaa5 | 26 | N_("git push [<options>] [<repository> [<refspec>...]]"), |
378c4832 DB |
27 | NULL, |
28 | }; | |
755225de | 29 | |
960786e7 RD |
30 | static int push_use_color = -1; |
31 | static char push_colors[][COLOR_MAXLEN] = { | |
32 | GIT_COLOR_RESET, | |
33 | GIT_COLOR_RED, /* ERROR */ | |
34 | }; | |
35 | ||
36 | enum color_push { | |
37 | PUSH_COLOR_RESET = 0, | |
38 | PUSH_COLOR_ERROR = 1 | |
39 | }; | |
40 | ||
41 | static int parse_push_color_slot(const char *slot) | |
42 | { | |
43 | if (!strcasecmp(slot, "reset")) | |
44 | return PUSH_COLOR_RESET; | |
45 | if (!strcasecmp(slot, "error")) | |
46 | return PUSH_COLOR_ERROR; | |
47 | return -1; | |
48 | } | |
49 | ||
50 | static const char *push_get_color(enum color_push ix) | |
51 | { | |
52 | if (want_color_stderr(push_use_color)) | |
53 | return push_colors[ix]; | |
54 | return ""; | |
55 | } | |
56 | ||
f7c815c3 | 57 | static int thin = 1; |
f517f1f2 | 58 | static int deleterefs; |
d23842fd | 59 | static const char *receivepack; |
8afd8dc0 | 60 | static int verbosity; |
d34141cd MC |
61 | static int progress = -1; |
62 | static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; | |
c915f11e | 63 | static enum transport_family family; |
755225de | 64 | |
28f5d176 JH |
65 | static struct push_cas_option cas; |
66 | ||
aa40289c | 67 | static struct refspec rs = REFSPEC_INIT_PUSH; |
755225de | 68 | |
d8052750 MP |
69 | static struct string_list push_options_config = STRING_LIST_INIT_DUP; |
70 | ||
30035d9c | 71 | static void refspec_append_mapped(struct refspec *refspec, const char *ref, |
aa561208 | 72 | struct remote *remote, struct ref *matched) |
755225de | 73 | { |
1768aaf0 | 74 | const char *branch_name; |
ca02465b | 75 | |
6bdb304b | 76 | if (remote->push.nr) { |
0ad4a5ff BW |
77 | struct refspec_item query; |
78 | memset(&query, 0, sizeof(struct refspec_item)); | |
ca02465b | 79 | query.src = matched->name; |
86baf825 | 80 | if (!query_refspecs(&remote->push, &query) && query.dst) { |
1af8b8c0 RS |
81 | refspec_appendf(refspec, "%s%s:%s", |
82 | query.force ? "+" : "", | |
83 | query.src, query.dst); | |
30035d9c | 84 | return; |
ca02465b JH |
85 | } |
86 | } | |
87 | ||
fc9261ca | 88 | if (push_default == PUSH_DEFAULT_UPSTREAM && |
1768aaf0 RS |
89 | skip_prefix(matched->name, "refs/heads/", &branch_name)) { |
90 | struct branch *branch = branch_get(branch_name); | |
fc9261ca | 91 | if (branch->merge_nr == 1 && branch->merge[0]->src) { |
1af8b8c0 RS |
92 | refspec_appendf(refspec, "%s:%s", |
93 | ref, branch->merge[0]->src); | |
30035d9c | 94 | return; |
fc9261ca JH |
95 | } |
96 | } | |
97 | ||
30035d9c | 98 | refspec_append(refspec, ref); |
ca02465b JH |
99 | } |
100 | ||
101 | static void set_refspecs(const char **refs, int nr, const char *repo) | |
102 | { | |
103 | struct remote *remote = NULL; | |
104 | struct ref *local_refs = NULL; | |
8558fd9e | 105 | int i; |
ca02465b | 106 | |
8558fd9e DB |
107 | for (i = 0; i < nr; i++) { |
108 | const char *ref = refs[i]; | |
109 | if (!strcmp("tag", ref)) { | |
8558fd9e | 110 | if (nr <= ++i) |
8352d29e | 111 | die(_("tag shorthand without <tag>")); |
50d829c1 JH |
112 | ref = refs[i]; |
113 | if (deleterefs) | |
1af8b8c0 | 114 | refspec_appendf(&rs, ":refs/tags/%s", ref); |
50d829c1 | 115 | else |
1af8b8c0 | 116 | refspec_appendf(&rs, "refs/tags/%s", ref); |
50d829c1 | 117 | } else if (deleterefs) { |
20e41640 | 118 | if (strchr(ref, ':') || !*ref) |
50d829c1 | 119 | die(_("--delete only accepts plain target ref names")); |
1af8b8c0 | 120 | refspec_appendf(&rs, ":%s", ref); |
ca02465b | 121 | } else if (!strchr(ref, ':')) { |
aa561208 ÆAB |
122 | struct ref *matched = NULL; |
123 | ||
124 | /* lazily grab local_refs */ | |
125 | if (!local_refs) | |
ca02465b | 126 | local_refs = get_local_heads(); |
aa561208 ÆAB |
127 | |
128 | /* Does "ref" uniquely name our ref? */ | |
129 | if (count_refspec_match(ref, local_refs, &matched) != 1) { | |
130 | refspec_append(&rs, ref); | |
131 | } else { | |
132 | /* lazily grab remote */ | |
133 | if (!remote) | |
134 | remote = remote_get(repo); | |
135 | if (!remote) | |
136 | BUG("must get a remote for repo '%s'", repo); | |
137 | ||
138 | refspec_append_mapped(&rs, ref, remote, matched); | |
f517f1f2 | 139 | } |
30035d9c RS |
140 | } else |
141 | refspec_append(&rs, ref); | |
755225de | 142 | } |
c65d18cb | 143 | free_refs(local_refs); |
755225de LT |
144 | } |
145 | ||
135dadef JH |
146 | static int push_url_of_remote(struct remote *remote, const char ***url_p) |
147 | { | |
148 | if (remote->pushurl_nr) { | |
149 | *url_p = remote->pushurl; | |
150 | return remote->pushurl_nr; | |
151 | } | |
152 | *url_p = remote->url; | |
153 | return remote->url_nr; | |
154 | } | |
155 | ||
dbcd970c JS |
156 | static NORETURN void die_push_simple(struct branch *branch, |
157 | struct remote *remote) | |
3b335762 | 158 | { |
b55e6775 MM |
159 | /* |
160 | * There's no point in using shorten_unambiguous_ref here, | |
161 | * as the ambiguity would be on the remote side, not what | |
162 | * we have locally. Plus, this is supposed to be the simple | |
163 | * mode. If the user is doing something crazy like setting | |
164 | * upstream to a non-branch, we should probably be showing | |
165 | * them the big ugly fully qualified ref. | |
166 | */ | |
bdaf1dfa TK |
167 | const char *advice_pushdefault_maybe = ""; |
168 | const char *advice_automergesimple_maybe = ""; | |
cf4fff57 JK |
169 | const char *short_upstream = branch->merge[0]->src; |
170 | ||
171 | skip_prefix(short_upstream, "refs/heads/", &short_upstream); | |
b55e6775 | 172 | |
b55e6775 | 173 | /* |
98e023de | 174 | * Don't show advice for people who explicitly set |
b55e6775 MM |
175 | * push.default. |
176 | */ | |
177 | if (push_default == PUSH_DEFAULT_UNSPECIFIED) | |
bdaf1dfa | 178 | advice_pushdefault_maybe = _("\n" |
b55e6775 | 179 | "To choose either option permanently, " |
bdaf1dfa TK |
180 | "see push.default in 'git help config'.\n"); |
181 | if (git_branch_track != BRANCH_TRACK_SIMPLE) | |
182 | advice_automergesimple_maybe = _("\n" | |
183 | "To avoid automatically configuring " | |
2a905f8f AH |
184 | "an upstream branch when its name\n" |
185 | "won't match the local branch, see option " | |
1f8496c6 | 186 | "'simple' of branch.autoSetupMerge\n" |
bdaf1dfa | 187 | "in 'git help config'.\n"); |
b55e6775 MM |
188 | die(_("The upstream branch of your current branch does not match\n" |
189 | "the name of your current branch. To push to the upstream branch\n" | |
190 | "on the remote, use\n" | |
191 | "\n" | |
192 | " git push %s HEAD:%s\n" | |
193 | "\n" | |
194 | "To push to the branch of the same name on the remote, use\n" | |
195 | "\n" | |
82471667 | 196 | " git push %s HEAD\n" |
bdaf1dfa | 197 | "%s%s"), |
b55e6775 | 198 | remote->name, short_upstream, |
bdaf1dfa TK |
199 | remote->name, advice_pushdefault_maybe, |
200 | advice_automergesimple_maybe); | |
b55e6775 MM |
201 | } |
202 | ||
35ee69c0 RR |
203 | static const char message_detached_head_die[] = |
204 | N_("You are not currently on a branch.\n" | |
205 | "To push the history leading to the current (detached HEAD)\n" | |
206 | "state now, use\n" | |
207 | "\n" | |
208 | " git push %s HEAD:<name-of-remote-branch>\n"); | |
209 | ||
05d57750 | 210 | static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name) |
52153747 | 211 | { |
05d57750 TK |
212 | if (branch->merge_nr == 0 && (flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { |
213 | /* if missing, assume same; set_upstream will be defined later */ | |
214 | return branch->refname; | |
215 | } | |
216 | ||
217 | if (!branch->merge_nr || !branch->merge || !branch->remote_name) { | |
218 | const char *advice_autosetup_maybe = ""; | |
219 | if (!(flags & TRANSPORT_PUSH_AUTO_UPSTREAM)) { | |
220 | advice_autosetup_maybe = _("\n" | |
221 | "To have this happen automatically for " | |
222 | "branches without a tracking\n" | |
223 | "upstream, see 'push.autoSetupRemote' " | |
224 | "in 'git help config'.\n"); | |
225 | } | |
6c80cd29 | 226 | die(_("The current branch %s has no upstream branch.\n" |
ec8460bd MM |
227 | "To push the current branch and set the remote as upstream, use\n" |
228 | "\n" | |
05d57750 TK |
229 | " git push --set-upstream %s %s\n" |
230 | "%s"), | |
ec8460bd | 231 | branch->name, |
533e0325 | 232 | remote_name, |
05d57750 TK |
233 | branch->name, |
234 | advice_autosetup_maybe); | |
235 | } | |
52153747 | 236 | if (branch->merge_nr != 1) |
6c80cd29 | 237 | die(_("The current branch %s has multiple upstream branches, " |
8352d29e | 238 | "refusing to push."), branch->name); |
533e0325 FC |
239 | |
240 | return branch->merge[0]->src; | |
241 | } | |
242 | ||
05d57750 | 243 | static void setup_default_push_refspecs(int *flags, struct remote *remote) |
52153747 | 244 | { |
65c63a00 | 245 | struct branch *branch; |
00458dc5 | 246 | const char *dst; |
e0c91cff | 247 | int same_remote; |
7b2ecd81 | 248 | |
52153747 | 249 | switch (push_default) { |
52153747 | 250 | case PUSH_DEFAULT_MATCHING: |
aa40289c | 251 | refspec_append(&rs, ":"); |
72739680 | 252 | return; |
52153747 | 253 | |
04159fba FC |
254 | case PUSH_DEFAULT_NOTHING: |
255 | die(_("You didn't specify any refspecs to push, and " | |
256 | "push.default is \"nothing\".")); | |
257 | return; | |
258 | default: | |
259 | break; | |
260 | } | |
261 | ||
65c63a00 | 262 | branch = branch_get(NULL); |
cc16f95d FC |
263 | if (!branch) |
264 | die(_(message_detached_head_die), remote->name); | |
265 | ||
1f934725 | 266 | dst = branch->refname; |
7088ce71 | 267 | same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); |
1f934725 | 268 | |
04159fba FC |
269 | switch (push_default) { |
270 | default: | |
b2ed944a | 271 | case PUSH_DEFAULT_UNSPECIFIED: |
b55e6775 | 272 | case PUSH_DEFAULT_SIMPLE: |
1f934725 FC |
273 | if (!same_remote) |
274 | break; | |
05d57750 | 275 | if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name))) |
1f934725 | 276 | die_push_simple(branch, remote); |
00458dc5 | 277 | break; |
b55e6775 | 278 | |
53c40311 | 279 | case PUSH_DEFAULT_UPSTREAM: |
0add899b FC |
280 | if (!same_remote) |
281 | die(_("You are pushing to remote '%s', which is not the upstream of\n" | |
282 | "your current branch '%s', without telling me what to push\n" | |
283 | "to update which remote branch."), | |
284 | remote->name, branch->name); | |
05d57750 | 285 | dst = get_upstream_ref(*flags, branch, remote->name); |
00458dc5 | 286 | break; |
52153747 FAG |
287 | |
288 | case PUSH_DEFAULT_CURRENT: | |
00458dc5 | 289 | break; |
52153747 | 290 | } |
00458dc5 | 291 | |
05d57750 TK |
292 | /* |
293 | * this is a default push - if auto-upstream is enabled and there is | |
294 | * no upstream defined, then set it (with options 'simple', 'upstream', | |
295 | * and 'current'). | |
296 | */ | |
297 | if ((*flags & TRANSPORT_PUSH_AUTO_UPSTREAM) && branch->merge_nr == 0) | |
298 | *flags |= TRANSPORT_PUSH_SET_UPSTREAM; | |
299 | ||
00458dc5 | 300 | refspec_appendf(&rs, "%s:%s", branch->refname, dst); |
52153747 FAG |
301 | } |
302 | ||
f25950f3 CT |
303 | static const char message_advice_pull_before_push[] = |
304 | N_("Updates were rejected because the tip of your current branch is behind\n" | |
fc6c4e96 JK |
305 | "its remote counterpart. Integrate the remote changes (e.g.\n" |
306 | "'git pull ...') before pushing again.\n" | |
f25950f3 CT |
307 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
308 | ||
f25950f3 CT |
309 | static const char message_advice_checkout_pull_push[] = |
310 | N_("Updates were rejected because a pushed branch tip is behind its remote\n" | |
fc6c4e96 JK |
311 | "counterpart. Check out this branch and integrate the remote changes\n" |
312 | "(e.g. 'git pull ...') before pushing again.\n" | |
f25950f3 CT |
313 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
314 | ||
75e5c0dc JH |
315 | static const char message_advice_ref_fetch_first[] = |
316 | N_("Updates were rejected because the remote contains work that you do\n" | |
317 | "not have locally. This is usually caused by another repository pushing\n" | |
fc6c4e96 JK |
318 | "to the same ref. You may want to first integrate the remote changes\n" |
319 | "(e.g., 'git pull ...') before pushing again.\n" | |
75e5c0dc JH |
320 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
321 | ||
b24e6047 | 322 | static const char message_advice_ref_already_exists[] = |
b4cf8db2 | 323 | N_("Updates were rejected because the tag already exists in the remote."); |
b24e6047 | 324 | |
75e5c0dc JH |
325 | static const char message_advice_ref_needs_force[] = |
326 | N_("You cannot update a remote ref that points at a non-commit object,\n" | |
327 | "or update a remote ref to make it point at a non-commit object,\n" | |
328 | "without using the '--force' option.\n"); | |
b24e6047 | 329 | |
3b990aa6 SK |
330 | static const char message_advice_ref_needs_update[] = |
331 | N_("Updates were rejected because the tip of the remote-tracking\n" | |
332 | "branch has been updated since the last checkout. You may want\n" | |
333 | "to integrate those changes locally (e.g., 'git pull ...')\n" | |
334 | "before forcing an update.\n"); | |
335 | ||
f25950f3 CT |
336 | static void advise_pull_before_push(void) |
337 | { | |
ed9bff08 | 338 | if (!advice_enabled(ADVICE_PUSH_NON_FF_CURRENT) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
f25950f3 CT |
339 | return; |
340 | advise(_(message_advice_pull_before_push)); | |
341 | } | |
342 | ||
f25950f3 CT |
343 | static void advise_checkout_pull_push(void) |
344 | { | |
ed9bff08 | 345 | if (!advice_enabled(ADVICE_PUSH_NON_FF_MATCHING) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
f25950f3 CT |
346 | return; |
347 | advise(_(message_advice_checkout_pull_push)); | |
348 | } | |
349 | ||
b24e6047 CR |
350 | static void advise_ref_already_exists(void) |
351 | { | |
ed9bff08 | 352 | if (!advice_enabled(ADVICE_PUSH_ALREADY_EXISTS) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
b4505682 | 353 | return; |
b24e6047 CR |
354 | advise(_(message_advice_ref_already_exists)); |
355 | } | |
356 | ||
75e5c0dc JH |
357 | static void advise_ref_fetch_first(void) |
358 | { | |
ed9bff08 | 359 | if (!advice_enabled(ADVICE_PUSH_FETCH_FIRST) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
75e5c0dc JH |
360 | return; |
361 | advise(_(message_advice_ref_fetch_first)); | |
362 | } | |
363 | ||
364 | static void advise_ref_needs_force(void) | |
365 | { | |
ed9bff08 | 366 | if (!advice_enabled(ADVICE_PUSH_NEEDS_FORCE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
75e5c0dc JH |
367 | return; |
368 | advise(_(message_advice_ref_needs_force)); | |
369 | } | |
370 | ||
3b990aa6 SK |
371 | static void advise_ref_needs_update(void) |
372 | { | |
ed9bff08 | 373 | if (!advice_enabled(ADVICE_PUSH_REF_NEEDS_UPDATE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
3b990aa6 SK |
374 | return; |
375 | advise(_(message_advice_ref_needs_update)); | |
376 | } | |
377 | ||
aa40289c BW |
378 | static int push_with_options(struct transport *transport, struct refspec *rs, |
379 | int flags) | |
fb0cc87e DB |
380 | { |
381 | int err; | |
10643d4e | 382 | unsigned int reject_reasons; |
d192fa50 | 383 | char *anon_url = transport_anonymize_url(transport->url); |
8afd8dc0 | 384 | |
78381069 | 385 | transport_set_verbosity(transport, verbosity, progress); |
c915f11e | 386 | transport->family = family; |
8afd8dc0 | 387 | |
fb0cc87e DB |
388 | if (receivepack) |
389 | transport_set_option(transport, | |
390 | TRANS_OPT_RECEIVEPACK, receivepack); | |
f7c815c3 | 391 | transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL); |
fb0cc87e | 392 | |
91048a95 JH |
393 | if (!is_empty_cas(&cas)) { |
394 | if (!transport->smart_options) | |
395 | die("underlying transport does not support --%s option", | |
396 | CAS_OPT_NAME); | |
397 | transport->smart_options->cas = &cas; | |
398 | } | |
399 | ||
8afd8dc0 | 400 | if (verbosity > 0) |
d192fa50 | 401 | fprintf(stderr, _("Pushing to %s\n"), anon_url); |
25e4b809 | 402 | trace2_region_enter("push", "transport_push", the_repository); |
6c6d5d07 NTND |
403 | err = transport_push(the_repository, transport, |
404 | rs, flags, &reject_reasons); | |
25e4b809 | 405 | trace2_region_leave("push", "transport_push", the_repository); |
960786e7 RD |
406 | if (err != 0) { |
407 | fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR)); | |
d192fa50 | 408 | error(_("failed to push some refs to '%s'"), anon_url); |
960786e7 RD |
409 | fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET)); |
410 | } | |
53970b92 | 411 | |
fb0cc87e | 412 | err |= transport_disconnect(transport); |
d192fa50 | 413 | free(anon_url); |
fb0cc87e DB |
414 | if (!err) |
415 | return 0; | |
416 | ||
10643d4e | 417 | if (reject_reasons & REJECT_NON_FF_HEAD) { |
f25950f3 | 418 | advise_pull_before_push(); |
10643d4e | 419 | } else if (reject_reasons & REJECT_NON_FF_OTHER) { |
b2ed944a | 420 | advise_checkout_pull_push(); |
b24e6047 CR |
421 | } else if (reject_reasons & REJECT_ALREADY_EXISTS) { |
422 | advise_ref_already_exists(); | |
75e5c0dc JH |
423 | } else if (reject_reasons & REJECT_FETCH_FIRST) { |
424 | advise_ref_fetch_first(); | |
425 | } else if (reject_reasons & REJECT_NEEDS_FORCE) { | |
426 | advise_ref_needs_force(); | |
3b990aa6 SK |
427 | } else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) { |
428 | advise_ref_needs_update(); | |
fb0cc87e DB |
429 | } |
430 | ||
431 | return 1; | |
432 | } | |
433 | ||
5b9427e0 | 434 | static int do_push(int flags, |
8e4c8af0 TG |
435 | const struct string_list *push_options, |
436 | struct remote *remote) | |
755225de | 437 | { |
5751f490 | 438 | int i, errs; |
20346234 MG |
439 | const char **url; |
440 | int url_nr; | |
aa40289c | 441 | struct refspec *push_refspec = &rs; |
755225de | 442 | |
f6a4e61f SB |
443 | if (push_options->nr) |
444 | flags |= TRANSPORT_PUSH_OPTIONS; | |
445 | ||
aa40289c BW |
446 | if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) { |
447 | if (remote->push.nr) { | |
448 | push_refspec = &remote->push; | |
52153747 | 449 | } else if (!(flags & TRANSPORT_PUSH_MIRROR)) |
05d57750 | 450 | setup_default_push_refspecs(&flags, remote); |
5751f490 | 451 | } |
fd1d1b05 | 452 | errs = 0; |
135dadef | 453 | url_nr = push_url_of_remote(remote, &url); |
fb0cc87e DB |
454 | if (url_nr) { |
455 | for (i = 0; i < url_nr; i++) { | |
456 | struct transport *transport = | |
457 | transport_get(remote, url[i]); | |
f6a4e61f SB |
458 | if (flags & TRANSPORT_PUSH_OPTIONS) |
459 | transport->push_options = push_options; | |
aa40289c | 460 | if (push_with_options(transport, push_refspec, flags)) |
fb0cc87e | 461 | errs++; |
07436e43 | 462 | } |
fb0cc87e DB |
463 | } else { |
464 | struct transport *transport = | |
465 | transport_get(remote, NULL); | |
f6a4e61f SB |
466 | if (flags & TRANSPORT_PUSH_OPTIONS) |
467 | transport->push_options = push_options; | |
aa40289c | 468 | if (push_with_options(transport, push_refspec, flags)) |
fb0cc87e | 469 | errs++; |
755225de | 470 | } |
fd1d1b05 | 471 | return !!errs; |
755225de LT |
472 | } |
473 | ||
d2b17b32 FG |
474 | static int option_parse_recurse_submodules(const struct option *opt, |
475 | const char *arg, int unset) | |
476 | { | |
b33a15b0 | 477 | int *recurse_submodules = opt->value; |
eb21c732 | 478 | |
b33a15b0 MC |
479 | if (unset) |
480 | *recurse_submodules = RECURSE_SUBMODULES_OFF; | |
e62f779a JT |
481 | else { |
482 | if (!strcmp(arg, "only-is-on-demand")) { | |
483 | if (*recurse_submodules == RECURSE_SUBMODULES_ONLY) { | |
484 | warning(_("recursing into submodule with push.recurseSubmodules=only; using on-demand instead")); | |
485 | *recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; | |
486 | } | |
487 | } else { | |
488 | *recurse_submodules = parse_push_recurse_submodules_arg(opt->long_name, arg); | |
489 | } | |
490 | } | |
d2b17b32 FG |
491 | |
492 | return 0; | |
493 | } | |
494 | ||
68c757f2 DB |
495 | static void set_push_cert_flags(int *flags, int v) |
496 | { | |
497 | switch (v) { | |
498 | case SEND_PACK_PUSH_CERT_NEVER: | |
499 | *flags &= ~(TRANSPORT_PUSH_CERT_ALWAYS | TRANSPORT_PUSH_CERT_IF_ASKED); | |
500 | break; | |
501 | case SEND_PACK_PUSH_CERT_ALWAYS: | |
502 | *flags |= TRANSPORT_PUSH_CERT_ALWAYS; | |
503 | *flags &= ~TRANSPORT_PUSH_CERT_IF_ASKED; | |
504 | break; | |
505 | case SEND_PACK_PUSH_CERT_IF_ASKED: | |
506 | *flags |= TRANSPORT_PUSH_CERT_IF_ASKED; | |
507 | *flags &= ~TRANSPORT_PUSH_CERT_ALWAYS; | |
508 | break; | |
509 | } | |
510 | } | |
511 | ||
512 | ||
b9459019 MG |
513 | static int git_push_config(const char *k, const char *v, void *cb) |
514 | { | |
960786e7 | 515 | const char *slot_name; |
a8bc269f | 516 | int *flags = cb; |
a8bc269f DO |
517 | |
518 | if (!strcmp(k, "push.followtags")) { | |
519 | if (git_config_bool(k, v)) | |
520 | *flags |= TRANSPORT_PUSH_FOLLOW_TAGS; | |
521 | else | |
522 | *flags &= ~TRANSPORT_PUSH_FOLLOW_TAGS; | |
523 | return 0; | |
05d57750 TK |
524 | } else if (!strcmp(k, "push.autosetupremote")) { |
525 | if (git_config_bool(k, v)) | |
526 | *flags |= TRANSPORT_PUSH_AUTO_UPSTREAM; | |
527 | return 0; | |
68c757f2 DB |
528 | } else if (!strcmp(k, "push.gpgsign")) { |
529 | const char *value; | |
530 | if (!git_config_get_value("push.gpgsign", &value)) { | |
89576613 | 531 | switch (git_parse_maybe_bool(value)) { |
68c757f2 DB |
532 | case 0: |
533 | set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_NEVER); | |
534 | break; | |
535 | case 1: | |
536 | set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_ALWAYS); | |
537 | break; | |
538 | default: | |
539 | if (value && !strcasecmp(value, "if-asked")) | |
540 | set_push_cert_flags(flags, SEND_PACK_PUSH_CERT_IF_ASKED); | |
541 | else | |
1a8aea85 | 542 | return error(_("invalid value for '%s'"), k); |
68c757f2 DB |
543 | } |
544 | } | |
b33a15b0 MC |
545 | } else if (!strcmp(k, "push.recursesubmodules")) { |
546 | const char *value; | |
547 | if (!git_config_get_value("push.recursesubmodules", &value)) | |
548 | recurse_submodules = parse_push_recurse_submodules_arg(k, value); | |
4e53d6a5 SB |
549 | } else if (!strcmp(k, "submodule.recurse")) { |
550 | int val = git_config_bool(k, v) ? | |
551 | RECURSE_SUBMODULES_ON_DEMAND : RECURSE_SUBMODULES_OFF; | |
552 | recurse_submodules = val; | |
d8052750 MP |
553 | } else if (!strcmp(k, "push.pushoption")) { |
554 | if (!v) | |
555 | return config_error_nonbool(k); | |
556 | else | |
557 | if (!*v) | |
558 | string_list_clear(&push_options_config, 0); | |
559 | else | |
560 | string_list_append(&push_options_config, v); | |
561 | return 0; | |
960786e7 RD |
562 | } else if (!strcmp(k, "color.push")) { |
563 | push_use_color = git_config_colorbool(k, v); | |
564 | return 0; | |
565 | } else if (skip_prefix(k, "color.push.", &slot_name)) { | |
566 | int slot = parse_push_color_slot(slot_name); | |
567 | if (slot < 0) | |
568 | return 0; | |
569 | if (!v) | |
570 | return config_error_nonbool(k); | |
571 | return color_parse(v, push_colors[slot]); | |
3b990aa6 SK |
572 | } else if (!strcmp(k, "push.useforceifincludes")) { |
573 | if (git_config_bool(k, v)) | |
574 | *flags |= TRANSPORT_PUSH_FORCE_IF_INCLUDES; | |
575 | else | |
576 | *flags &= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES; | |
577 | return 0; | |
a8bc269f DO |
578 | } |
579 | ||
06038cd7 | 580 | return git_default_config(k, v, NULL); |
b9459019 MG |
581 | } |
582 | ||
a633fca0 | 583 | int cmd_push(int argc, const char **argv, const char *prefix) |
755225de | 584 | { |
9b288516 | 585 | int flags = 0; |
378c4832 | 586 | int tags = 0; |
30261094 | 587 | int push_cert = -1; |
84bb2dfd | 588 | int rc; |
5751f490 | 589 | const char *repo = NULL; /* default repository */ |
d8052750 MP |
590 | struct string_list push_options_cmdline = STRING_LIST_INIT_DUP; |
591 | struct string_list *push_options; | |
54cc8aca | 592 | const struct string_list_item *item; |
8e4c8af0 | 593 | struct remote *remote; |
f6a4e61f | 594 | |
378c4832 | 595 | struct option options[] = { |
8afd8dc0 | 596 | OPT__VERBOSITY(&verbosity), |
78dafaa5 | 597 | OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")), |
425b4d7f TL |
598 | OPT_BIT( 0 , "all", &flags, N_("push all branches"), TRANSPORT_PUSH_ALL), |
599 | OPT_ALIAS( 0 , "branches", "all"), | |
78dafaa5 | 600 | OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"), |
c29c1b40 | 601 | (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)), |
38a25591 | 602 | OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")), |
425b4d7f | 603 | OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --branches or --mirror)")), |
78dafaa5 NTND |
604 | OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN), |
605 | OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN), | |
606 | OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE), | |
203c8533 DL |
607 | OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"), |
608 | N_("require old value of ref to be at this value"), | |
609 | PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option), | |
3b990aa6 SK |
610 | OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES, &flags, |
611 | N_("require remote updates to be integrated locally"), | |
612 | TRANSPORT_PUSH_FORCE_IF_INCLUDES), | |
ce9baf23 DL |
613 | OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)", |
614 | N_("control recursive pushing of submodules"), option_parse_recurse_submodules), | |
f1e1bdd6 | 615 | OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE), |
78dafaa5 NTND |
616 | OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")), |
617 | OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")), | |
618 | OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"), | |
e9fcd1e2 | 619 | TRANSPORT_PUSH_SET_UPSTREAM), |
78dafaa5 NTND |
620 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), |
621 | OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"), | |
6ddba5e2 | 622 | TRANSPORT_PUSH_PRUNE), |
ec55559f | 623 | OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK), |
c2aba155 JH |
624 | OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"), |
625 | TRANSPORT_PUSH_FOLLOW_TAGS), | |
203c8533 DL |
626 | OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"), |
627 | PARSE_OPT_OPTARG, option_parse_push_signed), | |
d16c33b4 | 628 | OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC), |
d8052750 | 629 | OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")), |
c915f11e EW |
630 | OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"), |
631 | TRANSPORT_FAMILY_IPV4), | |
632 | OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"), | |
633 | TRANSPORT_FAMILY_IPV6), | |
378c4832 DB |
634 | OPT_END() |
635 | }; | |
755225de | 636 | |
bbc30f99 | 637 | packet_trace_identity("push"); |
06c21e18 | 638 | git_config(git_push_config, &flags); |
37782920 | 639 | argc = parse_options(argc, argv, prefix, options, push_usage, 0); |
d8052750 MP |
640 | push_options = (push_options_cmdline.nr |
641 | ? &push_options_cmdline | |
642 | : &push_options_config); | |
68c757f2 | 643 | set_push_cert_flags(&flags, push_cert); |
378c4832 | 644 | |
f517f1f2 | 645 | if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR)))) |
425b4d7f | 646 | die(_("options '%s' and '%s' cannot be used together"), "--delete", "--all/--branches/--mirror/--tags"); |
f517f1f2 | 647 | if (deleterefs && argc < 2) |
8352d29e | 648 | die(_("--delete doesn't make sense without any refs")); |
f517f1f2 | 649 | |
b33a15b0 MC |
650 | if (recurse_submodules == RECURSE_SUBMODULES_CHECK) |
651 | flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK; | |
652 | else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) | |
653 | flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND; | |
225e8bf7 BW |
654 | else if (recurse_submodules == RECURSE_SUBMODULES_ONLY) |
655 | flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY; | |
b33a15b0 | 656 | |
378c4832 | 657 | if (tags) |
aa40289c | 658 | refspec_append(&rs, "refs/tags/*"); |
378c4832 DB |
659 | |
660 | if (argc > 0) { | |
661 | repo = argv[0]; | |
ca02465b | 662 | set_refspecs(argv + 1, argc - 1, repo); |
755225de | 663 | } |
8558fd9e | 664 | |
8e4c8af0 TG |
665 | remote = pushremote_get(repo); |
666 | if (!remote) { | |
667 | if (repo) | |
668 | die(_("bad repository '%s'"), repo); | |
669 | die(_("No configured push destination.\n" | |
670 | "Either specify the URL from the command-line or configure a remote repository using\n" | |
671 | "\n" | |
672 | " git remote add <name> <url>\n" | |
673 | "\n" | |
674 | "and then push using the remote name\n" | |
675 | "\n" | |
676 | " git push <name>\n")); | |
677 | } | |
678 | ||
679 | if (remote->mirror) | |
680 | flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE); | |
681 | ||
682 | if (flags & TRANSPORT_PUSH_ALL) { | |
683 | if (tags) | |
12909b6b | 684 | die(_("options '%s' and '%s' cannot be used together"), "--all", "--tags"); |
8e4c8af0 TG |
685 | if (argc >= 2) |
686 | die(_("--all can't be combined with refspecs")); | |
687 | } | |
688 | if (flags & TRANSPORT_PUSH_MIRROR) { | |
689 | if (tags) | |
12909b6b | 690 | die(_("options '%s' and '%s' cannot be used together"), "--mirror", "--tags"); |
8e4c8af0 TG |
691 | if (argc >= 2) |
692 | die(_("--mirror can't be combined with refspecs")); | |
693 | } | |
694 | if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR)) | |
12909b6b | 695 | die(_("options '%s' and '%s' cannot be used together"), "--all", "--mirror"); |
8e4c8af0 | 696 | |
3b990aa6 SK |
697 | if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)) |
698 | cas.use_force_if_includes = 1; | |
699 | ||
d8052750 | 700 | for_each_string_list_item(item, push_options) |
f6a4e61f SB |
701 | if (strchr(item->string, '\n')) |
702 | die(_("push options must not have new line characters")); | |
703 | ||
5b9427e0 | 704 | rc = do_push(flags, push_options, remote); |
d8052750 MP |
705 | string_list_clear(&push_options_cmdline, 0); |
706 | string_list_clear(&push_options_config, 0); | |
84bb2dfd | 707 | if (rc == -1) |
94c89ba6 | 708 | usage_with_options(push_usage, options); |
84bb2dfd PB |
709 | else |
710 | return rc; | |
755225de | 711 | } |