Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git push" |
| 3 | */ |
| 4 | #include "cache.h" |
| 5 | #include "refs.h" |
| 6 | #include "run-command.h" |
| 7 | #include "builtin.h" |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 8 | #include "remote.h" |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 9 | #include "transport.h" |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 10 | #include "parse-options.h" |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 11 | #include "submodule.h" |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 12 | |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 13 | static const char * const push_usage[] = { |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 14 | N_("git push [<options>] [<repository> [<refspec>...]]"), |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 15 | NULL, |
| 16 | }; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 17 | |
Michele Ballabio | c29c1b4 | 2008-07-20 14:02:20 +0200 | [diff] [blame] | 18 | static int thin; |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 19 | static int deleterefs; |
Uwe Kleine-König | d23842f | 2007-01-19 13:49:27 +0100 | [diff] [blame] | 20 | static const char *receivepack; |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 21 | static int verbosity; |
Clemens Buchacher | 01fdc21 | 2012-02-13 21:17:15 +0100 | [diff] [blame] | 22 | static int progress = -1; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 23 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 24 | static const char **refspec; |
| 25 | static int refspec_nr; |
Jared Hance | 8a883b0 | 2010-07-31 08:54:55 -0400 | [diff] [blame] | 26 | static int refspec_alloc; |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 27 | static int default_matching_used; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 28 | |
| 29 | static void add_refspec(const char *ref) |
| 30 | { |
Jared Hance | 8a883b0 | 2010-07-31 08:54:55 -0400 | [diff] [blame] | 31 | refspec_nr++; |
| 32 | ALLOC_GROW(refspec, refspec_nr, refspec_alloc); |
| 33 | refspec[refspec_nr-1] = ref; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 36 | static void set_refspecs(const char **refs, int nr) |
| 37 | { |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 38 | int i; |
| 39 | for (i = 0; i < nr; i++) { |
| 40 | const char *ref = refs[i]; |
| 41 | if (!strcmp("tag", ref)) { |
| 42 | char *tag; |
| 43 | int len; |
| 44 | if (nr <= ++i) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 45 | die(_("tag shorthand without <tag>")); |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 46 | len = strlen(refs[i]) + 11; |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 47 | if (deleterefs) { |
| 48 | tag = xmalloc(len+1); |
| 49 | strcpy(tag, ":refs/tags/"); |
| 50 | } else { |
| 51 | tag = xmalloc(len); |
| 52 | strcpy(tag, "refs/tags/"); |
| 53 | } |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 54 | strcat(tag, refs[i]); |
| 55 | ref = tag; |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 56 | } else if (deleterefs && !strchr(ref, ':')) { |
| 57 | char *delref; |
| 58 | int len = strlen(ref)+1; |
Jeff King | 7b48c17 | 2010-01-29 05:31:30 -0500 | [diff] [blame] | 59 | delref = xmalloc(len+1); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 60 | strcpy(delref, ":"); |
| 61 | strcat(delref, ref); |
| 62 | ref = delref; |
| 63 | } else if (deleterefs) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 64 | die(_("--delete only accepts plain target ref names")); |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 65 | add_refspec(ref); |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 66 | } |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 69 | static int push_url_of_remote(struct remote *remote, const char ***url_p) |
| 70 | { |
| 71 | if (remote->pushurl_nr) { |
| 72 | *url_p = remote->pushurl; |
| 73 | return remote->pushurl_nr; |
| 74 | } |
| 75 | *url_p = remote->url; |
| 76 | return remote->url_nr; |
| 77 | } |
| 78 | |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 79 | static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) { |
| 80 | /* |
| 81 | * There's no point in using shorten_unambiguous_ref here, |
| 82 | * as the ambiguity would be on the remote side, not what |
| 83 | * we have locally. Plus, this is supposed to be the simple |
| 84 | * mode. If the user is doing something crazy like setting |
| 85 | * upstream to a non-branch, we should probably be showing |
| 86 | * them the big ugly fully qualified ref. |
| 87 | */ |
| 88 | const char *advice_maybe = ""; |
| 89 | const char *short_upstream = |
| 90 | skip_prefix(branch->merge[0]->src, "refs/heads/"); |
| 91 | |
| 92 | if (!short_upstream) |
| 93 | short_upstream = branch->merge[0]->src; |
| 94 | /* |
| 95 | * Don't show advice for people who explicitely set |
| 96 | * push.default. |
| 97 | */ |
| 98 | if (push_default == PUSH_DEFAULT_UNSPECIFIED) |
| 99 | advice_maybe = _("\n" |
| 100 | "To choose either option permanently, " |
| 101 | "see push.default in 'git help config'."); |
| 102 | die(_("The upstream branch of your current branch does not match\n" |
| 103 | "the name of your current branch. To push to the upstream branch\n" |
| 104 | "on the remote, use\n" |
| 105 | "\n" |
| 106 | " git push %s HEAD:%s\n" |
| 107 | "\n" |
| 108 | "To push to the branch of the same name on the remote, use\n" |
| 109 | "\n" |
| 110 | " git push %s %s\n" |
| 111 | "%s"), |
| 112 | remote->name, short_upstream, |
| 113 | remote->name, branch->name, advice_maybe); |
| 114 | } |
| 115 | |
| 116 | static void setup_push_upstream(struct remote *remote, int simple) |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 117 | { |
| 118 | struct strbuf refspec = STRBUF_INIT; |
| 119 | struct branch *branch = branch_get(NULL); |
| 120 | if (!branch) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 121 | die(_("You are not currently on a branch.\n" |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 122 | "To push the history leading to the current (detached HEAD)\n" |
| 123 | "state now, use\n" |
| 124 | "\n" |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 125 | " git push %s HEAD:<name-of-remote-branch>\n"), |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 126 | remote->name); |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 127 | if (!branch->merge_nr || !branch->merge || !branch->remote_name) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 128 | die(_("The current branch %s has no upstream branch.\n" |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 129 | "To push the current branch and set the remote as upstream, use\n" |
| 130 | "\n" |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 131 | " git push --set-upstream %s %s\n"), |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 132 | branch->name, |
| 133 | remote->name, |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 134 | branch->name); |
| 135 | if (branch->merge_nr != 1) |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 136 | die(_("The current branch %s has multiple upstream branches, " |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 137 | "refusing to push."), branch->name); |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 138 | if (strcmp(branch->remote_name, remote->name)) |
| 139 | die(_("You are pushing to remote '%s', which is not the upstream of\n" |
| 140 | "your current branch '%s', without telling me what to push\n" |
| 141 | "to update which remote branch."), |
| 142 | remote->name, branch->name); |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 143 | if (simple && strcmp(branch->refname, branch->merge[0]->src)) |
| 144 | die_push_simple(branch, remote); |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 145 | |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 146 | strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src); |
| 147 | add_refspec(refspec.buf); |
| 148 | } |
| 149 | |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 150 | static void setup_default_push_refspecs(struct remote *remote) |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 151 | { |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 152 | switch (push_default) { |
Junio C Hamano | bba0fd2 | 2009-07-18 17:19:47 -0700 | [diff] [blame] | 153 | default: |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 154 | case PUSH_DEFAULT_UNSPECIFIED: |
| 155 | default_matching_used = 1; |
| 156 | /* fallthru */ |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 157 | case PUSH_DEFAULT_MATCHING: |
| 158 | add_refspec(":"); |
| 159 | break; |
| 160 | |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 161 | case PUSH_DEFAULT_SIMPLE: |
| 162 | setup_push_upstream(remote, 1); |
| 163 | break; |
| 164 | |
Johan Herland | 53c4031 | 2011-02-16 01:54:24 +0100 | [diff] [blame] | 165 | case PUSH_DEFAULT_UPSTREAM: |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 166 | setup_push_upstream(remote, 0); |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case PUSH_DEFAULT_CURRENT: |
| 170 | add_refspec("HEAD"); |
| 171 | break; |
| 172 | |
| 173 | case PUSH_DEFAULT_NOTHING: |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 174 | die(_("You didn't specify any refspecs to push, and " |
| 175 | "push.default is \"nothing\".")); |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 180 | static const char message_advice_pull_before_push[] = |
| 181 | N_("Updates were rejected because the tip of your current branch is behind\n" |
| 182 | "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n" |
| 183 | "before pushing again.\n" |
| 184 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
| 185 | |
| 186 | static const char message_advice_use_upstream[] = |
| 187 | N_("Updates were rejected because a pushed branch tip is behind its remote\n" |
| 188 | "counterpart. If you did not intend to push that branch, you may want to\n" |
| 189 | "specify branches to push or set the 'push.default' configuration\n" |
| 190 | "variable to 'current' or 'upstream' to push only the current branch."); |
| 191 | |
| 192 | static const char message_advice_checkout_pull_push[] = |
| 193 | N_("Updates were rejected because a pushed branch tip is behind its remote\n" |
| 194 | "counterpart. Check out this branch and merge the remote changes\n" |
| 195 | "(e.g. 'git pull') before pushing again.\n" |
| 196 | "See the 'Note about fast-forwards' in 'git push --help' for details."); |
| 197 | |
| 198 | static void advise_pull_before_push(void) |
| 199 | { |
| 200 | if (!advice_push_non_ff_current || !advice_push_nonfastforward) |
| 201 | return; |
| 202 | advise(_(message_advice_pull_before_push)); |
| 203 | } |
| 204 | |
| 205 | static void advise_use_upstream(void) |
| 206 | { |
| 207 | if (!advice_push_non_ff_default || !advice_push_nonfastforward) |
| 208 | return; |
| 209 | advise(_(message_advice_use_upstream)); |
| 210 | } |
| 211 | |
| 212 | static void advise_checkout_pull_push(void) |
| 213 | { |
| 214 | if (!advice_push_non_ff_matching || !advice_push_nonfastforward) |
| 215 | return; |
| 216 | advise(_(message_advice_checkout_pull_push)); |
| 217 | } |
| 218 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 219 | static int push_with_options(struct transport *transport, int flags) |
| 220 | { |
| 221 | int err; |
| 222 | int nonfastforward; |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 223 | |
Tay Ray Chuan | 7838106 | 2010-02-24 20:50:27 +0800 | [diff] [blame] | 224 | transport_set_verbosity(transport, verbosity, progress); |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 225 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 226 | if (receivepack) |
| 227 | transport_set_option(transport, |
| 228 | TRANS_OPT_RECEIVEPACK, receivepack); |
| 229 | if (thin) |
| 230 | transport_set_option(transport, TRANS_OPT_THIN, "yes"); |
| 231 | |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 232 | if (verbosity > 0) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 233 | fprintf(stderr, _("Pushing to %s\n"), transport->url); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 234 | err = transport_push(transport, refspec_nr, refspec, flags, |
| 235 | &nonfastforward); |
Tay Ray Chuan | 53970b9 | 2009-12-04 07:31:44 +0800 | [diff] [blame] | 236 | if (err != 0) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 237 | error(_("failed to push some refs to '%s'"), transport->url); |
Tay Ray Chuan | 53970b9 | 2009-12-04 07:31:44 +0800 | [diff] [blame] | 238 | |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 239 | err |= transport_disconnect(transport); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 240 | if (!err) |
| 241 | return 0; |
| 242 | |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 243 | switch (nonfastforward) { |
| 244 | default: |
| 245 | break; |
| 246 | case NON_FF_HEAD: |
| 247 | advise_pull_before_push(); |
| 248 | break; |
| 249 | case NON_FF_OTHER: |
| 250 | if (default_matching_used) |
| 251 | advise_use_upstream(); |
| 252 | else |
| 253 | advise_checkout_pull_push(); |
| 254 | break; |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return 1; |
| 258 | } |
| 259 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 260 | static int do_push(const char *repo, int flags) |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 261 | { |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 262 | int i, errs; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 263 | struct remote *remote = remote_get(repo); |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 264 | const char **url; |
| 265 | int url_nr; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 266 | |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 267 | if (!remote) { |
| 268 | if (repo) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 269 | die(_("bad repository '%s'"), repo); |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 270 | die(_("No configured push destination.\n" |
Matthieu Moy | a3f5e7a | 2011-03-02 21:12:11 +0100 | [diff] [blame] | 271 | "Either specify the URL from the command-line or configure a remote repository using\n" |
| 272 | "\n" |
| 273 | " git remote add <name> <url>\n" |
| 274 | "\n" |
| 275 | "and then push using the remote name\n" |
| 276 | "\n" |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 277 | " git push <name>\n")); |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 278 | } |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 279 | |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 280 | if (remote->mirror) |
| 281 | flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE); |
| 282 | |
Marek Zawirski | b259f09 | 2008-08-16 19:58:32 +0200 | [diff] [blame] | 283 | if ((flags & TRANSPORT_PUSH_ALL) && refspec) { |
| 284 | if (!strcmp(*refspec, "refs/tags/*")) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 285 | return error(_("--all and --tags are incompatible")); |
| 286 | return error(_("--all can't be combined with refspecs")); |
Marek Zawirski | b259f09 | 2008-08-16 19:58:32 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) { |
| 290 | if (!strcmp(*refspec, "refs/tags/*")) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 291 | return error(_("--mirror and --tags are incompatible")); |
| 292 | return error(_("--mirror can't be combined with refspecs")); |
Marek Zawirski | b259f09 | 2008-08-16 19:58:32 +0200 | [diff] [blame] | 293 | } |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 294 | |
| 295 | if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) == |
| 296 | (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) { |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 297 | return error(_("--all and --mirror are incompatible")); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 300 | if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) { |
| 301 | if (remote->push_refspec_nr) { |
| 302 | refspec = remote->push_refspec; |
| 303 | refspec_nr = remote->push_refspec_nr; |
| 304 | } else if (!(flags & TRANSPORT_PUSH_MIRROR)) |
Matthieu Moy | ec8460b | 2011-03-02 21:12:10 +0100 | [diff] [blame] | 305 | setup_default_push_refspecs(remote); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 306 | } |
Junio C Hamano | fd1d1b0 | 2007-04-06 23:04:53 -0700 | [diff] [blame] | 307 | errs = 0; |
Junio C Hamano | 135dade | 2012-03-30 16:07:12 -0700 | [diff] [blame] | 308 | url_nr = push_url_of_remote(remote, &url); |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 309 | if (url_nr) { |
| 310 | for (i = 0; i < url_nr; i++) { |
| 311 | struct transport *transport = |
| 312 | transport_get(remote, url[i]); |
| 313 | if (push_with_options(transport, flags)) |
| 314 | errs++; |
Matthieu Moy | 07436e4 | 2009-08-08 09:51:08 +0200 | [diff] [blame] | 315 | } |
Daniel Barkalow | fb0cc87 | 2009-11-18 02:42:22 +0100 | [diff] [blame] | 316 | } else { |
| 317 | struct transport *transport = |
| 318 | transport_get(remote, NULL); |
| 319 | |
| 320 | if (push_with_options(transport, flags)) |
| 321 | errs++; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 322 | } |
Junio C Hamano | fd1d1b0 | 2007-04-06 23:04:53 -0700 | [diff] [blame] | 323 | return !!errs; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 326 | static int option_parse_recurse_submodules(const struct option *opt, |
| 327 | const char *arg, int unset) |
| 328 | { |
| 329 | int *flags = opt->value; |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 330 | |
| 331 | if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK | |
| 332 | TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND)) |
| 333 | die("%s can only be used once.", opt->long_name); |
| 334 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 335 | if (arg) { |
| 336 | if (!strcmp(arg, "check")) |
| 337 | *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK; |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 338 | else if (!strcmp(arg, "on-demand")) |
| 339 | *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND; |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 340 | else |
| 341 | die("bad %s argument: %s", opt->long_name, arg); |
| 342 | } else |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 343 | die("option %s needs an argument (check|on-demand)", |
| 344 | opt->long_name); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 349 | int cmd_push(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 350 | { |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 351 | int flags = 0; |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 352 | int tags = 0; |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 353 | int rc; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 354 | const char *repo = NULL; /* default repository */ |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 355 | struct option options[] = { |
Tay Ray Chuan | 8afd8dc | 2010-02-24 20:50:24 +0800 | [diff] [blame] | 356 | OPT__VERBOSITY(&verbosity), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 357 | OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")), |
| 358 | OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL), |
| 359 | OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"), |
Michele Ballabio | c29c1b4 | 2008-07-20 14:02:20 +0200 | [diff] [blame] | 360 | (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 361 | OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")), |
| 362 | OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")), |
| 363 | OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN), |
| 364 | OPT_BIT( 0, "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN), |
| 365 | OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE), |
| 366 | { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"), |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame^] | 367 | N_("control recursive pushing of submodules"), |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 368 | PARSE_OPT_OPTARG, option_parse_recurse_submodules }, |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 369 | OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")), |
| 370 | OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")), |
| 371 | OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")), |
| 372 | 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] | 373 | TRANSPORT_PUSH_SET_UPSTREAM), |
Nguyễn Thái Ngọc Duy | 78dafaa | 2012-08-20 19:32:33 +0700 | [diff] [blame] | 374 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), |
| 375 | OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"), |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 376 | TRANSPORT_PUSH_PRUNE), |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 377 | OPT_END() |
| 378 | }; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 379 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 380 | packet_trace_identity("push"); |
Jeff King | 2aae905 | 2009-10-25 15:15:22 -0400 | [diff] [blame] | 381 | git_config(git_default_config, NULL); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 382 | argc = parse_options(argc, argv, prefix, options, push_usage, 0); |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 383 | |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 384 | if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR)))) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 385 | die(_("--delete is incompatible with --all, --mirror and --tags")); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 386 | if (deleterefs && argc < 2) |
Ævar Arnfjörð Bjarmason | 8352d29 | 2011-02-22 23:42:11 +0000 | [diff] [blame] | 387 | die(_("--delete doesn't make sense without any refs")); |
Jan Krüger | f517f1f | 2009-12-30 20:57:42 +0100 | [diff] [blame] | 388 | |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 389 | if (tags) |
| 390 | add_refspec("refs/tags/*"); |
Daniel Barkalow | 378c483 | 2007-11-04 22:35:37 -0500 | [diff] [blame] | 391 | |
| 392 | if (argc > 0) { |
| 393 | repo = argv[0]; |
| 394 | set_refspecs(argv + 1, argc - 1); |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 395 | } |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 396 | |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 397 | rc = do_push(repo, flags); |
| 398 | if (rc == -1) |
Andy Whitcroft | 94c89ba | 2007-11-09 23:32:25 +0000 | [diff] [blame] | 399 | usage_with_options(push_usage, options); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 400 | else |
| 401 | return rc; |
Linus Torvalds | 755225d | 2006-04-29 21:22:49 -0700 | [diff] [blame] | 402 | } |