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