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