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