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#include "dir.h" 16#include "refs.h" 17 18static const char * const pull_usage[] = { 19 N_("git pull [options] [<repository> [<refspec>...]]"), 20 NULL 21}; 22 23/* Shared options */ 24static int opt_verbosity; 25static char *opt_progress; 26 27/* Options passed to git-merge */ 28static char *opt_diffstat; 29static char *opt_log; 30static char *opt_squash; 31static char *opt_commit; 32static char *opt_edit; 33static char *opt_ff; 34static char *opt_verify_signatures; 35static struct argv_array opt_strategies = ARGV_ARRAY_INIT; 36static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT; 37static char *opt_gpg_sign; 38 39/* Options passed to git-fetch */ 40static char *opt_all; 41static char *opt_append; 42static char *opt_upload_pack; 43static int opt_force; 44static char *opt_tags; 45static char *opt_prune; 46static char *opt_recurse_submodules; 47static int opt_dry_run; 48static char *opt_keep; 49static char *opt_depth; 50static char *opt_unshallow; 51static char *opt_update_shallow; 52static char *opt_refmap; 53 54static struct option pull_options[] = { 55 /* Shared options */ 56 OPT__VERBOSITY(&opt_verbosity), 57 OPT_PASSTHRU(0, "progress", &opt_progress, NULL, 58 N_("force progress reporting"), 59 PARSE_OPT_NOARG), 60 61 /* Options passed to git-merge */ 62 OPT_GROUP(N_("Options related to merging")), 63 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, 64 N_("do not show a diffstat at the end of the merge"), 65 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 66 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL, 67 N_("show a diffstat at the end of the merge"), 68 PARSE_OPT_NOARG), 69 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL, 70 N_("(synonym to --stat)"), 71 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), 72 OPT_PASSTHRU(0, "log", &opt_log, N_("n"), 73 N_("add (at most <n>) entries from shortlog to merge commit message"), 74 PARSE_OPT_OPTARG), 75 OPT_PASSTHRU(0, "squash", &opt_squash, NULL, 76 N_("create a single commit instead of doing a merge"), 77 PARSE_OPT_NOARG), 78 OPT_PASSTHRU(0, "commit", &opt_commit, NULL, 79 N_("perform a commit if the merge succeeds (default)"), 80 PARSE_OPT_NOARG), 81 OPT_PASSTHRU(0, "edit", &opt_edit, NULL, 82 N_("edit message before committing"), 83 PARSE_OPT_NOARG), 84 OPT_PASSTHRU(0, "ff", &opt_ff, NULL, 85 N_("allow fast-forward"), 86 PARSE_OPT_NOARG), 87 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL, 88 N_("abort if fast-forward is not possible"), 89 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 90 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL, 91 N_("verify that the named commit has a valid GPG signature"), 92 PARSE_OPT_NOARG), 93 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"), 94 N_("merge strategy to use"), 95 0), 96 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts, 97 N_("option=value"), 98 N_("option for selected merge strategy"), 99 0), 100 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"), 101 N_("GPG sign commit"), 102 PARSE_OPT_OPTARG), 103 104 /* Options passed to git-fetch */ 105 OPT_GROUP(N_("Options related to fetching")), 106 OPT_PASSTHRU(0, "all", &opt_all, NULL, 107 N_("fetch from all remotes"), 108 PARSE_OPT_NOARG), 109 OPT_PASSTHRU('a', "append", &opt_append, NULL, 110 N_("append to .git/FETCH_HEAD instead of overwriting"), 111 PARSE_OPT_NOARG), 112 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"), 113 N_("path to upload pack on remote end"), 114 0), 115 OPT__FORCE(&opt_force, N_("force overwrite of local branch")), 116 OPT_PASSTHRU('t', "tags", &opt_tags, NULL, 117 N_("fetch all tags and associated objects"), 118 PARSE_OPT_NOARG), 119 OPT_PASSTHRU('p', "prune", &opt_prune, NULL, 120 N_("prune remote-tracking branches no longer on remote"), 121 PARSE_OPT_NOARG), 122 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules, 123 N_("on-demand"), 124 N_("control recursive fetching of submodules"), 125 PARSE_OPT_OPTARG), 126 OPT_BOOL(0, "dry-run", &opt_dry_run, 127 N_("dry run")), 128 OPT_PASSTHRU('k', "keep", &opt_keep, NULL, 129 N_("keep downloaded pack"), 130 PARSE_OPT_NOARG), 131 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"), 132 N_("deepen history of shallow clone"), 133 0), 134 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL, 135 N_("convert to a complete repository"), 136 PARSE_OPT_NONEG | PARSE_OPT_NOARG), 137 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL, 138 N_("accept refs that update .git/shallow"), 139 PARSE_OPT_NOARG), 140 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"), 141 N_("specify fetch refmap"), 142 PARSE_OPT_NONEG), 143 144 OPT_END() 145}; 146 147/** 148 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. 149 */ 150static void argv_push_verbosity(struct argv_array *arr) 151{ 152 int verbosity; 153 154 for (verbosity = opt_verbosity; verbosity > 0; verbosity--) 155 argv_array_push(arr, "-v"); 156 157 for (verbosity = opt_verbosity; verbosity < 0; verbosity++) 158 argv_array_push(arr, "-q"); 159} 160 161/** 162 * Pushes "-f" switches into arr to match the opt_force level. 163 */ 164static void argv_push_force(struct argv_array *arr) 165{ 166 int force = opt_force; 167 while (force-- > 0) 168 argv_array_push(arr, "-f"); 169} 170 171/** 172 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv 173 */ 174static void set_reflog_message(int argc, const char **argv) 175{ 176 int i; 177 struct strbuf msg = STRBUF_INIT; 178 179 for (i = 0; i < argc; i++) { 180 if (i) 181 strbuf_addch(&msg, ' '); 182 strbuf_addstr(&msg, argv[i]); 183 } 184 185 setenv("GIT_REFLOG_ACTION", msg.buf, 0); 186 187 strbuf_release(&msg); 188} 189 190/** 191 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If 192 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns 193 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an 194 * error. 195 */ 196static const char *config_get_ff(void) 197{ 198 const char *value; 199 200 if (git_config_get_value("pull.ff", &value)) 201 return NULL; 202 203 switch (git_config_maybe_bool("pull.ff", value)) { 204 case 0: 205 return "--no-ff"; 206 case 1: 207 return "--ff"; 208 } 209 210 if (!strcmp(value, "only")) 211 return "--ff-only"; 212 213 die(_("Invalid value for pull.ff: %s"), value); 214} 215 216/** 217 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge 218 * into merge_heads. 219 */ 220static void get_merge_heads(struct sha1_array *merge_heads) 221{ 222 const char *filename = git_path("FETCH_HEAD"); 223 FILE *fp; 224 struct strbuf sb = STRBUF_INIT; 225 unsigned char sha1[GIT_SHA1_RAWSZ]; 226 227 if (!(fp = fopen(filename, "r"))) 228 die_errno(_("could not open '%s' for reading"), filename); 229 while (strbuf_getline(&sb, fp, '\n') != EOF) { 230 if (get_sha1_hex(sb.buf, sha1)) 231 continue; /* invalid line: does not start with SHA1 */ 232 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t")) 233 continue; /* ref is not-for-merge */ 234 sha1_array_append(merge_heads, sha1); 235 } 236 fclose(fp); 237 strbuf_release(&sb); 238} 239 240/** 241 * Used by die_no_merge_candidates() as a for_each_remote() callback to 242 * retrieve the name of the remote if the repository only has one remote. 243 */ 244static int get_only_remote(struct remote *remote, void *cb_data) 245{ 246 const char **remote_name = cb_data; 247 248 if (*remote_name) 249 return -1; 250 251 *remote_name = remote->name; 252 return 0; 253} 254 255/** 256 * Dies with the appropriate reason for why there are no merge candidates: 257 * 258 * 1. We fetched from a specific remote, and a refspec was given, but it ended 259 * up not fetching anything. This is usually because the user provided a 260 * wildcard refspec which had no matches on the remote end. 261 * 262 * 2. We fetched from a non-default remote, but didn't specify a branch to 263 * merge. We can't use the configured one because it applies to the default 264 * remote, thus the user must specify the branches to merge. 265 * 266 * 3. We fetched from the branch's or repo's default remote, but: 267 * 268 * a. We are not on a branch, so there will never be a configured branch to 269 * merge with. 270 * 271 * b. We are on a branch, but there is no configured branch to merge with. 272 * 273 * 4. We fetched from the branch's or repo's default remote, but the configured 274 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't 275 * part of the configured fetch refspec.) 276 */ 277static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs) 278{ 279 struct branch *curr_branch = branch_get("HEAD"); 280 const char *remote = curr_branch ? curr_branch->remote_name : NULL; 281 282 if (*refspecs) { 283 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched.")); 284 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n" 285 "matches on the remote end.")); 286 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) { 287 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n" 288 "a branch. Because this is not the default configured remote\n" 289 "for your current branch, you must specify a branch on the command line."), 290 repo); 291 } else if (!curr_branch) { 292 fprintf_ln(stderr, _("You are not currently on a branch.")); 293 fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); 294 fprintf_ln(stderr, _("See git-pull(1) for details.")); 295 fprintf(stderr, "\n"); 296 fprintf_ln(stderr, " git pull <remote> <branch>"); 297 fprintf(stderr, "\n"); 298 } else if (!curr_branch->merge_nr) { 299 const char *remote_name = NULL; 300 301 if (for_each_remote(get_only_remote, &remote_name) || !remote_name) 302 remote_name = "<remote>"; 303 304 fprintf_ln(stderr, _("There is no tracking information for the current branch.")); 305 fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); 306 fprintf_ln(stderr, _("See git-pull(1) for details.")); 307 fprintf(stderr, "\n"); 308 fprintf_ln(stderr, " git pull <remote> <branch>"); 309 fprintf(stderr, "\n"); 310 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n" 311 "\n" 312 " git branch --set-upstream-to=%s/<branch> %s\n"), 313 remote_name, curr_branch->name); 314 } else 315 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n" 316 "from the remote, but no such ref was fetched."), 317 *curr_branch->merge_name); 318 exit(1); 319} 320 321/** 322 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo` 323 * as a string and `refspecs` as a null-terminated array of strings. If `repo` 324 * is not provided in argv, it is set to NULL. 325 */ 326static void parse_repo_refspecs(int argc, const char **argv, const char **repo, 327 const char ***refspecs) 328{ 329 if (argc > 0) { 330 *repo = *argv++; 331 argc--; 332 } else 333 *repo = NULL; 334 *refspecs = argv; 335} 336 337/** 338 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the 339 * repository and refspecs to fetch, or NULL if they are not provided. 340 */ 341static int run_fetch(const char *repo, const char **refspecs) 342{ 343 struct argv_array args = ARGV_ARRAY_INIT; 344 int ret; 345 346 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); 347 348 /* Shared options */ 349 argv_push_verbosity(&args); 350 if (opt_progress) 351 argv_array_push(&args, opt_progress); 352 353 /* Options passed to git-fetch */ 354 if (opt_all) 355 argv_array_push(&args, opt_all); 356 if (opt_append) 357 argv_array_push(&args, opt_append); 358 if (opt_upload_pack) 359 argv_array_push(&args, opt_upload_pack); 360 argv_push_force(&args); 361 if (opt_tags) 362 argv_array_push(&args, opt_tags); 363 if (opt_prune) 364 argv_array_push(&args, opt_prune); 365 if (opt_recurse_submodules) 366 argv_array_push(&args, opt_recurse_submodules); 367 if (opt_dry_run) 368 argv_array_push(&args, "--dry-run"); 369 if (opt_keep) 370 argv_array_push(&args, opt_keep); 371 if (opt_depth) 372 argv_array_push(&args, opt_depth); 373 if (opt_unshallow) 374 argv_array_push(&args, opt_unshallow); 375 if (opt_update_shallow) 376 argv_array_push(&args, opt_update_shallow); 377 if (opt_refmap) 378 argv_array_push(&args, opt_refmap); 379 380 if (repo) { 381 argv_array_push(&args, repo); 382 argv_array_pushv(&args, refspecs); 383 } else if (*refspecs) 384 die("BUG: refspecs without repo?"); 385 ret = run_command_v_opt(args.argv, RUN_GIT_CMD); 386 argv_array_clear(&args); 387 return ret; 388} 389 390/** 391 * "Pulls into void" by branching off merge_head. 392 */ 393static int pull_into_void(const unsigned char *merge_head, 394 const unsigned char *curr_head) 395{ 396 /* 397 * Two-way merge: we treat the index as based on an empty tree, 398 * and try to fast-forward to HEAD. This ensures we will not lose 399 * index/worktree changes that the user already made on the unborn 400 * branch. 401 */ 402 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0)) 403 return 1; 404 405 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) 406 return 1; 407 408 return 0; 409} 410 411/** 412 * Runs git-merge, returning its exit status. 413 */ 414static int run_merge(void) 415{ 416 int ret; 417 struct argv_array args = ARGV_ARRAY_INIT; 418 419 argv_array_pushl(&args, "merge", NULL); 420 421 /* Shared options */ 422 argv_push_verbosity(&args); 423 if (opt_progress) 424 argv_array_push(&args, opt_progress); 425 426 /* Options passed to git-merge */ 427 if (opt_diffstat) 428 argv_array_push(&args, opt_diffstat); 429 if (opt_log) 430 argv_array_push(&args, opt_log); 431 if (opt_squash) 432 argv_array_push(&args, opt_squash); 433 if (opt_commit) 434 argv_array_push(&args, opt_commit); 435 if (opt_edit) 436 argv_array_push(&args, opt_edit); 437 if (opt_ff) 438 argv_array_push(&args, opt_ff); 439 if (opt_verify_signatures) 440 argv_array_push(&args, opt_verify_signatures); 441 argv_array_pushv(&args, opt_strategies.argv); 442 argv_array_pushv(&args, opt_strategy_opts.argv); 443 if (opt_gpg_sign) 444 argv_array_push(&args, opt_gpg_sign); 445 446 argv_array_push(&args, "FETCH_HEAD"); 447 ret = run_command_v_opt(args.argv, RUN_GIT_CMD); 448 argv_array_clear(&args); 449 return ret; 450} 451 452int cmd_pull(int argc, const char **argv, const char *prefix) 453{ 454 const char *repo, **refspecs; 455 struct sha1_array merge_heads = SHA1_ARRAY_INIT; 456 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ]; 457 458 if (!getenv("_GIT_USE_BUILTIN_PULL")) { 459 const char *path = mkpath("%s/git-pull", git_exec_path()); 460 461 if (sane_execvp(path, (char **)argv) < 0) 462 die_errno("could not exec %s", path); 463 } 464 465 if (!getenv("GIT_REFLOG_ACTION")) 466 set_reflog_message(argc, argv); 467 468 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); 469 470 parse_repo_refspecs(argc, argv, &repo, &refspecs); 471 472 if (!opt_ff) 473 opt_ff = xstrdup_or_null(config_get_ff()); 474 475 git_config(git_default_config, NULL); 476 477 if (read_cache_unmerged()) 478 die_resolve_conflict("Pull"); 479 480 if (file_exists(git_path("MERGE_HEAD"))) 481 die_conclude_merge(); 482 483 if (get_sha1("HEAD", orig_head)) 484 hashclr(orig_head); 485 486 if (run_fetch(repo, refspecs)) 487 return 1; 488 489 if (opt_dry_run) 490 return 0; 491 492 if (get_sha1("HEAD", curr_head)) 493 hashclr(curr_head); 494 495 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) && 496 hashcmp(orig_head, curr_head)) { 497 /* 498 * The fetch involved updating the current branch. 499 * 500 * The working tree and the index file are still based on 501 * orig_head commit, but we are merging into curr_head. 502 * Update the working tree to match curr_head. 503 */ 504 505 warning(_("fetch updated the current branch head.\n" 506 "fast-forwarding your working tree from\n" 507 "commit %s."), sha1_to_hex(orig_head)); 508 509 if (checkout_fast_forward(orig_head, curr_head, 0)) 510 die(_("Cannot fast-forward your working tree.\n" 511 "After making sure that you saved anything precious from\n" 512 "$ git diff %s\n" 513 "output, run\n" 514 "$ git reset --hard\n" 515 "to recover."), sha1_to_hex(orig_head)); 516 } 517 518 get_merge_heads(&merge_heads); 519 520 if (!merge_heads.nr) 521 die_no_merge_candidates(repo, refspecs); 522 523 if (is_null_sha1(orig_head)) { 524 if (merge_heads.nr > 1) 525 die(_("Cannot merge multiple branches into empty head.")); 526 return pull_into_void(*merge_heads.sha1, curr_head); 527 } else 528 return run_merge(); 529}