647bcb9f3a44b0cd37f98110820c0f5cd42a2c7f
   1/*
   2 * Builtin "git pull"
   3 *
   4 * Based on git-pull.sh by Junio C Hamano
   5 *
   6 * Fetch one or more remote refs and merge it/them into the current HEAD.
   7 */
   8#include "cache.h"
   9#include "builtin.h"
  10#include "parse-options.h"
  11#include "exec_cmd.h"
  12#include "run-command.h"
  13#include "sha1-array.h"
  14#include "remote.h"
  15
  16static const char * const pull_usage[] = {
  17        N_("git pull [options] [<repository> [<refspec>...]]"),
  18        NULL
  19};
  20
  21/* Shared options */
  22static int opt_verbosity;
  23static char *opt_progress;
  24
  25/* Options passed to git-merge */
  26static char *opt_diffstat;
  27static char *opt_log;
  28static char *opt_squash;
  29static char *opt_commit;
  30static char *opt_edit;
  31static char *opt_ff;
  32static char *opt_verify_signatures;
  33static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
  34static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
  35static char *opt_gpg_sign;
  36
  37/* Options passed to git-fetch */
  38static char *opt_all;
  39static char *opt_append;
  40static char *opt_upload_pack;
  41static int opt_force;
  42static char *opt_tags;
  43static char *opt_prune;
  44static char *opt_recurse_submodules;
  45static int opt_dry_run;
  46static char *opt_keep;
  47static char *opt_depth;
  48static char *opt_unshallow;
  49static char *opt_update_shallow;
  50static char *opt_refmap;
  51
  52static struct option pull_options[] = {
  53        /* Shared options */
  54        OPT__VERBOSITY(&opt_verbosity),
  55        OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
  56                N_("force progress reporting"),
  57                PARSE_OPT_NOARG),
  58
  59        /* Options passed to git-merge */
  60        OPT_GROUP(N_("Options related to merging")),
  61        OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
  62                N_("do not show a diffstat at the end of the merge"),
  63                PARSE_OPT_NOARG | PARSE_OPT_NONEG),
  64        OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
  65                N_("show a diffstat at the end of the merge"),
  66                PARSE_OPT_NOARG),
  67        OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
  68                N_("(synonym to --stat)"),
  69                PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
  70        OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
  71                N_("add (at most <n>) entries from shortlog to merge commit message"),
  72                PARSE_OPT_OPTARG),
  73        OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
  74                N_("create a single commit instead of doing a merge"),
  75                PARSE_OPT_NOARG),
  76        OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
  77                N_("perform a commit if the merge succeeds (default)"),
  78                PARSE_OPT_NOARG),
  79        OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
  80                N_("edit message before committing"),
  81                PARSE_OPT_NOARG),
  82        OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
  83                N_("allow fast-forward"),
  84                PARSE_OPT_NOARG),
  85        OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
  86                N_("abort if fast-forward is not possible"),
  87                PARSE_OPT_NOARG | PARSE_OPT_NONEG),
  88        OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
  89                N_("verify that the named commit has a valid GPG signature"),
  90                PARSE_OPT_NOARG),
  91        OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
  92                N_("merge strategy to use"),
  93                0),
  94        OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
  95                N_("option=value"),
  96                N_("option for selected merge strategy"),
  97                0),
  98        OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
  99                N_("GPG sign commit"),
 100                PARSE_OPT_OPTARG),
 101
 102        /* Options passed to git-fetch */
 103        OPT_GROUP(N_("Options related to fetching")),
 104        OPT_PASSTHRU(0, "all", &opt_all, NULL,
 105                N_("fetch from all remotes"),
 106                PARSE_OPT_NOARG),
 107        OPT_PASSTHRU('a', "append", &opt_append, NULL,
 108                N_("append to .git/FETCH_HEAD instead of overwriting"),
 109                PARSE_OPT_NOARG),
 110        OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
 111                N_("path to upload pack on remote end"),
 112                0),
 113        OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
 114        OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
 115                N_("fetch all tags and associated objects"),
 116                PARSE_OPT_NOARG),
 117        OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
 118                N_("prune remote-tracking branches no longer on remote"),
 119                PARSE_OPT_NOARG),
 120        OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
 121                N_("on-demand"),
 122                N_("control recursive fetching of submodules"),
 123                PARSE_OPT_OPTARG),
 124        OPT_BOOL(0, "dry-run", &opt_dry_run,
 125                N_("dry run")),
 126        OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
 127                N_("keep downloaded pack"),
 128                PARSE_OPT_NOARG),
 129        OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
 130                N_("deepen history of shallow clone"),
 131                0),
 132        OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
 133                N_("convert to a complete repository"),
 134                PARSE_OPT_NONEG | PARSE_OPT_NOARG),
 135        OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
 136                N_("accept refs that update .git/shallow"),
 137                PARSE_OPT_NOARG),
 138        OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
 139                N_("specify fetch refmap"),
 140                PARSE_OPT_NONEG),
 141
 142        OPT_END()
 143};
 144
 145/**
 146 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
 147 */
 148static void argv_push_verbosity(struct argv_array *arr)
 149{
 150        int verbosity;
 151
 152        for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
 153                argv_array_push(arr, "-v");
 154
 155        for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
 156                argv_array_push(arr, "-q");
 157}
 158
 159/**
 160 * Pushes "-f" switches into arr to match the opt_force level.
 161 */
 162static void argv_push_force(struct argv_array *arr)
 163{
 164        int force = opt_force;
 165        while (force-- > 0)
 166                argv_array_push(arr, "-f");
 167}
 168
 169/**
 170 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
 171 * into merge_heads.
 172 */
 173static void get_merge_heads(struct sha1_array *merge_heads)
 174{
 175        const char *filename = git_path("FETCH_HEAD");
 176        FILE *fp;
 177        struct strbuf sb = STRBUF_INIT;
 178        unsigned char sha1[GIT_SHA1_RAWSZ];
 179
 180        if (!(fp = fopen(filename, "r")))
 181                die_errno(_("could not open '%s' for reading"), filename);
 182        while (strbuf_getline(&sb, fp, '\n') != EOF) {
 183                if (get_sha1_hex(sb.buf, sha1))
 184                        continue;  /* invalid line: does not start with SHA1 */
 185                if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
 186                        continue;  /* ref is not-for-merge */
 187                sha1_array_append(merge_heads, sha1);
 188        }
 189        fclose(fp);
 190        strbuf_release(&sb);
 191}
 192
 193/**
 194 * Used by die_no_merge_candidates() as a for_each_remote() callback to
 195 * retrieve the name of the remote if the repository only has one remote.
 196 */
 197static int get_only_remote(struct remote *remote, void *cb_data)
 198{
 199        const char **remote_name = cb_data;
 200
 201        if (*remote_name)
 202                return -1;
 203
 204        *remote_name = remote->name;
 205        return 0;
 206}
 207
 208/**
 209 * Dies with the appropriate reason for why there are no merge candidates:
 210 *
 211 * 1. We fetched from a specific remote, and a refspec was given, but it ended
 212 *    up not fetching anything. This is usually because the user provided a
 213 *    wildcard refspec which had no matches on the remote end.
 214 *
 215 * 2. We fetched from a non-default remote, but didn't specify a branch to
 216 *    merge. We can't use the configured one because it applies to the default
 217 *    remote, thus the user must specify the branches to merge.
 218 *
 219 * 3. We fetched from the branch's or repo's default remote, but:
 220 *
 221 *    a. We are not on a branch, so there will never be a configured branch to
 222 *       merge with.
 223 *
 224 *    b. We are on a branch, but there is no configured branch to merge with.
 225 *
 226 * 4. We fetched from the branch's or repo's default remote, but the configured
 227 *    branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
 228 *    part of the configured fetch refspec.)
 229 */
 230static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
 231{
 232        struct branch *curr_branch = branch_get("HEAD");
 233        const char *remote = curr_branch ? curr_branch->remote_name : NULL;
 234
 235        if (*refspecs) {
 236                fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
 237                fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
 238                                        "matches on the remote end."));
 239        } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
 240                fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
 241                        "a branch. Because this is not the default configured remote\n"
 242                        "for your current branch, you must specify a branch on the command line."),
 243                        repo);
 244        } else if (!curr_branch) {
 245                fprintf_ln(stderr, _("You are not currently on a branch."));
 246                fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
 247                fprintf_ln(stderr, _("See git-pull(1) for details."));
 248                fprintf(stderr, "\n");
 249                fprintf_ln(stderr, "    git pull <remote> <branch>");
 250                fprintf(stderr, "\n");
 251        } else if (!curr_branch->merge_nr) {
 252                const char *remote_name = NULL;
 253
 254                if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
 255                        remote_name = "<remote>";
 256
 257                fprintf_ln(stderr, _("There is no tracking information for the current branch."));
 258                fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
 259                fprintf_ln(stderr, _("See git-pull(1) for details."));
 260                fprintf(stderr, "\n");
 261                fprintf_ln(stderr, "    git pull <remote> <branch>");
 262                fprintf(stderr, "\n");
 263                fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
 264                                "\n"
 265                                "    git branch --set-upstream-to=%s/<branch> %s\n"),
 266                                remote_name, curr_branch->name);
 267        } else
 268                fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
 269                        "from the remote, but no such ref was fetched."),
 270                        *curr_branch->merge_name);
 271        exit(1);
 272}
 273
 274/**
 275 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
 276 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
 277 * is not provided in argv, it is set to NULL.
 278 */
 279static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
 280                const char ***refspecs)
 281{
 282        if (argc > 0) {
 283                *repo = *argv++;
 284                argc--;
 285        } else
 286                *repo = NULL;
 287        *refspecs = argv;
 288}
 289
 290/**
 291 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
 292 * repository and refspecs to fetch, or NULL if they are not provided.
 293 */
 294static int run_fetch(const char *repo, const char **refspecs)
 295{
 296        struct argv_array args = ARGV_ARRAY_INIT;
 297        int ret;
 298
 299        argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
 300
 301        /* Shared options */
 302        argv_push_verbosity(&args);
 303        if (opt_progress)
 304                argv_array_push(&args, opt_progress);
 305
 306        /* Options passed to git-fetch */
 307        if (opt_all)
 308                argv_array_push(&args, opt_all);
 309        if (opt_append)
 310                argv_array_push(&args, opt_append);
 311        if (opt_upload_pack)
 312                argv_array_push(&args, opt_upload_pack);
 313        argv_push_force(&args);
 314        if (opt_tags)
 315                argv_array_push(&args, opt_tags);
 316        if (opt_prune)
 317                argv_array_push(&args, opt_prune);
 318        if (opt_recurse_submodules)
 319                argv_array_push(&args, opt_recurse_submodules);
 320        if (opt_dry_run)
 321                argv_array_push(&args, "--dry-run");
 322        if (opt_keep)
 323                argv_array_push(&args, opt_keep);
 324        if (opt_depth)
 325                argv_array_push(&args, opt_depth);
 326        if (opt_unshallow)
 327                argv_array_push(&args, opt_unshallow);
 328        if (opt_update_shallow)
 329                argv_array_push(&args, opt_update_shallow);
 330        if (opt_refmap)
 331                argv_array_push(&args, opt_refmap);
 332
 333        if (repo) {
 334                argv_array_push(&args, repo);
 335                argv_array_pushv(&args, refspecs);
 336        } else if (*refspecs)
 337                die("BUG: refspecs without repo?");
 338        ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
 339        argv_array_clear(&args);
 340        return ret;
 341}
 342
 343/**
 344 * Runs git-merge, returning its exit status.
 345 */
 346static int run_merge(void)
 347{
 348        int ret;
 349        struct argv_array args = ARGV_ARRAY_INIT;
 350
 351        argv_array_pushl(&args, "merge", NULL);
 352
 353        /* Shared options */
 354        argv_push_verbosity(&args);
 355        if (opt_progress)
 356                argv_array_push(&args, opt_progress);
 357
 358        /* Options passed to git-merge */
 359        if (opt_diffstat)
 360                argv_array_push(&args, opt_diffstat);
 361        if (opt_log)
 362                argv_array_push(&args, opt_log);
 363        if (opt_squash)
 364                argv_array_push(&args, opt_squash);
 365        if (opt_commit)
 366                argv_array_push(&args, opt_commit);
 367        if (opt_edit)
 368                argv_array_push(&args, opt_edit);
 369        if (opt_ff)
 370                argv_array_push(&args, opt_ff);
 371        if (opt_verify_signatures)
 372                argv_array_push(&args, opt_verify_signatures);
 373        argv_array_pushv(&args, opt_strategies.argv);
 374        argv_array_pushv(&args, opt_strategy_opts.argv);
 375        if (opt_gpg_sign)
 376                argv_array_push(&args, opt_gpg_sign);
 377
 378        argv_array_push(&args, "FETCH_HEAD");
 379        ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
 380        argv_array_clear(&args);
 381        return ret;
 382}
 383
 384int cmd_pull(int argc, const char **argv, const char *prefix)
 385{
 386        const char *repo, **refspecs;
 387        struct sha1_array merge_heads = SHA1_ARRAY_INIT;
 388
 389        if (!getenv("_GIT_USE_BUILTIN_PULL")) {
 390                const char *path = mkpath("%s/git-pull", git_exec_path());
 391
 392                if (sane_execvp(path, (char **)argv) < 0)
 393                        die_errno("could not exec %s", path);
 394        }
 395
 396        argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
 397
 398        parse_repo_refspecs(argc, argv, &repo, &refspecs);
 399
 400        if (run_fetch(repo, refspecs))
 401                return 1;
 402
 403        if (opt_dry_run)
 404                return 0;
 405
 406        get_merge_heads(&merge_heads);
 407
 408        if (!merge_heads.nr)
 409                die_no_merge_candidates(repo, refspecs);
 410
 411        return run_merge();
 412}