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