builtin / push.con commit t5533: test "push --force-with-lease" (d887cc1)
   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 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 explicitely 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, int simple)
 126{
 127        struct strbuf refspec = STRBUF_INIT;
 128        struct branch *branch = branch_get(NULL);
 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 (strcmp(branch->remote_name, remote->name))
 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        if (simple && strcmp(branch->refname, branch->merge[0]->src))
 148                die_push_simple(branch, remote);
 149
 150        strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
 151        add_refspec(refspec.buf);
 152}
 153
 154static char warn_unspecified_push_default_msg[] =
 155N_("push.default is unset; its implicit value is changing in\n"
 156   "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
 157   "and maintain the current behavior after the default changes, use:\n"
 158   "\n"
 159   "  git config --global push.default matching\n"
 160   "\n"
 161   "To squelch this message and adopt the new behavior now, use:\n"
 162   "\n"
 163   "  git config --global push.default simple\n"
 164   "\n"
 165   "See 'git help config' and search for 'push.default' for further information.\n"
 166   "(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode\n"
 167   "'current' instead of 'simple' if you sometimes use older versions of Git)");
 168
 169static void warn_unspecified_push_default_configuration(void)
 170{
 171        static int warn_once;
 172
 173        if (warn_once++)
 174                return;
 175        warning("%s\n", _(warn_unspecified_push_default_msg));
 176}
 177
 178static void setup_default_push_refspecs(struct remote *remote)
 179{
 180        struct branch *branch;
 181
 182        switch (push_default) {
 183        default:
 184        case PUSH_DEFAULT_UNSPECIFIED:
 185                default_matching_used = 1;
 186                warn_unspecified_push_default_configuration();
 187                /* fallthru */
 188        case PUSH_DEFAULT_MATCHING:
 189                add_refspec(":");
 190                break;
 191
 192        case PUSH_DEFAULT_SIMPLE:
 193                setup_push_upstream(remote, 1);
 194                break;
 195
 196        case PUSH_DEFAULT_UPSTREAM:
 197                setup_push_upstream(remote, 0);
 198                break;
 199
 200        case PUSH_DEFAULT_CURRENT:
 201                branch = branch_get(NULL);
 202                if (!branch)
 203                        die(_(message_detached_head_die), remote->name);
 204                add_refspec(branch->name);
 205                break;
 206
 207        case PUSH_DEFAULT_NOTHING:
 208                die(_("You didn't specify any refspecs to push, and "
 209                    "push.default is \"nothing\"."));
 210                break;
 211        }
 212}
 213
 214static const char message_advice_pull_before_push[] =
 215        N_("Updates were rejected because the tip of your current branch is behind\n"
 216           "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
 217           "before pushing again.\n"
 218           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 219
 220static const char message_advice_use_upstream[] =
 221        N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 222           "counterpart. If you did not intend to push that branch, you may want to\n"
 223           "specify branches to push or set the 'push.default' configuration variable\n"
 224           "to 'simple', 'current' or 'upstream' to push only the current branch.");
 225
 226static const char message_advice_checkout_pull_push[] =
 227        N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 228           "counterpart. Check out this branch and merge the remote changes\n"
 229           "(e.g. 'git pull') before pushing again.\n"
 230           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 231
 232static const char message_advice_ref_fetch_first[] =
 233        N_("Updates were rejected because the remote contains work that you do\n"
 234           "not have locally. This is usually caused by another repository pushing\n"
 235           "to the same ref. You may want to first merge the remote changes (e.g.,\n"
 236           "'git pull') before pushing again.\n"
 237           "See the 'Note about fast-forwards' in 'git push --help' for details.");
 238
 239static const char message_advice_ref_already_exists[] =
 240        N_("Updates were rejected because the tag already exists in the remote.");
 241
 242static const char message_advice_ref_needs_force[] =
 243        N_("You cannot update a remote ref that points at a non-commit object,\n"
 244           "or update a remote ref to make it point at a non-commit object,\n"
 245           "without using the '--force' option.\n");
 246
 247static void advise_pull_before_push(void)
 248{
 249        if (!advice_push_non_ff_current || !advice_push_update_rejected)
 250                return;
 251        advise(_(message_advice_pull_before_push));
 252}
 253
 254static void advise_use_upstream(void)
 255{
 256        if (!advice_push_non_ff_default || !advice_push_update_rejected)
 257                return;
 258        advise(_(message_advice_use_upstream));
 259}
 260
 261static void advise_checkout_pull_push(void)
 262{
 263        if (!advice_push_non_ff_matching || !advice_push_update_rejected)
 264                return;
 265        advise(_(message_advice_checkout_pull_push));
 266}
 267
 268static void advise_ref_already_exists(void)
 269{
 270        if (!advice_push_already_exists || !advice_push_update_rejected)
 271                return;
 272        advise(_(message_advice_ref_already_exists));
 273}
 274
 275static void advise_ref_fetch_first(void)
 276{
 277        if (!advice_push_fetch_first || !advice_push_update_rejected)
 278                return;
 279        advise(_(message_advice_ref_fetch_first));
 280}
 281
 282static void advise_ref_needs_force(void)
 283{
 284        if (!advice_push_needs_force || !advice_push_update_rejected)
 285                return;
 286        advise(_(message_advice_ref_needs_force));
 287}
 288
 289static int push_with_options(struct transport *transport, int flags)
 290{
 291        int err;
 292        unsigned int reject_reasons;
 293
 294        transport_set_verbosity(transport, verbosity, progress);
 295
 296        if (receivepack)
 297                transport_set_option(transport,
 298                                     TRANS_OPT_RECEIVEPACK, receivepack);
 299        if (thin)
 300                transport_set_option(transport, TRANS_OPT_THIN, "yes");
 301
 302        if (!is_empty_cas(&cas)) {
 303                if (!transport->smart_options)
 304                        die("underlying transport does not support --%s option",
 305                            CAS_OPT_NAME);
 306                transport->smart_options->cas = &cas;
 307        }
 308
 309        if (verbosity > 0)
 310                fprintf(stderr, _("Pushing to %s\n"), transport->url);
 311        err = transport_push(transport, refspec_nr, refspec, flags,
 312                             &reject_reasons);
 313        if (err != 0)
 314                error(_("failed to push some refs to '%s'"), transport->url);
 315
 316        err |= transport_disconnect(transport);
 317        if (!err)
 318                return 0;
 319
 320        if (reject_reasons & REJECT_NON_FF_HEAD) {
 321                advise_pull_before_push();
 322        } else if (reject_reasons & REJECT_NON_FF_OTHER) {
 323                if (default_matching_used)
 324                        advise_use_upstream();
 325                else
 326                        advise_checkout_pull_push();
 327        } else if (reject_reasons & REJECT_ALREADY_EXISTS) {
 328                advise_ref_already_exists();
 329        } else if (reject_reasons & REJECT_FETCH_FIRST) {
 330                advise_ref_fetch_first();
 331        } else if (reject_reasons & REJECT_NEEDS_FORCE) {
 332                advise_ref_needs_force();
 333        }
 334
 335        return 1;
 336}
 337
 338static int do_push(const char *repo, int flags)
 339{
 340        int i, errs;
 341        struct remote *remote = pushremote_get(repo);
 342        const char **url;
 343        int url_nr;
 344
 345        if (!remote) {
 346                if (repo)
 347                        die(_("bad repository '%s'"), repo);
 348                die(_("No configured push destination.\n"
 349                    "Either specify the URL from the command-line or configure a remote repository using\n"
 350                    "\n"
 351                    "    git remote add <name> <url>\n"
 352                    "\n"
 353                    "and then push using the remote name\n"
 354                    "\n"
 355                    "    git push <name>\n"));
 356        }
 357
 358        if (remote->mirror)
 359                flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
 360
 361        if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
 362                if (!strcmp(*refspec, "refs/tags/*"))
 363                        return error(_("--all and --tags are incompatible"));
 364                return error(_("--all can't be combined with refspecs"));
 365        }
 366
 367        if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
 368                if (!strcmp(*refspec, "refs/tags/*"))
 369                        return error(_("--mirror and --tags are incompatible"));
 370                return error(_("--mirror can't be combined with refspecs"));
 371        }
 372
 373        if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
 374                                (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
 375                return error(_("--all and --mirror are incompatible"));
 376        }
 377
 378        if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
 379                if (remote->push_refspec_nr) {
 380                        refspec = remote->push_refspec;
 381                        refspec_nr = remote->push_refspec_nr;
 382                } else if (!(flags & TRANSPORT_PUSH_MIRROR))
 383                        setup_default_push_refspecs(remote);
 384        }
 385        errs = 0;
 386        url_nr = push_url_of_remote(remote, &url);
 387        if (url_nr) {
 388                for (i = 0; i < url_nr; i++) {
 389                        struct transport *transport =
 390                                transport_get(remote, url[i]);
 391                        if (push_with_options(transport, flags))
 392                                errs++;
 393                }
 394        } else {
 395                struct transport *transport =
 396                        transport_get(remote, NULL);
 397
 398                if (push_with_options(transport, flags))
 399                        errs++;
 400        }
 401        return !!errs;
 402}
 403
 404static int option_parse_recurse_submodules(const struct option *opt,
 405                                   const char *arg, int unset)
 406{
 407        int *flags = opt->value;
 408
 409        if (*flags & (TRANSPORT_RECURSE_SUBMODULES_CHECK |
 410                      TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND))
 411                die("%s can only be used once.", opt->long_name);
 412
 413        if (arg) {
 414                if (!strcmp(arg, "check"))
 415                        *flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
 416                else if (!strcmp(arg, "on-demand"))
 417                        *flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
 418                else
 419                        die("bad %s argument: %s", opt->long_name, arg);
 420        } else
 421                die("option %s needs an argument (check|on-demand)",
 422                                opt->long_name);
 423
 424        return 0;
 425}
 426
 427int cmd_push(int argc, const char **argv, const char *prefix)
 428{
 429        int flags = 0;
 430        int tags = 0;
 431        int rc;
 432        const char *repo = NULL;        /* default repository */
 433        struct option options[] = {
 434                OPT__VERBOSITY(&verbosity),
 435                OPT_STRING( 0 , "repo", &repo, N_("repository"), N_("repository")),
 436                OPT_BIT( 0 , "all", &flags, N_("push all refs"), TRANSPORT_PUSH_ALL),
 437                OPT_BIT( 0 , "mirror", &flags, N_("mirror all refs"),
 438                            (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
 439                OPT_BOOL( 0, "delete", &deleterefs, N_("delete refs")),
 440                OPT_BOOL( 0 , "tags", &tags, N_("push tags (can't be used with --all or --mirror)")),
 441                OPT_BIT('n' , "dry-run", &flags, N_("dry run"), TRANSPORT_PUSH_DRY_RUN),
 442                OPT_BIT( 0,  "porcelain", &flags, N_("machine-readable output"), TRANSPORT_PUSH_PORCELAIN),
 443                OPT_BIT('f', "force", &flags, N_("force updates"), TRANSPORT_PUSH_FORCE),
 444                { OPTION_CALLBACK,
 445                  0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
 446                  N_("require old value of ref to be at this value"),
 447                  PARSE_OPT_OPTARG, parseopt_push_cas_option },
 448                { OPTION_CALLBACK, 0, "recurse-submodules", &flags, N_("check"),
 449                        N_("control recursive pushing of submodules"),
 450                        PARSE_OPT_OPTARG, option_parse_recurse_submodules },
 451                OPT_BOOL( 0 , "thin", &thin, N_("use thin pack")),
 452                OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
 453                OPT_STRING( 0 , "exec", &receivepack, "receive-pack", N_("receive pack program")),
 454                OPT_BIT('u', "set-upstream", &flags, N_("set upstream for git pull/status"),
 455                        TRANSPORT_PUSH_SET_UPSTREAM),
 456                OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
 457                OPT_BIT(0, "prune", &flags, N_("prune locally removed refs"),
 458                        TRANSPORT_PUSH_PRUNE),
 459                OPT_BIT(0, "no-verify", &flags, N_("bypass pre-push hook"), TRANSPORT_PUSH_NO_HOOK),
 460                OPT_BIT(0, "follow-tags", &flags, N_("push missing but relevant tags"),
 461                        TRANSPORT_PUSH_FOLLOW_TAGS),
 462                OPT_END()
 463        };
 464
 465        packet_trace_identity("push");
 466        git_config(git_default_config, NULL);
 467        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
 468
 469        if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
 470                die(_("--delete is incompatible with --all, --mirror and --tags"));
 471        if (deleterefs && argc < 2)
 472                die(_("--delete doesn't make sense without any refs"));
 473
 474        if (tags)
 475                add_refspec("refs/tags/*");
 476
 477        if (argc > 0) {
 478                repo = argv[0];
 479                set_refspecs(argv + 1, argc - 1);
 480        }
 481
 482        rc = do_push(repo, flags);
 483        if (rc == -1)
 484                usage_with_options(push_usage, options);
 485        else
 486                return rc;
 487}