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