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