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