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