Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git push" |
| 3 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 4 | #include "builtin.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 5 | #include "advice.h" |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 6 | #include "branch.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 8 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 9 | #include "gettext.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 10 | #include "refspec.h" |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 11 | #include "run-command.h" |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 12 | #include "remote.h" |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 13 | #include "transport.h" |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 14 | #include "parse-options.h" |
Elijah Newren | b388633 | 2023-04-22 20:17:14 +0000 | [diff] [blame] | 15 | #include "pkt-line.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 16 | #include "repository.h" |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 17 | #include "submodule.h" |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 18 | #include "submodule-config.h" |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 19 | #include "send-pack.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 20 | #include "trace2.h" |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 21 | #include "color.h" |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 22 | |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 23 | static const char * const push_usage[] = { |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 24 | N_("git push [<options>] [<repository> [<refspec>...]]"), |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 25 | NULL, |
| 26 | }; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 27 | |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | f7c815c | 2013-08-12 20:55:55 +0700 | [diff] [blame] | 55 | static int thin = 1; |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 56 | static int deleterefs; |
Uwe Kleine-König | d23842f | 2007-01-19 13:49:27 +0100 | [diff] [blame] | 57 | static const char *receivepack; |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 58 | static int verbosity; |
Mike Crowe | d34141c | 2015-12-03 13:10:35 +0000 | [diff] [blame] | 59 | static int progress = -1; |
| 60 | static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
Eric Wong | c915f11 | 2016-02-03 04:09:14 +0000 | [diff] [blame] | 61 | static enum transport_family family; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 62 | |
Junio C Hamano | 28f5d17 | 2013-07-08 15:34:36 -0700 | [diff] [blame] | 63 | static struct push_cas_option cas; |
| 64 | |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 65 | static struct refspec rs = REFSPEC_INIT_PUSH; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 66 | |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 67 | static struct string_list push_options_config = STRING_LIST_INIT_DUP; |
| 68 | |
René Scharfe | 30035d9 | 2020-09-05 16:47:47 +0200 | [diff] [blame] | 69 | static void refspec_append_mapped(struct refspec *refspec, const char *ref, |
Ævar Arnfjörð Bjarmason | aa56120 | 2023-02-07 00:07:53 +0100 | [diff] [blame] | 70 | struct remote *remote, struct ref *matched) |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 71 | { |
René Scharfe | 1768aaf | 2019-11-26 16:18:28 +0100 | [diff] [blame] | 72 | const char *branch_name; |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 73 | |
Brandon Williams | 6bdb304 | 2018-05-16 15:58:00 -0700 | [diff] [blame] | 74 | if (remote->push.nr) { |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 75 | struct refspec_item query; |
| 76 | memset(&query, 0, sizeof(struct refspec_item)); |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 77 | query.src = matched->name; |
Brandon Williams | 86baf82 | 2018-05-16 15:58:12 -0700 | [diff] [blame] | 78 | if (!query_refspecs(&remote->push, &query) && query.dst) { |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 79 | refspec_appendf(refspec, "%s%s:%s", |
| 80 | query.force ? "+" : "", |
| 81 | query.src, query.dst); |
René Scharfe | 30035d9 | 2020-09-05 16:47:47 +0200 | [diff] [blame] | 82 | return; |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Junio C Hamano | fc9261c | 2013-12-03 16:23:35 -0800 | [diff] [blame] | 86 | if (push_default == PUSH_DEFAULT_UPSTREAM && |
René Scharfe | 1768aaf | 2019-11-26 16:18:28 +0100 | [diff] [blame] | 87 | skip_prefix(matched->name, "refs/heads/", &branch_name)) { |
| 88 | struct branch *branch = branch_get(branch_name); |
Junio C Hamano | fc9261c | 2013-12-03 16:23:35 -0800 | [diff] [blame] | 89 | if (branch->merge_nr == 1 && branch->merge[0]->src) { |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 90 | refspec_appendf(refspec, "%s:%s", |
| 91 | ref, branch->merge[0]->src); |
René Scharfe | 30035d9 | 2020-09-05 16:47:47 +0200 | [diff] [blame] | 92 | return; |
Junio C Hamano | fc9261c | 2013-12-03 16:23:35 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
René Scharfe | 30035d9 | 2020-09-05 16:47:47 +0200 | [diff] [blame] | 96 | refspec_append(refspec, ref); |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 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; |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 103 | int i; |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 104 | |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 105 | for (i = 0; i < nr; i++) { |
| 106 | const char *ref = refs[i]; |
| 107 | if (!strcmp("tag", ref)) { |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 108 | if (nr <= ++i) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 109 | die(_("tag shorthand without <tag>")); |
Junio C Hamano | 50d829c | 2013-12-03 14:33:10 -0800 | [diff] [blame] | 110 | ref = refs[i]; |
| 111 | if (deleterefs) |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 112 | refspec_appendf(&rs, ":refs/tags/%s", ref); |
Junio C Hamano | 50d829c | 2013-12-03 14:33:10 -0800 | [diff] [blame] | 113 | else |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 114 | refspec_appendf(&rs, "refs/tags/%s", ref); |
Junio C Hamano | 50d829c | 2013-12-03 14:33:10 -0800 | [diff] [blame] | 115 | } else if (deleterefs) { |
Junio C Hamano | 20e4164 | 2021-02-23 15:13:32 -0800 | [diff] [blame] | 116 | if (strchr(ref, ':') || !*ref) |
Junio C Hamano | 50d829c | 2013-12-03 14:33:10 -0800 | [diff] [blame] | 117 | die(_("--delete only accepts plain target ref names")); |
René Scharfe | 1af8b8c | 2020-09-05 16:49:30 +0200 | [diff] [blame] | 118 | refspec_appendf(&rs, ":%s", ref); |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 119 | } else if (!strchr(ref, ':')) { |
Ævar Arnfjörð Bjarmason | aa56120 | 2023-02-07 00:07:53 +0100 | [diff] [blame] | 120 | struct ref *matched = NULL; |
| 121 | |
| 122 | /* lazily grab local_refs */ |
| 123 | if (!local_refs) |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 124 | local_refs = get_local_heads(); |
Ævar Arnfjörð Bjarmason | aa56120 | 2023-02-07 00:07:53 +0100 | [diff] [blame] | 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); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 137 | } |
René Scharfe | 30035d9 | 2020-09-05 16:47:47 +0200 | [diff] [blame] | 138 | } else |
| 139 | refspec_append(&rs, ref); |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 140 | } |
Ævar Arnfjörð Bjarmason | c65d18c | 2023-02-07 00:07:54 +0100 | [diff] [blame] | 141 | free_refs(local_refs); |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 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 | |
Johannes Schindelin | dbcd970 | 2019-09-30 02:55:31 -0700 | [diff] [blame] | 154 | static NORETURN void die_push_simple(struct branch *branch, |
| 155 | struct remote *remote) |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 156 | { |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 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 | */ |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 165 | const char *advice_pushdefault_maybe = ""; |
| 166 | const char *advice_automergesimple_maybe = ""; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 167 | const char *short_upstream = branch->merge[0]->src; |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 168 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 169 | skip_prefix(short_upstream, "refs/heads/", &short_upstream); |
| 170 | |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 171 | /* |
Ondřej Bílka | 98e023d | 2013-07-29 10:18:21 +0200 | [diff] [blame] | 172 | * Don't show advice for people who explicitly set |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 173 | * push.default. |
| 174 | */ |
| 175 | if (push_default == PUSH_DEFAULT_UNSPECIFIED) |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 176 | advice_pushdefault_maybe = _("\n" |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 177 | "To choose either option permanently, " |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 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 " |
Alex Henrie | 2a905f8 | 2022-09-27 23:58:11 -0600 | [diff] [blame] | 182 | "an upstream branch when its name\n" |
| 183 | "won't match the local branch, see option " |
Fangyi Zhou | 1f8496c | 2022-06-15 15:35:44 +0000 | [diff] [blame] | 184 | "'simple' of branch.autoSetupMerge\n" |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 185 | "in 'git help config'.\n"); |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 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" |
Ævar Arnfjörð Bjarmason | 8247166 | 2018-11-13 20:39:09 +0000 | [diff] [blame] | 194 | " git push %s HEAD\n" |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 195 | "%s%s"), |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 196 | remote->name, short_upstream, |
Tao Klerks | bdaf1df | 2022-04-29 09:56:44 +0000 | [diff] [blame] | 197 | remote->name, advice_pushdefault_maybe, |
| 198 | advice_automergesimple_maybe); |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Ramkumar Ramachandra | 35ee69c | 2013-05-30 00:51:49 +0530 | [diff] [blame] | 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 | |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 208 | static const char *get_upstream_ref(int flags, struct branch *branch, const char *remote_name) |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 209 | { |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 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 | } |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 224 | die(_("The current branch %s has no upstream branch.\n" |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 225 | "To push the current branch and set the remote as upstream, use\n" |
| 226 | "\n" |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 227 | " git push --set-upstream %s %s\n" |
| 228 | "%s"), |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 229 | branch->name, |
Felipe Contreras | 533e032 | 2021-05-31 14:51:12 -0500 | [diff] [blame] | 230 | remote_name, |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 231 | branch->name, |
| 232 | advice_autosetup_maybe); |
| 233 | } |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 234 | if (branch->merge_nr != 1) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 235 | die(_("The current branch %s has multiple upstream branches, " |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 236 | "refusing to push."), branch->name); |
Felipe Contreras | 533e032 | 2021-05-31 14:51:12 -0500 | [diff] [blame] | 237 | |
| 238 | return branch->merge[0]->src; |
| 239 | } |
| 240 | |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 241 | static void setup_default_push_refspecs(int *flags, struct remote *remote) |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 242 | { |
Felipe Contreras | 65c63a0 | 2021-05-31 14:51:16 -0500 | [diff] [blame] | 243 | struct branch *branch; |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 244 | const char *dst; |
Felipe Contreras | e0c91cf | 2021-05-31 14:51:23 -0500 | [diff] [blame] | 245 | int same_remote; |
Ramkumar Ramachandra | 7b2ecd8 | 2013-05-30 00:51:50 +0530 | [diff] [blame] | 246 | |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 247 | switch (push_default) { |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 248 | case PUSH_DEFAULT_MATCHING: |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 249 | refspec_append(&rs, ":"); |
Felipe Contreras | 7273968 | 2021-05-31 14:51:13 -0500 | [diff] [blame] | 250 | return; |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 251 | |
Felipe Contreras | 04159fb | 2021-05-31 14:51:14 -0500 | [diff] [blame] | 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 | |
Felipe Contreras | 65c63a0 | 2021-05-31 14:51:16 -0500 | [diff] [blame] | 260 | branch = branch_get(NULL); |
Felipe Contreras | cc16f95 | 2021-05-31 14:51:15 -0500 | [diff] [blame] | 261 | if (!branch) |
| 262 | die(_(message_detached_head_die), remote->name); |
| 263 | |
Felipe Contreras | 1f93472 | 2021-05-31 14:51:20 -0500 | [diff] [blame] | 264 | dst = branch->refname; |
Felipe Contreras | 7088ce7 | 2021-05-31 14:51:24 -0500 | [diff] [blame] | 265 | same_remote = !strcmp(remote->name, remote_for_branch(branch, NULL)); |
Felipe Contreras | 1f93472 | 2021-05-31 14:51:20 -0500 | [diff] [blame] | 266 | |
Felipe Contreras | 04159fb | 2021-05-31 14:51:14 -0500 | [diff] [blame] | 267 | switch (push_default) { |
| 268 | default: |
Junio C Hamano | b2ed944 | 2013-01-04 16:02:29 -0800 | [diff] [blame] | 269 | case PUSH_DEFAULT_UNSPECIFIED: |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 270 | case PUSH_DEFAULT_SIMPLE: |
Felipe Contreras | 1f93472 | 2021-05-31 14:51:20 -0500 | [diff] [blame] | 271 | if (!same_remote) |
| 272 | break; |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 273 | if (strcmp(branch->refname, get_upstream_ref(*flags, branch, remote->name))) |
Felipe Contreras | 1f93472 | 2021-05-31 14:51:20 -0500 | [diff] [blame] | 274 | die_push_simple(branch, remote); |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 275 | break; |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 276 | |
Johan Herland | 53c4031 | 2011-02-16 01:54:24 +0100 | [diff] [blame] | 277 | case PUSH_DEFAULT_UPSTREAM: |
Felipe Contreras | 0add899 | 2021-05-31 14:51:19 -0500 | [diff] [blame] | 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); |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 283 | dst = get_upstream_ref(*flags, branch, remote->name); |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 284 | break; |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 285 | |
| 286 | case PUSH_DEFAULT_CURRENT: |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 287 | break; |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 288 | } |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 289 | |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 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 | |
Felipe Contreras | 00458dc | 2021-05-31 14:51:17 -0500 | [diff] [blame] | 298 | refspec_appendf(&rs, "%s:%s", branch->refname, dst); |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 301 | static const char message_advice_pull_before_push[] = |
| 302 | N_("Updates were rejected because the tip of your current branch is behind\n" |
Alex Henrie | c577d65 | 2023-07-12 22:41:15 -0600 | [diff] [blame] | 303 | "its remote counterpart. If you want to integrate the remote changes,\n" |
| 304 | "use 'git pull' before pushing again.\n" |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 305 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
| 306 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 307 | static const char message_advice_checkout_pull_push[] = |
| 308 | N_("Updates were rejected because a pushed branch tip is behind its remote\n" |
Alex Henrie | c577d65 | 2023-07-12 22:41:15 -0600 | [diff] [blame] | 309 | "counterpart. If you want to integrate the remote changes, use 'git pull'\n" |
| 310 | "before pushing again.\n" |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 311 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
| 312 | |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 313 | static const char message_advice_ref_fetch_first[] = |
Alex Henrie | c577d65 | 2023-07-12 22:41:15 -0600 | [diff] [blame] | 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" |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 318 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
| 319 | |
Chris Rorvick | b24e604 | 2012-11-29 19:41:34 -0600 | [diff] [blame] | 320 | static const char message_advice_ref_already_exists[] = |
Junio C Hamano | b4cf8db | 2013-01-24 21:09:00 -0800 | [diff] [blame] | 321 | N_("Updates were rejected because the tag already exists in the remote."); |
Chris Rorvick | b24e604 | 2012-11-29 19:41:34 -0600 | [diff] [blame] | 322 | |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 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"); |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 327 | |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 328 | static const char message_advice_ref_needs_update[] = |
Alex Henrie | c577d65 | 2023-07-12 22:41:15 -0600 | [diff] [blame] | 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."); |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 333 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 334 | static void advise_pull_before_push(void) |
| 335 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 336 | if (!advice_enabled(ADVICE_PUSH_NON_FF_CURRENT) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 337 | return; |
| 338 | advise(_(message_advice_pull_before_push)); |
| 339 | } |
| 340 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 341 | static void advise_checkout_pull_push(void) |
| 342 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 343 | if (!advice_enabled(ADVICE_PUSH_NON_FF_MATCHING) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 344 | return; |
| 345 | advise(_(message_advice_checkout_pull_push)); |
| 346 | } |
| 347 | |
Chris Rorvick | b24e604 | 2012-11-29 19:41:34 -0600 | [diff] [blame] | 348 | static void advise_ref_already_exists(void) |
| 349 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 350 | if (!advice_enabled(ADVICE_PUSH_ALREADY_EXISTS) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Chris Rorvick | b450568 | 2012-12-02 21:27:51 -0600 | [diff] [blame] | 351 | return; |
Chris Rorvick | b24e604 | 2012-11-29 19:41:34 -0600 | [diff] [blame] | 352 | advise(_(message_advice_ref_already_exists)); |
| 353 | } |
| 354 | |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 355 | static void advise_ref_fetch_first(void) |
| 356 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 357 | if (!advice_enabled(ADVICE_PUSH_FETCH_FIRST) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 358 | return; |
| 359 | advise(_(message_advice_ref_fetch_first)); |
| 360 | } |
| 361 | |
| 362 | static void advise_ref_needs_force(void) |
| 363 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 364 | if (!advice_enabled(ADVICE_PUSH_NEEDS_FORCE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 365 | return; |
| 366 | advise(_(message_advice_ref_needs_force)); |
| 367 | } |
| 368 | |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 369 | static void advise_ref_needs_update(void) |
| 370 | { |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 371 | if (!advice_enabled(ADVICE_PUSH_REF_NEEDS_UPDATE) || !advice_enabled(ADVICE_PUSH_UPDATE_REJECTED)) |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 372 | return; |
| 373 | advise(_(message_advice_ref_needs_update)); |
| 374 | } |
| 375 | |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 376 | static int push_with_options(struct transport *transport, struct refspec *rs, |
| 377 | int flags) |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 378 | { |
| 379 | int err; |
Chris Rorvick | 10643d4 | 2012-11-29 19:41:33 -0600 | [diff] [blame] | 380 | unsigned int reject_reasons; |
Johannes Schindelin | d192fa5 | 2020-04-24 14:20:08 +0000 | [diff] [blame] | 381 | char *anon_url = transport_anonymize_url(transport->url); |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 382 | |
Tay Ray Chuan | 7838106 | 2010-02-24 20:50:27 +0800 | [diff] [blame] | 383 | transport_set_verbosity(transport, verbosity, progress); |
Eric Wong | c915f11 | 2016-02-03 04:09:14 +0000 | [diff] [blame] | 384 | transport->family = family; |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 385 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 386 | if (receivepack) |
| 387 | transport_set_option(transport, |
| 388 | TRANS_OPT_RECEIVEPACK, receivepack); |
Nguyễn Thái Ngọc Duy | f7c815c | 2013-08-12 20:55:55 +0700 | [diff] [blame] | 389 | transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 390 | |
Junio C Hamano | 91048a9 | 2013-07-09 11:01:06 -0700 | [diff] [blame] | 391 | if (!is_empty_cas(&cas)) { |
| 392 | if (!transport->smart_options) |
| 393 | die("underlying transport does not support --%s option", |
Junio C Hamano | a762af3 | 2023-12-19 11:26:25 -0800 | [diff] [blame] | 394 | "force-with-lease"); |
Junio C Hamano | 91048a9 | 2013-07-09 11:01:06 -0700 | [diff] [blame] | 395 | transport->smart_options->cas = &cas; |
| 396 | } |
| 397 | |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 398 | if (verbosity > 0) |
Johannes Schindelin | d192fa5 | 2020-04-24 14:20:08 +0000 | [diff] [blame] | 399 | fprintf(stderr, _("Pushing to %s\n"), anon_url); |
Josh Steadmon | 25e4b80 | 2019-10-02 16:49:29 -0700 | [diff] [blame] | 400 | trace2_region_enter("push", "transport_push", the_repository); |
Nguyễn Thái Ngọc Duy | 6c6d5d0 | 2018-11-10 06:48:55 +0100 | [diff] [blame] | 401 | err = transport_push(the_repository, transport, |
| 402 | rs, flags, &reject_reasons); |
Josh Steadmon | 25e4b80 | 2019-10-02 16:49:29 -0700 | [diff] [blame] | 403 | trace2_region_leave("push", "transport_push", the_repository); |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 404 | if (err != 0) { |
| 405 | fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR)); |
Johannes Schindelin | d192fa5 | 2020-04-24 14:20:08 +0000 | [diff] [blame] | 406 | error(_("failed to push some refs to '%s'"), anon_url); |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 407 | fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET)); |
| 408 | } |
Tay Ray Chuan | 53970b9 | 2009-12-04 07:31:44 +0800 | [diff] [blame] | 409 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 410 | err |= transport_disconnect(transport); |
Johannes Schindelin | d192fa5 | 2020-04-24 14:20:08 +0000 | [diff] [blame] | 411 | free(anon_url); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 412 | if (!err) |
| 413 | return 0; |
| 414 | |
Chris Rorvick | 10643d4 | 2012-11-29 19:41:33 -0600 | [diff] [blame] | 415 | if (reject_reasons & REJECT_NON_FF_HEAD) { |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 416 | advise_pull_before_push(); |
Chris Rorvick | 10643d4 | 2012-11-29 19:41:33 -0600 | [diff] [blame] | 417 | } else if (reject_reasons & REJECT_NON_FF_OTHER) { |
Junio C Hamano | b2ed944 | 2013-01-04 16:02:29 -0800 | [diff] [blame] | 418 | advise_checkout_pull_push(); |
Chris Rorvick | b24e604 | 2012-11-29 19:41:34 -0600 | [diff] [blame] | 419 | } else if (reject_reasons & REJECT_ALREADY_EXISTS) { |
| 420 | advise_ref_already_exists(); |
Junio C Hamano | 75e5c0d | 2013-01-23 13:55:30 -0800 | [diff] [blame] | 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(); |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 425 | } else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) { |
| 426 | advise_ref_needs_update(); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | return 1; |
| 430 | } |
| 431 | |
Jeff King | 5b9427e | 2020-09-30 08:29:09 -0400 | [diff] [blame] | 432 | static int do_push(int flags, |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 433 | const struct string_list *push_options, |
| 434 | struct remote *remote) |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 435 | { |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 436 | int i, errs; |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 437 | const char **url; |
| 438 | int url_nr; |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 439 | struct refspec *push_refspec = &rs; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 440 | |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 441 | if (push_options->nr) |
| 442 | flags |= TRANSPORT_PUSH_OPTIONS; |
| 443 | |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 444 | if (!push_refspec->nr && !(flags & TRANSPORT_PUSH_ALL)) { |
| 445 | if (remote->push.nr) { |
| 446 | push_refspec = &remote->push; |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 447 | } else if (!(flags & TRANSPORT_PUSH_MIRROR)) |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 448 | setup_default_push_refspecs(&flags, remote); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 449 | } |
Junio C Hamano | fd1d1b0 | 2007-04-06 23:04:53 -0700 | [diff] [blame] | 450 | errs = 0; |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 451 | url_nr = push_url_of_remote(remote, &url); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 452 | if (url_nr) { |
| 453 | for (i = 0; i < url_nr; i++) { |
| 454 | struct transport *transport = |
| 455 | transport_get(remote, url[i]); |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 456 | if (flags & TRANSPORT_PUSH_OPTIONS) |
| 457 | transport->push_options = push_options; |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 458 | if (push_with_options(transport, push_refspec, flags)) |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 459 | errs++; |
Matthieu Moy | 07436e4 | 2009-08-08 09:51:08 +0200 | [diff] [blame] | 460 | } |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 461 | } else { |
| 462 | struct transport *transport = |
| 463 | transport_get(remote, NULL); |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 464 | if (flags & TRANSPORT_PUSH_OPTIONS) |
| 465 | transport->push_options = push_options; |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 466 | if (push_with_options(transport, push_refspec, flags)) |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 467 | errs++; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 468 | } |
Junio C Hamano | fd1d1b0 | 2007-04-06 23:04:53 -0700 | [diff] [blame] | 469 | return !!errs; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 472 | static int option_parse_recurse_submodules(const struct option *opt, |
| 473 | const char *arg, int unset) |
| 474 | { |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 475 | int *recurse_submodules = opt->value; |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 476 | |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 477 | if (unset) |
| 478 | *recurse_submodules = RECURSE_SUBMODULES_OFF; |
Jonathan Tan | e62f779 | 2022-11-14 13:37:12 -0800 | [diff] [blame] | 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 | } |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
Dave Borowitz | 68c757f | 2015-08-19 11:26:47 -0400 | [diff] [blame] | 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 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 511 | static int git_push_config(const char *k, const char *v, |
| 512 | const struct config_context *ctx, void *cb) |
Michael J Gruber | b945901 | 2014-10-22 16:57:49 +0200 | [diff] [blame] | 513 | { |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 514 | const char *slot_name; |
Dave Olszewski | a8bc269 | 2015-02-16 01:16:19 -0500 | [diff] [blame] | 515 | int *flags = cb; |
Dave Olszewski | a8bc269 | 2015-02-16 01:16:19 -0500 | [diff] [blame] | 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; |
Tao Klerks | 05d5775 | 2022-04-29 09:56:46 +0000 | [diff] [blame] | 523 | } else if (!strcmp(k, "push.autosetupremote")) { |
| 524 | if (git_config_bool(k, v)) |
| 525 | *flags |= TRANSPORT_PUSH_AUTO_UPSTREAM; |
| 526 | return 0; |
Dave Borowitz | 68c757f | 2015-08-19 11:26:47 -0400 | [diff] [blame] | 527 | } else if (!strcmp(k, "push.gpgsign")) { |
Jeff King | 37e8a34 | 2023-12-07 02:26:22 -0500 | [diff] [blame] | 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); |
Dave Borowitz | 68c757f | 2015-08-19 11:26:47 -0400 | [diff] [blame] | 540 | } |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 541 | } else if (!strcmp(k, "push.recursesubmodules")) { |
Jeff King | 37e8a34 | 2023-12-07 02:26:22 -0500 | [diff] [blame] | 542 | recurse_submodules = parse_push_recurse_submodules_arg(k, v); |
Stefan Beller | 4e53d6a | 2017-05-31 17:30:49 -0700 | [diff] [blame] | 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; |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 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; |
Ryan Dammrose | 960786e | 2018-04-21 12:10:00 +0200 | [diff] [blame] | 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]); |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 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; |
Dave Olszewski | a8bc269 | 2015-02-16 01:16:19 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 574 | return git_default_config(k, v, ctx, NULL); |
Michael J Gruber | b945901 | 2014-10-22 16:57:49 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 577 | int cmd_push(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 578 | { |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 579 | int flags = 0; |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 580 | int tags = 0; |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 581 | int push_cert = -1; |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 582 | int rc; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 583 | const char *repo = NULL; /* default repository */ |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 584 | struct string_list push_options_cmdline = STRING_LIST_INIT_DUP; |
| 585 | struct string_list *push_options; |
Brandon Williams | 54cc8ac | 2017-03-31 16:56:22 -0700 | [diff] [blame] | 586 | const struct string_list_item *item; |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 587 | struct remote *remote; |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 588 | |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 589 | struct option options[] = { |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 590 | OPT__VERBOSITY(&verbosity), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 591 | OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")), |
Teng Long | 425b4d7 | 2023-05-06 19:34:08 +0800 | [diff] [blame] | 592 | OPT_BIT( 0 , "all", &flags, N_("push all branches"), TRANSPORT_PUSH_ALL), |
| 593 | OPT_ALIAS( 0 , "branches", "all"), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 594 | OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"), |
Michele Ballabio | c29c1b4 | 2008-07-20 14:02:20 +0200 | [diff] [blame] | 595 | (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)), |
Patrick Steinhardt | 38a2559 | 2015-12-14 16:23:04 +0100 | [diff] [blame] | 596 | OPT_BOOL('d', "delete", &deleterefs, N_("delete refs")), |
Teng Long | 425b4d7 | 2023-05-06 19:34:08 +0800 | [diff] [blame] | 597 | OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --branches or --mirror)")), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 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), |
Junio C Hamano | a762af3 | 2023-12-19 11:26:25 -0800 | [diff] [blame] | 601 | OPT_CALLBACK_F(0, "force-with-lease", &cas, N_("<refname>:<expect>"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 602 | N_("require old value of ref to be at this value"), |
| 603 | PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option), |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 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), |
Denton Liu | ce9baf2 | 2020-04-27 02:44:08 -0400 | [diff] [blame] | 607 | OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)", |
| 608 | N_("control recursive pushing of submodules"), option_parse_recurse_submodules), |
Nguyễn Thái Ngọc Duy | f1e1bdd | 2018-02-09 18:02:10 +0700 | [diff] [blame] | 609 | OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 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"), |
Ilari Liusvaara | e9fcd1e | 2010-01-16 23:45:31 +0200 | [diff] [blame] | 613 | TRANSPORT_PUSH_SET_UPSTREAM), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 614 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), |
| 615 | OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"), |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 616 | TRANSPORT_PUSH_PRUNE), |
Aaron Schrab | ec55559 | 2013-01-13 00:17:03 -0500 | [diff] [blame] | 617 | OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK), |
Junio C Hamano | c2aba15 | 2013-03-04 12:09:50 -0800 | [diff] [blame] | 618 | OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"), |
| 619 | TRANSPORT_PUSH_FOLLOW_TAGS), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 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), |
Jeff King | d16c33b | 2015-02-16 01:12:04 -0500 | [diff] [blame] | 622 | OPT_BIT(0, "atomic", &flags, N_("request atomic transaction on remote side"), TRANSPORT_PUSH_ATOMIC), |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 623 | OPT_STRING_LIST('o', "push-option", &push_options_cmdline, N_("server-specific"), N_("option to transmit")), |
Junio C Hamano | ae2c912 | 2023-07-18 14:34:33 -0700 | [diff] [blame] | 624 | OPT_IPVERSION(&family), |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 625 | OPT_END() |
| 626 | }; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 627 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 628 | packet_trace_identity("push"); |
Jeff King | 06c21e18 | 2015-02-16 01:13:25 -0500 | [diff] [blame] | 629 | git_config(git_push_config, &flags); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 630 | argc = parse_options(argc, argv, prefix, options, push_usage, 0); |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 631 | push_options = (push_options_cmdline.nr |
| 632 | ? &push_options_cmdline |
| 633 | : &push_options_config); |
Dave Borowitz | 68c757f | 2015-08-19 11:26:47 -0400 | [diff] [blame] | 634 | set_push_cert_flags(&flags, push_cert); |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 635 | |
René Scharfe | b3bf470 | 2023-12-06 12:51:55 +0100 | [diff] [blame] | 636 | die_for_incompatible_opt4(deleterefs, "--delete", |
| 637 | tags, "--tags", |
| 638 | flags & TRANSPORT_PUSH_ALL, "--all/--branches", |
| 639 | flags & TRANSPORT_PUSH_MIRROR, "--mirror"); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 640 | if (deleterefs && argc < 2) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 641 | die(_("--delete doesn't make sense without any refs")); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 642 | |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 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; |
Brandon Williams | 225e8bf | 2016-12-19 10:25:33 -0800 | [diff] [blame] | 647 | else if (recurse_submodules == RECURSE_SUBMODULES_ONLY) |
| 648 | flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY; |
Mike Crowe | b33a15b | 2015-11-17 11:05:56 +0000 | [diff] [blame] | 649 | |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 650 | if (tags) |
Brandon Williams | aa40289 | 2018-05-16 15:58:16 -0700 | [diff] [blame] | 651 | refspec_append(&rs, "refs/tags/*"); |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 652 | |
| 653 | if (argc > 0) { |
| 654 | repo = argv[0]; |
Junio C Hamano | ca02465 | 2013-12-03 15:41:15 -0800 | [diff] [blame] | 655 | set_refspecs(argv + 1, argc - 1, repo); |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 656 | } |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 657 | |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 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) { |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 676 | if (argc >= 2) |
| 677 | die(_("--all can't be combined with refspecs")); |
| 678 | } |
| 679 | if (flags & TRANSPORT_PUSH_MIRROR) { |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 680 | if (argc >= 2) |
| 681 | die(_("--mirror can't be combined with refspecs")); |
| 682 | } |
Thomas Gummerer | 8e4c8af | 2019-09-02 19:08:28 +0100 | [diff] [blame] | 683 | |
Srinidhi Kaushik | 3b990aa | 2020-10-03 17:40:45 +0530 | [diff] [blame] | 684 | if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)) |
| 685 | cas.use_force_if_includes = 1; |
| 686 | |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 687 | for_each_string_list_item(item, push_options) |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 688 | if (strchr(item->string, '\n')) |
| 689 | die(_("push options must not have new line characters")); |
| 690 | |
Jeff King | 5b9427e | 2020-09-30 08:29:09 -0400 | [diff] [blame] | 691 | rc = do_push(flags, push_options, remote); |
Marius Paliga | d805275 | 2017-10-23 13:44:49 +0200 | [diff] [blame] | 692 | string_list_clear(&push_options_cmdline, 0); |
| 693 | string_list_clear(&push_options_config, 0); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 694 | if (rc == -1) |
Andy Whitcroft | 94c89ba | 2007-11-09 23:32:25 +0000 | [diff] [blame] | 695 | usage_with_options(push_usage, options); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 696 | else |
| 697 | return rc; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 698 | } |