builtin / push.con commit push: switch default from "matching" to "simple" (b2ed944)
   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;
  19static int deleterefs;
  20static const char *receivepack;
  21static int verbosity;
  22static int progress = -1;
  23
  24static const char **refspec;
  25static int refspec_nr;
  26static int refspec_alloc;
  27
  28static void add_refspec(const char *ref)
  29{
  30        refspec_nr++;
  31        ALLOC_GROW(refspec, refspec_nr, refspec_alloc);
  32        refspec[refspec_nr-1] = ref;
  33}
  34
  35static void set_refspecs(const char **refs, int nr)
  36{
  37        int i;
  38        for (i = 0; i < nr; i++) {
  39                const char *ref = refs[i];
  40                if (!strcmp("tag", ref)) {
  41                        char *tag;
  42                        int len;
  43                        if (nr <= ++i)
  44                                die(_("tag shorthand without <tag>"));
  45                        len = strlen(refs[i]) + 11;
  46                        if (deleterefs) {
  47                                tag = xmalloc(len+1);
  48                                strcpy(tag, ":refs/tags/");
  49                        } else {
  50                                tag = xmalloc(len);
  51                                strcpy(tag, "refs/tags/");
  52                        }
  53                        strcat(tag, refs[i]);
  54                        ref = tag;
  55                } else if (deleterefs && !strchr(ref, ':')) {
  56                        char *delref;
  57                        int len = strlen(ref)+1;
  58                        delref = xmalloc(len+1);
  59                        strcpy(delref, ":");
  60                        strcat(delref, ref);
  61                        ref = delref;
  62                } else if (deleterefs)
  63                        die(_("--delete only accepts plain target ref names"));
  64                add_refspec(ref);
  65        }
  66}
  67
  68static int push_url_of_remote(struct remote *remote, const char ***url_p)
  69{
  70        if (remote->pushurl_nr) {
  71                *url_p = remote->pushurl;
  72                return remote->pushurl_nr;
  73        }
  74        *url_p = remote->url;
  75        return remote->url_nr;
  76}
  77
  78static NORETURN int die_push_simple(struct branch *branch, struct remote *remote) {
  79        /*
  80         * There's no point in using shorten_unambiguous_ref here,
  81         * as the ambiguity would be on the remote side, not what
  82         * we have locally. Plus, this is supposed to be the simple
  83         * mode. If the user is doing something crazy like setting
  84         * upstream to a non-branch, we should probably be showing
  85         * them the big ugly fully qualified ref.
  86         */
  87        const char *advice_maybe = "";
  88        const char *short_upstream =
  89                skip_prefix(branch->merge[0]->src, "refs/heads/");
  90
  91        if (!short_upstream)
  92                short_upstream = branch->merge[0]->src;
  93        /*
  94         * Don't show advice for people who explicitely set
  95         * push.default.
  96         */
  97        if (push_default == PUSH_DEFAULT_UNSPECIFIED)
  98                advice_maybe = _("\n"
  99                                 "To choose either option permanently, "
 100                                 "see push.default in 'git help config'.");
 101        die(_("The upstream branch of your current branch does not match\n"
 102              "the name of your current branch.  To push to the upstream branch\n"
 103              "on the remote, use\n"
 104              "\n"
 105              "    git push %s HEAD:%s\n"
 106              "\n"
 107              "To push to the branch of the same name on the remote, use\n"
 108              "\n"
 109              "    git push %s %s\n"
 110              "%s"),
 111            remote->name, short_upstream,
 112            remote->name, branch->name, advice_maybe);
 113}
 114
 115static void setup_push_upstream(struct remote *remote, int simple)
 116{
 117        struct strbuf refspec = STRBUF_INIT;
 118        struct branch *branch = branch_get(NULL);
 119        if (!branch)
 120                die(_("You are not currently on a branch.\n"
 121                    "To push the history leading to the current (detached HEAD)\n"
 122                    "state now, use\n"
 123                    "\n"
 124                    "    git push %s HEAD:<name-of-remote-branch>\n"),
 125                    remote->name);
 126        if (!branch->merge_nr || !branch->merge || !branch->remote_name)
 127                die(_("The current branch %s has no upstream branch.\n"
 128                    "To push the current branch and set the remote as upstream, use\n"
 129                    "\n"
 130                    "    git push --set-upstream %s %s\n"),
 131                    branch->name,
 132                    remote->name,
 133                    branch->name);
 134        if (branch->merge_nr != 1)
 135                die(_("The current branch %s has multiple upstream branches, "
 136                    "refusing to push."), branch->name);
 137        if (strcmp(branch->remote_name, remote->name))
 138                die(_("You are pushing to remote '%s', which is not the upstream of\n"
 139                      "your current branch '%s', without telling me what to push\n"
 140                      "to update which remote branch."),
 141                    remote->name, branch->name);
 142        if (simple && strcmp(branch->refname, branch->merge[0]->src))
 143                die_push_simple(branch, remote);
 144
 145        strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
 146        add_refspec(refspec.buf);
 147}
 148
 149static char warn_unspecified_push_default_msg[] =
 150N_("push.default is unset; its implicit value has changed in\n"
 151   "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
 152   "and maintain the traditional behavior, use:\n"
 153   "\n"
 154   "  git config --global push.default matching\n"
 155   "\n"
 156   "To squelch this message and adopt the new behavior now, use:\n"
 157   "\n"
 158   "  git config --global push.default simple\n"
 159   "\n"
 160   "See 'git help config' and search for 'push.default' for further information.\n"
 161   "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
 162   "'current' instead of 'simple' if you sometimes use older versions of Git)");
 163
 164static void warn_unspecified_push_default_configuration(void)
 165{
 166        static int warn_once;
 167
 168        if (warn_once++)
 169                return;
 170        warning("%s\n", _(warn_unspecified_push_default_msg));
 171}
 172
 173static void setup_default_push_refspecs(struct remote *remote)
 174{
 175        switch (push_default) {
 176        default:
 177        case PUSH_DEFAULT_MATCHING:
 178                add_refspec(":");
 179                break;
 180
 181        case PUSH_DEFAULT_UNSPECIFIED:
 182                warn_unspecified_push_default_configuration();
 183                /* fallthru */
 184
 185        case PUSH_DEFAULT_SIMPLE:
 186                setup_push_upstream(remote, 1);
 187                break;
 188
 189        case PUSH_DEFAULT_UPSTREAM:
 190                setup_push_upstream(remote, 0);
 191                break;
 192
 193        case PUSH_DEFAULT_CURRENT:
 194                add_refspec("HEAD");
 195                break;
 196
 197        case PUSH_DEFAULT_NOTHING:
 198                die(_("You didn't specify any refspecs to push, and "
 199                    "push.default is \"nothing\"."));
 200                break;
 201        }
 202}
 203
 204static const char message_advice_pull_before_push[] =
 205        N_("Updates were rejected because the tip of your current branch is behind\n"
 206           "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
 207           "before pushing again.\n"
 208           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 209
 210static const char message_advice_checkout_pull_push[] =
 211        N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 212           "counterpart. Check out this branch and merge the remote changes\n"
 213           "(e.g. 'git pull') before pushing again.\n"
 214           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 215
 216static void advise_pull_before_push(void)
 217{
 218        if (!advice_push_non_ff_current || !advice_push_nonfastforward)
 219                return;
 220        advise(_(message_advice_pull_before_push));
 221}
 222
 223static void advise_checkout_pull_push(void)
 224{
 225        if (!advice_push_non_ff_matching || !advice_push_nonfastforward)
 226                return;
 227        advise(_(message_advice_checkout_pull_push));
 228}
 229
 230static int push_with_options(struct transport *transport, int flags)
 231{
 232        int err;
 233        int nonfastforward;
 234
 235        transport_set_verbosity(transport, verbosity, progress);
 236
 237        if (receivepack)
 238                transport_set_option(transport,
 239                                     TRANS_OPT_RECEIVEPACK, receivepack);
 240        if (thin)
 241                transport_set_option(transport, TRANS_OPT_THIN, "yes");
 242
 243        if (verbosity > 0)
 244                fprintf(stderr, _("Pushing to %s\n"), transport->url);
 245        err = transport_push(transport, refspec_nr, refspec, flags,
 246                             &nonfastforward);
 247        if (err != 0)
 248                error(_("failed to push some refs to '%s'"), transport->url);
 249
 250        err |= transport_disconnect(transport);
 251        if (!err)
 252                return 0;
 253
 254        switch (nonfastforward) {
 255        default:
 256                break;
 257        case NON_FF_HEAD:
 258                advise_pull_before_push();
 259                break;
 260        case NON_FF_OTHER:
 261                advise_checkout_pull_push();
 262                break;
 263        }
 264
 265        return 1;
 266}
 267
 268static int do_push(const char *repo, int flags)
 269{
 270        int i, errs;
 271        struct remote *remote = remote_get(repo);
 272        const char **url;
 273        int url_nr;
 274
 275        if (!remote) {
 276                if (repo)
 277                        die(_("bad repository '%s'"), repo);
 278                die(_("No configured push destination.\n"
 279                    "Either specify the URL from the command-line or configure a remote repository using\n"
 280                    "\n"
 281                    "    git remote add <name> <url>\n"
 282                    "\n"
 283                    "and then push using the remote name\n"
 284                    "\n"
 285                    "    git push <name>\n"));
 286        }
 287
 288        if (remote->mirror)
 289                flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
 290
 291        if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
 292                if (!strcmp(*refspec, "refs/tags/*"))
 293                        return error(_("--all and --tags are incompatible"));
 294                return error(_("--all can't be combined with refspecs"));
 295        }
 296
 297        if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
 298                if (!strcmp(*refspec, "refs/tags/*"))
 299                        return error(_("--mirror and --tags are incompatible"));
 300                return error(_("--mirror can't be combined with refspecs"));
 301        }
 302
 303        if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
 304                                (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
 305                return error(_("--all and --mirror are incompatible"));
 306        }
 307
 308        if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
 309                if (remote->push_refspec_nr) {
 310                        refspec = remote->push_refspec;
 311                        refspec_nr = remote->push_refspec_nr;
 312                } else if (!(flags & TRANSPORT_PUSH_MIRROR))
 313                        setup_default_push_refspecs(remote);
 314        }
 315        errs = 0;
 316        url_nr = push_url_of_remote(remote, &url);
 317        if (url_nr) {
 318                for (i = 0; i < url_nr; i++) {
 319                        struct transport *transport =
 320                                transport_get(remote, url[i]);
 321                        if (push_with_options(transport, flags))
 322                                errs++;
 323                }
 324        } else {
 325                struct transport *transport =
 326                        transport_get(remote, NULL);
 327
 328                if (push_with_options(transport, flags))
 329                        errs++;
 330        }
 331        return !!errs;
 332}
 333
 334static int option_parse_recurse_submodules(const struct option *opt,
 335                                   const char *arg, int unset)
 336{
 337        int *flags = opt->value;
 338
 339        if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
 340                      TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
 341                die("%s can only be used once.", opt->long_name);
 342
 343        if (arg) {
 344                if (!strcmp(arg, "check"))
 345                        *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
 346                else if (!strcmp(arg, "on-demand"))
 347                        *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
 348                else
 349                        die("bad %s argument: %s", opt->long_name, arg);
 350        } else
 351                die("option %s needs an argument (check|on-demand)",
 352                                opt->long_name);
 353
 354        return 0;
 355}
 356
 357int cmd_push(int argc, const char **argv, const char *prefix)
 358{
 359        int flags = 0;
 360        int tags = 0;
 361        int rc;
 362        const char *repo = NULL;        /* default repository */
 363        struct option options[] = {
 364                OPT__VERBOSITY(&verbosity),
 365                OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
 366                OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
 367                OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
 368                            (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
 369                OPT_BOOLEAN( 0, "delete", &deleterefs, N_("delete refs")),
 370                OPT_BOOLEAN( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
 371                OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
 372                OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
 373                OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
 374                { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
 375                        N_("control recursive pushing of submodules"),
 376                        PARSE_OPT_OPTARG, option_parse_recurse_submodules },
 377                OPT_BOOLEAN( 0 , "thin", &thin, N_("use thin pack")),
 378                OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
 379                OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
 380                OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
 381                        TRANSPORT_PUSH_SET_UPSTREAM),
 382                OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
 383                OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
 384                        TRANSPORT_PUSH_PRUNE),
 385                OPT_END()
 386        };
 387
 388        packet_trace_identity("push");
 389        git_config(git_default_config, NULL);
 390        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
 391
 392        if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
 393                die(_("--delete is incompatible with --all, --mirror and --tags"));
 394        if (deleterefs && argc < 2)
 395                die(_("--delete doesn't make sense without any refs"));
 396
 397        if (tags)
 398                add_refspec("refs/tags/*");
 399
 400        if (argc > 0) {
 401                repo = argv[0];
 402                set_refspecs(argv + 1, argc - 1);
 403        }
 404
 405        rc = do_push(repo, flags);
 406        if (rc == -1)
 407                usage_with_options(push_usage, options);
 408        else
 409                return rc;
 410}