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