76e4400c4ab73cacc5d39a27bf1688fb3af21425
   1/*
   2 * "git push"
   3 */
   4#include "cache.h"
   5#include "refs.h"
   6#include "run-command.h"
   7#include "builtin.h"
   8#include "remote.h"
   9#include "transport.h"
  10#include "parse-options.h"
  11#include "submodule.h"
  12
  13static const char * const push_usage[] = {
  14        N_("git push [<options>] [<repository> [<refspec>...]]"),
  15        NULL,
  16};
  17
  18static int thin = 1;
  19static int deleterefs;
  20static const char *receivepack;
  21static int verbosity;
  22static int progress = -1;
  23
  24static struct push_cas_option cas;
  25
  26static const char **refspec;
  27static int refspec_nr;
  28static int refspec_alloc;
  29static int default_matching_used;
  30
  31static void add_refspec(const char *ref)
  32{
  33        refspec_nr++;
  34        ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
  35        refspec[refspec_nr-1] = ref;
  36}
  37
  38static void set_refspecs(const char **refs, int nr)
  39{
  40        int i;
  41        for (i = 0; i < nr; i++) {
  42                const char *ref = refs[i];
  43                if (!strcmp("tag", ref)) {
  44                        struct strbuf tagref = STRBUF_INIT;
  45                        if (nr <= ++i)
  46                                die(_("tag shorthand without <tag>"));
  47                        ref = refs[i];
  48                        if (deleterefs)
  49                                strbuf_addf(&tagref, ":refs/tags/%s", ref);
  50                        else
  51                                strbuf_addf(&tagref, "refs/tags/%s", ref);
  52                        ref = strbuf_detach(&tagref, NULL);
  53                } else if (deleterefs) {
  54                        struct strbuf delref = STRBUF_INIT;
  55                        if (strchr(ref, ':'))
  56                                die(_("--delete only accepts plain target ref names"));
  57                        strbuf_addf(&delref, ":%s", ref);
  58                        ref = strbuf_detach(&delref, NULL);
  59                }
  60                add_refspec(ref);
  61        }
  62}
  63
  64static int push_url_of_remote(struct remote *remote, const char ***url_p)
  65{
  66        if (remote->pushurl_nr) {
  67                *url_p = remote->pushurl;
  68                return remote->pushurl_nr;
  69        }
  70        *url_p = remote->url;
  71        return remote->url_nr;
  72}
  73
  74static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
  75        /*
  76         * There's no point in using shorten_unambiguous_ref here,
  77         * as the ambiguity would be on the remote side, not what
  78         * we have locally. Plus, this is supposed to be the simple
  79         * mode. If the user is doing something crazy like setting
  80         * upstream to a non-branch, we should probably be showing
  81         * them the big ugly fully qualified ref.
  82         */
  83        const char *advice_maybe = "";
  84        const char *short_upstream =
  85                skip_prefix(branch->merge[0]->src, "refs/heads/");
  86
  87        if (!short_upstream)
  88                short_upstream = branch->merge[0]->src;
  89        /*
  90         * Don't show advice for people who explicitly set
  91         * push.default.
  92         */
  93        if (push_default == PUSH_DEFAULT_UNSPECIFIED)
  94                advice_maybe = _("\n"
  95                                 "To choose either option permanently, "
  96                                 "see push.default in 'git help config'.");
  97        die(_("The upstream branch of your current branch does not match\n"
  98              "the name of your current branch.  To push to the upstream branch\n"
  99              "on the remote, use\n"
 100              "\n"
 101              "    git push %s HEAD:%s\n"
 102              "\n"
 103              "To push to the branch of the same name on the remote, use\n"
 104              "\n"
 105              "    git push %s %s\n"
 106              "%s"),
 107            remote->name, short_upstream,
 108            remote->name, branch->name, advice_maybe);
 109}
 110
 111static const char message_detached_head_die[] =
 112        N_("You are not currently on a branch.\n"
 113           "To push the history leading to the current (detached HEAD)\n"
 114           "state now, use\n"
 115           "\n"
 116           "    git push %s HEAD:<name-of-remote-branch>\n");
 117
 118static void setup_push_upstream(struct remote *remote, struct branch *branch,
 119                                int triangular)
 120{
 121        struct strbuf refspec = STRBUF_INIT;
 122
 123        if (!branch)
 124                die(_(message_detached_head_die), remote->name);
 125        if (!branch->merge_nr || !branch->merge || !branch->remote_name)
 126                die(_("The current branch %s has no upstream branch.\n"
 127                    "To push the current branch and set the remote as upstream, use\n"
 128                    "\n"
 129                    "    git push --set-upstream %s %s\n"),
 130                    branch->name,
 131                    remote->name,
 132                    branch->name);
 133        if (branch->merge_nr != 1)
 134                die(_("The current branch %s has multiple upstream branches, "
 135                    "refusing to push."), branch->name);
 136        if (triangular)
 137                die(_("You are pushing to remote '%s', which is not the upstream of\n"
 138                      "your current branch '%s', without telling me what to push\n"
 139                      "to update which remote branch."),
 140                    remote->name, branch->name);
 141
 142        if (push_default == PUSH_DEFAULT_SIMPLE) {
 143                /* Additional safety */
 144                if (strcmp(branch->refname, branch->merge[0]->src))
 145                        die_push_simple(branch, remote);
 146        }
 147
 148        strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
 149        add_refspec(refspec.buf);
 150}
 151
 152static void setup_push_current(struct remote *remote, struct branch *branch)
 153{
 154        if (!branch)
 155                die(_(message_detached_head_die), remote->name);
 156        add_refspec(branch->name);
 157}
 158
 159static char warn_unspecified_push_default_msg[] =
 160N_("push.default is unset; its implicit value is changing in\n"
 161   "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
 162   "and maintain the current behavior after the default changes, use:\n"
 163   "\n"
 164   "  git config --global push.default matching\n"
 165   "\n"
 166   "To squelch this message and adopt the new behavior now, use:\n"
 167   "\n"
 168   "  git config --global push.default simple\n"
 169   "\n"
 170   "See 'git help config' and search for 'push.default' for further information.\n"
 171   "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
 172   "'current' instead of 'simple' if you sometimes use older versions of Git)");
 173
 174static void warn_unspecified_push_default_configuration(void)
 175{
 176        static int warn_once;
 177
 178        if (warn_once++)
 179                return;
 180        warning("%s\n", _(warn_unspecified_push_default_msg));
 181}
 182
 183static int is_workflow_triangular(struct remote *remote)
 184{
 185        struct remote *fetch_remote = remote_get(NULL);
 186        return (fetch_remote && fetch_remote != remote);
 187}
 188
 189static void setup_default_push_refspecs(struct remote *remote)
 190{
 191        struct branch *branch = branch_get(NULL);
 192        int triangular = is_workflow_triangular(remote);
 193
 194        switch (push_default) {
 195        default:
 196        case PUSH_DEFAULT_UNSPECIFIED:
 197                default_matching_used = 1;
 198                warn_unspecified_push_default_configuration();
 199                /* fallthru */
 200        case PUSH_DEFAULT_MATCHING:
 201                add_refspec(":");
 202                break;
 203
 204        case PUSH_DEFAULT_SIMPLE:
 205                if (triangular)
 206                        setup_push_current(remote, branch);
 207                else
 208                        setup_push_upstream(remote, branch, triangular);
 209                break;
 210
 211        case PUSH_DEFAULT_UPSTREAM:
 212                setup_push_upstream(remote, branch, triangular);
 213                break;
 214
 215        case PUSH_DEFAULT_CURRENT:
 216                setup_push_current(remote, branch);
 217                break;
 218
 219        case PUSH_DEFAULT_NOTHING:
 220                die(_("You didn't specify any refspecs to push, and "
 221                    "push.default is \"nothing\"."));
 222                break;
 223        }
 224}
 225
 226static const char message_advice_pull_before_push[] =
 227        N_("Updates were rejected because the tip of your current branch is behind\n"
 228           "its remote counterpart. Integrate the remote changes (e.g.\n"
 229           "'git pull ...') before pushing again.\n"
 230           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 231
 232static const char message_advice_use_upstream[] =
 233        N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 234           "counterpart. If you did not intend to push that branch, you may want to\n"
 235           "specify branches to push or set the 'push.default' configuration variable\n"
 236           "to 'simple', 'current' or 'upstream' to push only the current branch.");
 237
 238static const char message_advice_checkout_pull_push[] =
 239        N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 240           "counterpart. Check out this branch and integrate the remote changes\n"
 241           "(e.g. 'git pull ...') before pushing again.\n"
 242           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 243
 244static const char message_advice_ref_fetch_first[] =
 245        N_("Updates were rejected because the remote contains work that you do\n"
 246           "not have locally. This is usually caused by another repository pushing\n"
 247           "to the same ref. You may want to first integrate the remote changes\n"
 248           "(e.g., 'git pull ...') before pushing again.\n"
 249           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 250
 251static const char message_advice_ref_already_exists[] =
 252        N_("Updates were rejected because the tag already exists in the remote.");
 253
 254static const char message_advice_ref_needs_force[] =
 255        N_("You cannot update a remote ref that points at a non-commit object,\n"
 256           "or update a remote ref to make it point at a non-commit object,\n"
 257           "without using the '--force' option.\n");
 258
 259static void advise_pull_before_push(void)
 260{
 261        if (!advice_push_non_ff_current || !advice_push_update_rejected)
 262                return;
 263        advise(_(message_advice_pull_before_push));
 264}
 265
 266static void advise_use_upstream(void)
 267{
 268        if (!advice_push_non_ff_default || !advice_push_update_rejected)
 269                return;
 270        advise(_(message_advice_use_upstream));
 271}
 272
 273static void advise_checkout_pull_push(void)
 274{
 275        if (!advice_push_non_ff_matching || !advice_push_update_rejected)
 276                return;
 277        advise(_(message_advice_checkout_pull_push));
 278}
 279
 280static void advise_ref_already_exists(void)
 281{
 282        if (!advice_push_already_exists || !advice_push_update_rejected)
 283                return;
 284        advise(_(message_advice_ref_already_exists));
 285}
 286
 287static void advise_ref_fetch_first(void)
 288{
 289        if (!advice_push_fetch_first || !advice_push_update_rejected)
 290                return;
 291        advise(_(message_advice_ref_fetch_first));
 292}
 293
 294static void advise_ref_needs_force(void)
 295{
 296        if (!advice_push_needs_force || !advice_push_update_rejected)
 297                return;
 298        advise(_(message_advice_ref_needs_force));
 299}
 300
 301static int push_with_options(struct transport *transport, int flags)
 302{
 303        int err;
 304        unsigned int reject_reasons;
 305
 306        transport_set_verbosity(transport, verbosity, progress);
 307
 308        if (receivepack)
 309                transport_set_option(transport,
 310                                     TRANS_OPT_RECEIVEPACK, receivepack);
 311        transport_set_option(transport, TRANS_OPT_THIN, thin ? "yes" : NULL);
 312
 313        if (!is_empty_cas(&cas)) {
 314                if (!transport->smart_options)
 315                        die("underlying transport does not support --%s option",
 316                            CAS_OPT_NAME);
 317                transport->smart_options->cas = &cas;
 318        }
 319
 320        if (verbosity > 0)
 321                fprintf(stderr, _("Pushing to %s\n"), transport->url);
 322        err = transport_push(transport, refspec_nr, refspec, flags,
 323                             &reject_reasons);
 324        if (err != 0)
 325                error(_("failed to push some refs to '%s'"), transport->url);
 326
 327        err |= transport_disconnect(transport);
 328        if (!err)
 329                return 0;
 330
 331        if (reject_reasons & REJECT_NON_FF_HEAD) {
 332                advise_pull_before_push();
 333        } else if (reject_reasons & REJECT_NON_FF_OTHER) {
 334                if (default_matching_used)
 335                        advise_use_upstream();
 336                else
 337                        advise_checkout_pull_push();
 338        } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
 339                advise_ref_already_exists();
 340        } else if (reject_reasons & REJECT_FETCH_FIRST) {
 341                advise_ref_fetch_first();
 342        } else if (reject_reasons & REJECT_NEEDS_FORCE) {
 343                advise_ref_needs_force();
 344        }
 345
 346        return 1;
 347}
 348
 349static int do_push(const char *repo, int flags)
 350{
 351        int i, errs;
 352        struct remote *remote = pushremote_get(repo);
 353        const char **url;
 354        int url_nr;
 355
 356        if (!remote) {
 357                if (repo)
 358                        die(_("bad repository '%s'"), repo);
 359                die(_("No configured push destination.\n"
 360                    "Either specify the URL from the command-line or configure a remote repository using\n"
 361                    "\n"
 362                    "    git remote add <name> <url>\n"
 363                    "\n"
 364                    "and then push using the remote name\n"
 365                    "\n"
 366                    "    git push <name>\n"));
 367        }
 368
 369        if (remote->mirror)
 370                flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
 371
 372        if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
 373                if (!strcmp(*refspec, "refs/tags/*"))
 374                        return error(_("--all and --tags are incompatible"));
 375                return error(_("--all can't be combined with refspecs"));
 376        }
 377
 378        if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
 379                if (!strcmp(*refspec, "refs/tags/*"))
 380                        return error(_("--mirror and --tags are incompatible"));
 381                return error(_("--mirror can't be combined with refspecs"));
 382        }
 383
 384        if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
 385                                (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
 386                return error(_("--all and --mirror are incompatible"));
 387        }
 388
 389        if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
 390                if (remote->push_refspec_nr) {
 391                        refspec = remote->push_refspec;
 392                        refspec_nr = remote->push_refspec_nr;
 393                } else if (!(flags & TRANSPORT_PUSH_MIRROR))
 394                        setup_default_push_refspecs(remote);
 395        }
 396        errs = 0;
 397        url_nr = push_url_of_remote(remote, &url);
 398        if (url_nr) {
 399                for (i = 0; i < url_nr; i++) {
 400                        struct transport *transport =
 401                                transport_get(remote, url[i]);
 402                        if (push_with_options(transport, flags))
 403                                errs++;
 404                }
 405        } else {
 406                struct transport *transport =
 407                        transport_get(remote, NULL);
 408
 409                if (push_with_options(transport, flags))
 410                        errs++;
 411        }
 412        return !!errs;
 413}
 414
 415static int option_parse_recurse_submodules(const struct option *opt,
 416                                   const char *arg, int unset)
 417{
 418        int *flags = opt->value;
 419
 420        if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
 421                      TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
 422                die("%s can only be used once.", opt->long_name);
 423
 424        if (arg) {
 425                if (!strcmp(arg, "check"))
 426                        *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
 427                else if (!strcmp(arg, "on-demand"))
 428                        *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
 429                else
 430                        die("bad %s argument: %s", opt->long_name, arg);
 431        } else
 432                die("option %s needs an argument (check|on-demand)",
 433                                opt->long_name);
 434
 435        return 0;
 436}
 437
 438int cmd_push(int argc, const char **argv, const char *prefix)
 439{
 440        int flags = 0;
 441        int tags = 0;
 442        int rc;
 443        const char *repo = NULL;        /* default repository */
 444        struct option options[] = {
 445                OPT__VERBOSITY(&verbosity),
 446                OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
 447                OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
 448                OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
 449                            (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
 450                OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
 451                OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
 452                OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
 453                OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
 454                OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
 455                { OPTION_CALLBACK,
 456                  0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
 457                  N_("require old value of ref to be at this value"),
 458                  PARSE_OPT_OPTARG, parseopt_push_cas_option },
 459                { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
 460                        N_("control recursive pushing of submodules"),
 461                        PARSE_OPT_OPTARG, option_parse_recurse_submodules },
 462                OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
 463                OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
 464                OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
 465                OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
 466                        TRANSPORT_PUSH_SET_UPSTREAM),
 467                OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
 468                OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
 469                        TRANSPORT_PUSH_PRUNE),
 470                OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
 471                OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
 472                        TRANSPORT_PUSH_FOLLOW_TAGS),
 473                OPT_END()
 474        };
 475
 476        packet_trace_identity("push");
 477        git_config(git_default_config, NULL);
 478        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
 479
 480        if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
 481                die(_("--delete is incompatible with --all, --mirror and --tags"));
 482        if (deleterefs && argc < 2)
 483                die(_("--delete doesn't make sense without any refs"));
 484
 485        if (tags)
 486                add_refspec("refs/tags/*");
 487
 488        if (argc > 0) {
 489                repo = argv[0];
 490                set_refspecs(argv + 1, argc - 1);
 491        }
 492
 493        rc = do_push(repo, flags);
 494        if (rc == -1)
 495                usage_with_options(push_usage, options);
 496        else
 497                return rc;
 498}