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