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