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