Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/pull.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / pull.c` 1197 lines ยท 36166 bytes Raw Blame History 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 9 #define USE_THE_REPOSITORY_VARIABLE 10 11 #include "builtin.h" 12 #include "advice.h" 13 #include "config.h" 14 #include "environment.h" 15 #include "gettext.h" 16 #include "hex.h" 17 #include "merge.h" 18 #include "object-name.h" 19 #include "parse-options.h" 20 #include "run-command.h" 21 #include "oid-array.h" 22 #include "remote.h" 23 #include "dir.h" 24 #include "path.h" 25 #include "read-cache-ll.h" 26 #include "rebase.h" 27 #include "refs.h" 28 #include "refspec.h" 29 #include "submodule.h" 30 #include "submodule-config.h" 31 #include "wt-status.h" 32 #include "commit-reach.h" 33 #include "sequencer.h" 34 35 /** 36 * Parses the value of --rebase. If value is a false value, returns 37 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is 38 * "merges", returns REBASE_MERGES. If value is a invalid value, dies with 39 * a fatal error if fatal is true, otherwise returns REBASE_INVALID. 40 */ 41 static enum rebase_type parse_config_rebase(const char *key, const char *value, 42 int fatal) 43 { 44 enum rebase_type v = rebase_parse_value(value); 45 if (v != REBASE_INVALID) 46 return v; 47 48 if (fatal) 49 die(_("invalid value for '%s': '%s'"), key, value); 50 else 51 error(_("invalid value for '%s': '%s'"), key, value); 52 53 return REBASE_INVALID; 54 } 55 56 /** 57 * Callback for --rebase, which parses arg with parse_config_rebase(). 58 */ 59 static int parse_opt_rebase(const struct option *opt, const char *arg, int unset) 60 { 61 enum rebase_type *value = opt->value; 62 63 if (arg) 64 *value = parse_config_rebase("--rebase", arg, 0); 65 else 66 *value = unset ? REBASE_FALSE : REBASE_TRUE; 67 return *value == REBASE_INVALID ? -1 : 0; 68 } 69 70 static const char * const pull_usage[] = { 71 N_("git pull [] [ [...]]"), 72 NULL 73 }; 74 75 /* Shared options */ 76 static int opt_verbosity; 77 static const char *opt_progress; 78 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT; 79 static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT; 80 81 /* Options passed to git-merge or git-rebase */ 82 static enum rebase_type opt_rebase = -1; 83 static const char *opt_diffstat; 84 static const char *opt_log; 85 static const char *opt_signoff; 86 static const char *opt_squash; 87 static const char *opt_commit; 88 static const char *opt_edit; 89 static const char *cleanup_arg; 90 static char *opt_ff; 91 static const char *opt_verify_signatures; 92 static const char *opt_verify; 93 static int opt_autostash = -1; 94 static int config_rebase_autostash; 95 static int config_pull_autostash = -1; 96 static int check_trust_level = 1; 97 static struct strvec opt_strategies = STRVEC_INIT; 98 static struct strvec opt_strategy_opts = STRVEC_INIT; 99 static const char *opt_gpg_sign; 100 static int opt_allow_unrelated_histories; 101 102 /* Options passed to git-fetch */ 103 static const char *opt_all; 104 static const char *opt_append; 105 static const char *opt_upload_pack; 106 static int opt_force; 107 static const char *opt_tags; 108 static const char *opt_prune; 109 static const char *max_children; 110 static int opt_dry_run; 111 static const char *opt_keep; 112 static const char *opt_depth; 113 static const char *opt_unshallow; 114 static const char *opt_update_shallow; 115 static const char *opt_refmap; 116 static const char *opt_ipv4; 117 static const char *opt_ipv6; 118 static int opt_show_forced_updates = -1; 119 static const char *set_upstream; 120 static struct strvec opt_fetch = STRVEC_INIT; 121 122 /** 123 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. 124 */ 125 static void argv_push_verbosity(struct strvec *arr) 126 { 127 int verbosity; 128 129 for (verbosity = opt_verbosity; verbosity > 0; verbosity--) 130 strvec_push(arr, "-v"); 131 132 for (verbosity = opt_verbosity; verbosity < 0; verbosity++) 133 strvec_push(arr, "-q"); 134 } 135 136 /** 137 * Pushes "-f" switches into arr to match the opt_force level. 138 */ 139 static void argv_push_force(struct strvec *arr) 140 { 141 int force = opt_force; 142 while (force-- > 0) 143 strvec_push(arr, "-f"); 144 } 145 146 /** 147 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv 148 */ 149 static void set_reflog_message(int argc, const char **argv) 150 { 151 int i; 152 struct strbuf msg = STRBUF_INIT; 153 154 for (i = 0; i < argc; i++) { 155 if (i) 156 strbuf_addch(&msg, ' '); 157 strbuf_addstr(&msg, argv[i]); 158 } 159 160 setenv("GIT_REFLOG_ACTION", msg.buf, 0); 161 162 strbuf_release(&msg); 163 } 164 165 /** 166 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If 167 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns 168 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an 169 * error. 170 */ 171 static const char *config_get_ff(void) 172 { 173 const char *value; 174 175 if (repo_config_get_value(the_repository, "pull.ff", &value)) 176 return NULL; 177 178 switch (git_parse_maybe_bool(value)) { 179 case 0: 180 return "--no-ff"; 181 case 1: 182 return "--ff"; 183 } 184 185 if (!strcmp(value, "only")) 186 return "--ff-only"; 187 188 die(_("invalid value for '%s': '%s'"), "pull.ff", value); 189 } 190 191 /** 192 * Returns the default configured value for --rebase. It first looks for the 193 * value of "branch.$curr_branch.rebase", where $curr_branch is the current 194 * branch, and if HEAD is detached or the configuration key does not exist, 195 * looks for the value of "pull.rebase". If both configuration keys do not 196 * exist, returns REBASE_FALSE. 197 */ 198 static enum rebase_type config_get_rebase(int *rebase_unspecified) 199 { 200 struct branch *curr_branch = branch_get("HEAD"); 201 const char *value; 202 203 if (curr_branch) { 204 char *key = xstrfmt("branch.%s.rebase", curr_branch->name); 205 206 if (!repo_config_get_value(the_repository, key, &value)) { 207 enum rebase_type ret = parse_config_rebase(key, value, 1); 208 free(key); 209 return ret; 210 } 211 212 free(key); 213 } 214 215 if (!repo_config_get_value(the_repository, "pull.rebase", &value)) 216 return parse_config_rebase("pull.rebase", value, 1); 217 218 *rebase_unspecified = 1; 219 220 return REBASE_FALSE; 221 } 222 223 /** 224 * Read config variables. 225 */ 226 static int git_pull_config(const char *var, const char *value, 227 const struct config_context *ctx, void *cb) 228 { 229 if (!strcmp(var, "rebase.autostash")) { 230 /* 231 * run_rebase() also reads this option. The reason we handle it here is 232 * that when pull.rebase is true, a fast-forward may occur without 233 * invoking run_rebase(). We need to ensure that autostash is set even 234 * in the fast-forward case. 235 * 236 * run_merge() handles merge.autostash, so we don't handle it here. 237 */ 238 config_rebase_autostash = git_config_bool(var, value); 239 return 0; 240 } else if (!strcmp(var, "pull.autostash")) { 241 config_pull_autostash = git_config_bool(var, value); 242 return 0; 243 } else if (!strcmp(var, "submodule.recurse")) { 244 recurse_submodules = git_config_bool(var, value) ? 245 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; 246 return 0; 247 } else if (!strcmp(var, "gpg.mintrustlevel")) { 248 check_trust_level = 0; 249 } 250 251 return git_default_config(var, value, ctx, cb); 252 } 253 254 /** 255 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge 256 * into merge_heads. 257 */ 258 static void get_merge_heads(struct oid_array *merge_heads) 259 { 260 const char *filename = git_path_fetch_head(the_repository); 261 FILE *fp; 262 struct strbuf sb = STRBUF_INIT; 263 struct object_id oid; 264 265 fp = xfopen(filename, "r"); 266 while (strbuf_getline_lf(&sb, fp) != EOF) { 267 const char *p; 268 if (parse_oid_hex(sb.buf, &oid, &p)) 269 continue; /* invalid line: does not start with object ID */ 270 if (starts_with(p, "\tnot-for-merge\t")) 271 continue; /* ref is not-for-merge */ 272 oid_array_append(merge_heads, &oid); 273 } 274 fclose(fp); 275 strbuf_release(&sb); 276 } 277 278 /** 279 * Used by die_no_merge_candidates() as a for_each_remote() callback to 280 * retrieve the name of the remote if the repository only has one remote. 281 */ 282 static int get_only_remote(struct remote *remote, void *cb_data) 283 { 284 const char **remote_name = cb_data; 285 286 if (*remote_name) 287 return -1; 288 289 *remote_name = remote->name; 290 return 0; 291 } 292 293 /** 294 * Dies with the appropriate reason for why there are no merge candidates: 295 * 296 * 1. We fetched from a specific remote, and a refspec was given, but it ended 297 * up not fetching anything. This is usually because the user provided a 298 * wildcard refspec which had no matches on the remote end. 299 * 300 * 2. We fetched from a non-default remote, but didn't specify a branch to 301 * merge. We can't use the configured one because it applies to the default 302 * remote, thus the user must specify the branches to merge. 303 * 304 * 3. We fetched from the branch's or repo's default remote, but: 305 * 306 * a. We are not on a branch, so there will never be a configured branch to 307 * merge with. 308 * 309 * b. We are on a branch, but there is no configured branch to merge with. 310 * 311 * 4. We fetched from the branch's or repo's default remote, but the configured 312 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't 313 * part of the configured fetch refspec.) 314 */ 315 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs) 316 { 317 struct branch *curr_branch = branch_get("HEAD"); 318 const char *remote = curr_branch ? curr_branch->remote_name : NULL; 319 320 if (*refspecs) { 321 if (opt_rebase) 322 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched.")); 323 else 324 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched.")); 325 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n" 326 "matches on the remote end.")); 327 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) { 328 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n" 329 "a branch. Because this is not the default configured remote\n" 330 "for your current branch, you must specify a branch on the command line."), 331 repo); 332 } else if (!curr_branch) { 333 fprintf_ln(stderr, _("You are not currently on a branch.")); 334 if (opt_rebase) 335 fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); 336 else 337 fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); 338 fprintf_ln(stderr, _("See git-pull(1) for details.")); 339 fprintf(stderr, "\n"); 340 fprintf_ln(stderr, " git pull %s %s", _(""), _("")); 341 fprintf(stderr, "\n"); 342 } else if (!curr_branch->merge_nr) { 343 const char *remote_name = NULL; 344 345 if (for_each_remote(get_only_remote, &remote_name) || !remote_name) 346 remote_name = _(""); 347 348 fprintf_ln(stderr, _("There is no tracking information for the current branch.")); 349 if (opt_rebase) 350 fprintf_ln(stderr, _("Please specify which branch you want to rebase against.")); 351 else 352 fprintf_ln(stderr, _("Please specify which branch you want to merge with.")); 353 fprintf_ln(stderr, _("See git-pull(1) for details.")); 354 fprintf(stderr, "\n"); 355 fprintf_ln(stderr, " git pull %s %s", _(""), _("")); 356 fprintf(stderr, "\n"); 357 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:")); 358 fprintf(stderr, "\n"); 359 fprintf_ln(stderr, " git branch --set-upstream-to=%s/%s %s\n", 360 remote_name, _(""), curr_branch->name); 361 } else 362 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n" 363 "from the remote, but no such ref was fetched."), 364 curr_branch->merge[0]->src); 365 exit(1); 366 } 367 368 /** 369 * Parses argv into [ [...]], returning their values in `repo` 370 * as a string and `refspecs` as a null-terminated array of strings. If `repo` 371 * is not provided in argv, it is set to NULL. 372 */ 373 static void parse_repo_refspecs(int argc, const char **argv, const char **repo, 374 const char ***refspecs) 375 { 376 if (argc > 0) { 377 *repo = *argv++; 378 argc--; 379 } else 380 *repo = NULL; 381 *refspecs = argv; 382 } 383 384 /** 385 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the 386 * repository and refspecs to fetch, or NULL if they are not provided. 387 */ 388 static int run_fetch(const char *repo, const char **refspecs) 389 { 390 struct child_process cmd = CHILD_PROCESS_INIT; 391 392 strvec_pushl(&cmd.args, "fetch", "--update-head-ok", NULL); 393 394 /* Shared options */ 395 argv_push_verbosity(&cmd.args); 396 if (opt_progress) 397 strvec_push(&cmd.args, opt_progress); 398 399 /* Options passed to git-fetch */ 400 if (opt_all) 401 strvec_push(&cmd.args, opt_all); 402 if (opt_append) 403 strvec_push(&cmd.args, opt_append); 404 if (opt_upload_pack) 405 strvec_push(&cmd.args, opt_upload_pack); 406 argv_push_force(&cmd.args); 407 if (opt_tags) 408 strvec_push(&cmd.args, opt_tags); 409 if (opt_prune) 410 strvec_push(&cmd.args, opt_prune); 411 if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) 412 switch (recurse_submodules_cli) { 413 case RECURSE_SUBMODULES_ON: 414 strvec_push(&cmd.args, "--recurse-submodules=on"); 415 break; 416 case RECURSE_SUBMODULES_OFF: 417 strvec_push(&cmd.args, "--recurse-submodules=no"); 418 break; 419 case RECURSE_SUBMODULES_ON_DEMAND: 420 strvec_push(&cmd.args, "--recurse-submodules=on-demand"); 421 break; 422 default: 423 BUG("submodule recursion option not understood"); 424 } 425 if (max_children) 426 strvec_push(&cmd.args, max_children); 427 if (opt_dry_run) 428 strvec_push(&cmd.args, "--dry-run"); 429 if (opt_keep) 430 strvec_push(&cmd.args, opt_keep); 431 if (opt_depth) 432 strvec_push(&cmd.args, opt_depth); 433 if (opt_unshallow) 434 strvec_push(&cmd.args, opt_unshallow); 435 if (opt_update_shallow) 436 strvec_push(&cmd.args, opt_update_shallow); 437 if (opt_refmap) 438 strvec_push(&cmd.args, opt_refmap); 439 if (opt_ipv4) 440 strvec_push(&cmd.args, opt_ipv4); 441 if (opt_ipv6) 442 strvec_push(&cmd.args, opt_ipv6); 443 if (opt_show_forced_updates > 0) 444 strvec_push(&cmd.args, "--show-forced-updates"); 445 else if (opt_show_forced_updates == 0) 446 strvec_push(&cmd.args, "--no-show-forced-updates"); 447 if (set_upstream) 448 strvec_push(&cmd.args, set_upstream); 449 strvec_pushv(&cmd.args, opt_fetch.v); 450 451 if (repo) { 452 strvec_push(&cmd.args, repo); 453 strvec_pushv(&cmd.args, refspecs); 454 } else if (*refspecs) 455 BUG("refspecs without repo?"); 456 cmd.git_cmd = 1; 457 cmd.odb_to_close = the_repository->objects; 458 return run_command(&cmd); 459 } 460 461 /** 462 * "Pulls into void" by branching off merge_head. 463 */ 464 static int pull_into_void(const struct object_id *merge_head, 465 const struct object_id *curr_head) 466 { 467 if (opt_verify_signatures) { 468 struct commit *commit; 469 470 commit = lookup_commit(the_repository, merge_head); 471 if (!commit) 472 die(_("unable to access commit %s"), 473 oid_to_hex(merge_head)); 474 475 verify_merge_signature(commit, opt_verbosity, 476 check_trust_level); 477 } 478 479 /* 480 * Two-way merge: we treat the index as based on an empty tree, 481 * and try to fast-forward to HEAD. This ensures we will not lose 482 * index/worktree changes that the user already made on the unborn 483 * branch. 484 */ 485 if (checkout_fast_forward(the_repository, 486 the_hash_algo->empty_tree, 487 merge_head, 0)) 488 return 1; 489 490 if (refs_update_ref(get_main_ref_store(the_repository), "initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) 491 return 1; 492 493 return 0; 494 } 495 496 static int rebase_submodules(void) 497 { 498 struct child_process cp = CHILD_PROCESS_INIT; 499 500 cp.git_cmd = 1; 501 cp.no_stdin = 1; 502 strvec_pushl(&cp.args, "submodule", "update", 503 "--recursive", "--rebase", NULL); 504 argv_push_verbosity(&cp.args); 505 506 return run_command(&cp); 507 } 508 509 static int update_submodules(void) 510 { 511 struct child_process cp = CHILD_PROCESS_INIT; 512 513 cp.git_cmd = 1; 514 cp.no_stdin = 1; 515 strvec_pushl(&cp.args, "submodule", "update", 516 "--recursive", "--checkout", NULL); 517 argv_push_verbosity(&cp.args); 518 519 return run_command(&cp); 520 } 521 522 /** 523 * Runs git-merge, returning its exit status. 524 */ 525 static int run_merge(void) 526 { 527 struct child_process cmd = CHILD_PROCESS_INIT; 528 529 strvec_pushl(&cmd.args, "merge", NULL); 530 531 /* Shared options */ 532 argv_push_verbosity(&cmd.args); 533 if (opt_progress) 534 strvec_push(&cmd.args, opt_progress); 535 536 /* Options passed to git-merge */ 537 if (opt_diffstat) 538 strvec_push(&cmd.args, opt_diffstat); 539 if (opt_log) 540 strvec_push(&cmd.args, opt_log); 541 if (opt_signoff) 542 strvec_push(&cmd.args, opt_signoff); 543 if (opt_squash) 544 strvec_push(&cmd.args, opt_squash); 545 if (opt_commit) 546 strvec_push(&cmd.args, opt_commit); 547 if (opt_edit) 548 strvec_push(&cmd.args, opt_edit); 549 if (cleanup_arg) 550 strvec_pushf(&cmd.args, "--cleanup=%s", cleanup_arg); 551 if (opt_ff) 552 strvec_push(&cmd.args, opt_ff); 553 if (opt_verify) 554 strvec_push(&cmd.args, opt_verify); 555 if (opt_verify_signatures) 556 strvec_push(&cmd.args, opt_verify_signatures); 557 strvec_pushv(&cmd.args, opt_strategies.v); 558 strvec_pushv(&cmd.args, opt_strategy_opts.v); 559 if (opt_gpg_sign) 560 strvec_push(&cmd.args, opt_gpg_sign); 561 if (opt_autostash == 0) 562 strvec_push(&cmd.args, "--no-autostash"); 563 else if (opt_autostash == 1) 564 strvec_push(&cmd.args, "--autostash"); 565 if (opt_allow_unrelated_histories > 0) 566 strvec_push(&cmd.args, "--allow-unrelated-histories"); 567 568 strvec_push(&cmd.args, "FETCH_HEAD"); 569 cmd.git_cmd = 1; 570 return run_command(&cmd); 571 } 572 573 /** 574 * Returns remote's upstream branch for the current branch. If remote is NULL, 575 * the current branch's configured default remote is used. Returns NULL if 576 * `remote` does not name a valid remote, HEAD does not point to a branch, 577 * remote is not the branch's configured remote or the branch does not have any 578 * configured upstream branch. 579 */ 580 static const char *get_upstream_branch(const char *remote) 581 { 582 struct remote *rm; 583 struct branch *curr_branch; 584 const char *curr_branch_remote; 585 586 rm = remote_get(remote); 587 if (!rm) 588 return NULL; 589 590 curr_branch = branch_get("HEAD"); 591 if (!curr_branch) 592 return NULL; 593 594 curr_branch_remote = remote_for_branch(curr_branch, NULL); 595 assert(curr_branch_remote); 596 597 if (strcmp(curr_branch_remote, rm->name)) 598 return NULL; 599 600 return branch_get_upstream(curr_branch, NULL); 601 } 602 603 /** 604 * Derives the remote-tracking branch from the remote and refspec. 605 * 606 * FIXME: The current implementation assumes the default mapping of 607 * refs/heads/ to refs/remotes//. 608 */ 609 static const char *get_tracking_branch(const char *remote, const char *refspec) 610 { 611 struct refspec_item spec; 612 const char *spec_src; 613 const char *merge_branch; 614 615 if (!refspec_item_init_fetch(&spec, refspec, the_hash_algo)) 616 die(_("invalid refspec '%s'"), refspec); 617 spec_src = spec.src; 618 if (!*spec_src || !strcmp(spec_src, "HEAD")) 619 spec_src = "HEAD"; 620 else if (skip_prefix(spec_src, "heads/", &spec_src)) 621 ; 622 else if (skip_prefix(spec_src, "refs/heads/", &spec_src)) 623 ; 624 else if (starts_with(spec_src, "refs/") || 625 starts_with(spec_src, "tags/") || 626 starts_with(spec_src, "remotes/")) 627 spec_src = ""; 628 629 if (*spec_src) { 630 if (!strcmp(remote, ".")) 631 merge_branch = mkpath("refs/heads/%s", spec_src); 632 else 633 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src); 634 } else 635 merge_branch = NULL; 636 637 refspec_item_clear(&spec); 638 return merge_branch; 639 } 640 641 /** 642 * Given the repo and refspecs, sets fork_point to the point at which the 643 * current branch forked from its remote-tracking branch. Returns 0 on success, 644 * -1 on failure. 645 */ 646 static int get_rebase_fork_point(struct object_id *fork_point, const char *repo, 647 const char *refspec) 648 { 649 int ret; 650 struct branch *curr_branch; 651 const char *remote_branch; 652 struct child_process cp = CHILD_PROCESS_INIT; 653 struct strbuf sb = STRBUF_INIT; 654 655 curr_branch = branch_get("HEAD"); 656 if (!curr_branch) 657 return -1; 658 659 if (refspec) 660 remote_branch = get_tracking_branch(repo, refspec); 661 else 662 remote_branch = get_upstream_branch(repo); 663 664 if (!remote_branch) 665 return -1; 666 667 strvec_pushl(&cp.args, "merge-base", "--fork-point", 668 remote_branch, curr_branch->name, NULL); 669 cp.no_stdin = 1; 670 cp.no_stderr = 1; 671 cp.git_cmd = 1; 672 673 ret = capture_command(&cp, &sb, GIT_MAX_HEXSZ); 674 if (ret) 675 goto cleanup; 676 677 ret = get_oid_hex(sb.buf, fork_point); 678 if (ret) 679 goto cleanup; 680 681 cleanup: 682 strbuf_release(&sb); 683 return ret ? -1 : 0; 684 } 685 686 /** 687 * Sets merge_base to the octopus merge base of curr_head, merge_head and 688 * fork_point. Returns 0 if a merge base is found, 1 otherwise. 689 */ 690 static int get_octopus_merge_base(struct object_id *merge_base, 691 const struct object_id *curr_head, 692 const struct object_id *merge_head, 693 const struct object_id *fork_point) 694 { 695 struct commit_list *revs = NULL, *result = NULL; 696 697 commit_list_insert(lookup_commit_reference(the_repository, curr_head), 698 &revs); 699 commit_list_insert(lookup_commit_reference(the_repository, merge_head), 700 &revs); 701 if (!is_null_oid(fork_point)) 702 commit_list_insert(lookup_commit_reference(the_repository, fork_point), 703 &revs); 704 705 if (get_octopus_merge_bases(revs, &result) < 0) 706 exit(128); 707 commit_list_free(revs); 708 reduce_heads_replace(&result); 709 710 if (!result) 711 return 1; 712 713 oidcpy(merge_base, &result->item->object.oid); 714 commit_list_free(result); 715 return 0; 716 } 717 718 /** 719 * Given the current HEAD oid, the merge head returned from git-fetch and the 720 * fork point calculated by get_rebase_fork_point(), compute the and 721 * arguments to use for the upcoming git-rebase invocation. 722 */ 723 static int get_rebase_newbase_and_upstream(struct object_id *newbase, 724 struct object_id *upstream, 725 const struct object_id *curr_head, 726 const struct object_id *merge_head, 727 const struct object_id *fork_point) 728 { 729 struct object_id oct_merge_base; 730 731 if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point)) 732 if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point)) 733 fork_point = NULL; 734 735 if (fork_point && !is_null_oid(fork_point)) 736 oidcpy(upstream, fork_point); 737 else 738 oidcpy(upstream, merge_head); 739 740 oidcpy(newbase, merge_head); 741 742 return 0; 743 } 744 745 /** 746 * Given the and calculated by 747 * get_rebase_newbase_and_upstream(), runs git-rebase with the 748 * appropriate arguments and returns its exit status. 749 */ 750 static int run_rebase(const struct object_id *newbase, 751 const struct object_id *upstream) 752 { 753 struct child_process cmd = CHILD_PROCESS_INIT; 754 755 strvec_push(&cmd.args, "rebase"); 756 757 /* Shared options */ 758 argv_push_verbosity(&cmd.args); 759 760 /* Options passed to git-rebase */ 761 if (opt_rebase == REBASE_MERGES) 762 strvec_push(&cmd.args, "--rebase-merges"); 763 else if (opt_rebase == REBASE_INTERACTIVE) 764 strvec_push(&cmd.args, "--interactive"); 765 if (opt_diffstat) 766 strvec_push(&cmd.args, opt_diffstat); 767 strvec_pushv(&cmd.args, opt_strategies.v); 768 strvec_pushv(&cmd.args, opt_strategy_opts.v); 769 if (opt_gpg_sign) 770 strvec_push(&cmd.args, opt_gpg_sign); 771 if (opt_signoff) 772 strvec_push(&cmd.args, opt_signoff); 773 if (opt_autostash == 0) 774 strvec_push(&cmd.args, "--no-autostash"); 775 else if (opt_autostash == 1) 776 strvec_push(&cmd.args, "--autostash"); 777 if (opt_verify_signatures && 778 !strcmp(opt_verify_signatures, "--verify-signatures")) 779 warning(_("ignoring --verify-signatures for rebase")); 780 781 strvec_push(&cmd.args, "--onto"); 782 strvec_push(&cmd.args, oid_to_hex(newbase)); 783 784 strvec_push(&cmd.args, oid_to_hex(upstream)); 785 786 cmd.git_cmd = 1; 787 return run_command(&cmd); 788 } 789 790 static int get_can_ff(struct object_id *orig_head, 791 struct oid_array *merge_heads) 792 { 793 int ret; 794 struct commit_list *list = NULL; 795 struct commit *merge_head, *head; 796 struct object_id *orig_merge_head; 797 798 if (merge_heads->nr > 1) 799 return 0; 800 801 orig_merge_head = &merge_heads->oid[0]; 802 head = lookup_commit_reference(the_repository, orig_head); 803 if (!head) 804 return 0; 805 merge_head = lookup_commit_reference(the_repository, orig_merge_head); 806 if (!merge_head) 807 return 0; 808 commit_list_insert(head, &list); 809 ret = repo_is_descendant_of(the_repository, merge_head, list); 810 commit_list_free(list); 811 if (ret < 0) 812 exit(128); 813 return ret; 814 } 815 816 /* 817 * Is orig_head a descendant of _all_ merge_heads? 818 * Unfortunately is_descendant_of() cannot be used as it asks 819 * if orig_head is a descendant of at least one of them. 820 */ 821 static int already_up_to_date(struct object_id *orig_head, 822 struct oid_array *merge_heads) 823 { 824 struct commit *ours; 825 826 ours = lookup_commit_reference(the_repository, orig_head); 827 if (!ours) 828 return 0; 829 for (size_t i = 0; i < merge_heads->nr; i++) { 830 struct commit_list *list = NULL; 831 struct commit *theirs; 832 int ok; 833 834 theirs = lookup_commit_reference(the_repository, &merge_heads->oid[i]); 835 if (!theirs) 836 return 0; 837 commit_list_insert(theirs, &list); 838 ok = repo_is_descendant_of(the_repository, ours, list); 839 commit_list_free(list); 840 if (ok < 0) 841 exit(128); 842 if (!ok) 843 return 0; 844 } 845 return 1; 846 } 847 848 static void show_advice_pull_non_ff(void) 849 { 850 advise(_("You have divergent branches and need to specify how to reconcile them.\n" 851 "You can do so by running one of the following commands sometime before\n" 852 "your next pull:\n" 853 "\n" 854 " git config pull.rebase false # merge\n" 855 " git config pull.rebase true # rebase\n" 856 " git config pull.ff only # fast-forward only\n" 857 "\n" 858 "You can replace \"git config\" with \"git config --global\" to set a default\n" 859 "preference for all repositories. You can also pass --rebase, --no-rebase,\n" 860 "or --ff-only on the command line to override the configured default per\n" 861 "invocation.\n")); 862 } 863 864 int cmd_pull(int argc, 865 const char **argv, 866 const char *prefix, 867 struct repository *repository UNUSED) 868 { 869 const char *repo, **refspecs; 870 struct oid_array merge_heads = OID_ARRAY_INIT; 871 struct object_id orig_head, curr_head; 872 struct object_id rebase_fork_point; 873 int rebase_unspecified = 0; 874 int can_ff; 875 int divergent; 876 int ret; 877 static struct option pull_options[] = { 878 /* Shared options */ 879 OPT__VERBOSITY(&opt_verbosity), 880 OPT_PASSTHRU(0, "progress", &opt_progress, NULL, 881 N_("force progress reporting"), 882 PARSE_OPT_NOARG), 883 OPT_CALLBACK_F(0, "recurse-submodules", 884 &recurse_submodules_cli, N_("on-demand"), 885 N_("control for recursive fetching of submodules"), 886 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules), 887 888 /* Options passed to git-merge or git-rebase */ 889 OPT_GROUP(N_("Options related to merging")), 890 OPT_CALLBACK_F('r', "rebase", &opt_rebase, 891 "(false|true|merges|interactive)", 892 N_("incorporate changes by rebasing rather than merging"), 893 PARSE_OPT_OPTARG, parse_opt_rebase), 894 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, 895 N_("do not show a diffstat at the end of the merge"), 896 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 897 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL, 898 N_("show a diffstat at the end of the merge"), 899 PARSE_OPT_NOARG), 900 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL, 901 N_("(synonym to --stat)"), 902 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), 903 OPT_PASSTHRU(0, "compact-summary", &opt_diffstat, NULL, 904 N_("show a compact-summary at the end of the merge"), 905 PARSE_OPT_NOARG), 906 OPT_PASSTHRU(0, "log", &opt_log, N_("n"), 907 N_("add (at most ) entries from shortlog to merge commit message"), 908 PARSE_OPT_OPTARG), 909 OPT_PASSTHRU(0, "signoff", &opt_signoff, NULL, 910 N_("add a Signed-off-by trailer"), 911 PARSE_OPT_OPTARG), 912 OPT_PASSTHRU(0, "squash", &opt_squash, NULL, 913 N_("create a single commit instead of doing a merge"), 914 PARSE_OPT_NOARG), 915 OPT_PASSTHRU(0, "commit", &opt_commit, NULL, 916 N_("perform a commit if the merge succeeds (default)"), 917 PARSE_OPT_NOARG), 918 OPT_PASSTHRU(0, "edit", &opt_edit, NULL, 919 N_("edit message before committing"), 920 PARSE_OPT_NOARG), 921 OPT_CLEANUP(&cleanup_arg), 922 OPT_PASSTHRU(0, "ff", &opt_ff, NULL, 923 N_("allow fast-forward"), 924 PARSE_OPT_NOARG), 925 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL, 926 N_("abort if fast-forward is not possible"), 927 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 928 OPT_PASSTHRU(0, "verify", &opt_verify, NULL, 929 N_("control use of pre-merge-commit and commit-msg hooks"), 930 PARSE_OPT_NOARG), 931 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL, 932 N_("verify that the named commit has a valid GPG signature"), 933 PARSE_OPT_NOARG), 934 OPT_BOOL(0, "autostash", &opt_autostash, 935 N_("automatically stash/stash pop before and after")), 936 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"), 937 N_("merge strategy to use"), 938 0), 939 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts, 940 N_("option=value"), 941 N_("option for selected merge strategy"), 942 0), 943 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"), 944 N_("GPG sign commit"), 945 PARSE_OPT_OPTARG), 946 OPT_SET_INT(0, "allow-unrelated-histories", 947 &opt_allow_unrelated_histories, 948 N_("allow merging unrelated histories"), 1), 949 950 /* Options passed to git-fetch */ 951 OPT_GROUP(N_("Options related to fetching")), 952 OPT_PASSTHRU(0, "all", &opt_all, NULL, 953 N_("fetch from all remotes"), 954 PARSE_OPT_NOARG), 955 OPT_PASSTHRU('a', "append", &opt_append, NULL, 956 N_("append to .git/FETCH_HEAD instead of overwriting"), 957 PARSE_OPT_NOARG), 958 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"), 959 N_("path to upload pack on remote end"), 960 0), 961 OPT__FORCE(&opt_force, N_("force overwrite of local branch"), 0), 962 OPT_PASSTHRU('t', "tags", &opt_tags, NULL, 963 N_("fetch all tags and associated objects"), 964 PARSE_OPT_NOARG), 965 OPT_PASSTHRU('p', "prune", &opt_prune, NULL, 966 N_("prune remote-tracking branches no longer on remote"), 967 PARSE_OPT_NOARG), 968 OPT_PASSTHRU('j', "jobs", &max_children, N_("n"), 969 N_("number of submodules pulled in parallel"), 970 PARSE_OPT_OPTARG), 971 OPT_BOOL(0, "dry-run", &opt_dry_run, 972 N_("dry run")), 973 OPT_PASSTHRU('k', "keep", &opt_keep, NULL, 974 N_("keep downloaded pack"), 975 PARSE_OPT_NOARG), 976 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"), 977 N_("deepen history of shallow clone"), 978 0), 979 OPT_PASSTHRU_ARGV(0, "shallow-since", &opt_fetch, N_("time"), 980 N_("deepen history of shallow repository based on time"), 981 0), 982 OPT_PASSTHRU_ARGV(0, "shallow-exclude", &opt_fetch, N_("ref"), 983 N_("deepen history of shallow clone, excluding ref"), 984 0), 985 OPT_PASSTHRU_ARGV(0, "deepen", &opt_fetch, N_("n"), 986 N_("deepen history of shallow clone"), 987 0), 988 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL, 989 N_("convert to a complete repository"), 990 PARSE_OPT_NONEG | PARSE_OPT_NOARG), 991 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL, 992 N_("accept refs that update .git/shallow"), 993 PARSE_OPT_NOARG), 994 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"), 995 N_("specify fetch refmap"), 996 PARSE_OPT_NONEG), 997 OPT_PASSTHRU_ARGV('o', "server-option", &opt_fetch, 998 N_("server-specific"), 999 N_("option to transmit"), 1000 0), 1001 OPT_PASSTHRU('4', "ipv4", &opt_ipv4, NULL, 1002 N_("use IPv4 addresses only"), 1003 PARSE_OPT_NOARG), 1004 OPT_PASSTHRU('6', "ipv6", &opt_ipv6, NULL, 1005 N_("use IPv6 addresses only"), 1006 PARSE_OPT_NOARG), 1007 OPT_PASSTHRU_ARGV(0, "negotiation-restrict", &opt_fetch, N_("revision"), 1008 N_("report that we have only objects reachable from this object"), 1009 0), 1010 OPT_ALIAS(0, "negotiation-tip", "negotiation-restrict"), 1011 OPT_PASSTHRU_ARGV(0, "negotiation-include", &opt_fetch, N_("revision"), 1012 N_("ensure this ref is always sent as a negotiation have"), 1013 0), 1014 OPT_BOOL(0, "show-forced-updates", &opt_show_forced_updates, 1015 N_("check for forced-updates on all updated branches")), 1016 OPT_PASSTHRU(0, "set-upstream", &set_upstream, NULL, 1017 N_("set upstream for git pull/fetch"), 1018 PARSE_OPT_NOARG), 1019 1020 OPT_END() 1021 }; 1022 1023 if (!getenv("GIT_REFLOG_ACTION")) 1024 set_reflog_message(argc, argv); 1025 1026 repo_config(the_repository, git_pull_config, NULL); 1027 if (the_repository->gitdir) { 1028 prepare_repo_settings(the_repository); 1029 the_repository->settings.command_requires_full_index = 0; 1030 } 1031 1032 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0); 1033 if (opt_autostash == -1) 1034 opt_autostash = config_pull_autostash; 1035 1036 if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT) 1037 recurse_submodules = recurse_submodules_cli; 1038 1039 if (cleanup_arg) 1040 /* 1041 * this only checks the validity of cleanup_arg; we don't need 1042 * a valid value for use_editor 1043 */ 1044 get_cleanup_mode(cleanup_arg, 0); 1045 1046 parse_repo_refspecs(argc, argv, &repo, &refspecs); 1047 1048 if (!opt_ff) { 1049 opt_ff = xstrdup_or_null(config_get_ff()); 1050 /* 1051 * A subtle point: opt_ff was set on the line above via 1052 * reading from config. opt_rebase, in contrast, is set 1053 * before this point via command line options. The setting 1054 * of opt_rebase via reading from config (using 1055 * config_get_rebase()) does not happen until later. We 1056 * are relying on the next if-condition happening before 1057 * the config_get_rebase() call so that an explicit 1058 * "--rebase" can override a config setting of 1059 * pull.ff=only. 1060 */ 1061 if (opt_rebase >= 0 && opt_ff && !strcmp(opt_ff, "--ff-only")) { 1062 free(opt_ff); 1063 opt_ff = xstrdup("--ff"); 1064 } 1065 } 1066 1067 if (opt_rebase < 0) 1068 opt_rebase = config_get_rebase(&rebase_unspecified); 1069 1070 if (repo_read_index_unmerged(the_repository)) 1071 die_resolve_conflict("pull"); 1072 1073 if (file_exists(git_path_merge_head(the_repository))) 1074 die_conclude_merge(); 1075 1076 if (repo_get_oid(the_repository, "HEAD", &orig_head)) 1077 oidclr(&orig_head, the_repository->hash_algo); 1078 1079 if (opt_rebase) { 1080 if (opt_autostash == -1) 1081 opt_autostash = config_rebase_autostash; 1082 1083 if (is_null_oid(&orig_head) && !is_index_unborn(the_repository->index)) 1084 die(_("Updating an unborn branch with changes added to the index.")); 1085 1086 if (!opt_autostash) 1087 require_clean_work_tree(the_repository, 1088 N_("pull with rebase"), 1089 _("Please commit or stash them."), 1, 0); 1090 1091 if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs)) 1092 oidclr(&rebase_fork_point, the_repository->hash_algo); 1093 } 1094 1095 if (run_fetch(repo, refspecs)) 1096 return 1; 1097 1098 if (opt_dry_run) 1099 return 0; 1100 1101 if (repo_get_oid(the_repository, "HEAD", &curr_head)) 1102 oidclr(&curr_head, the_repository->hash_algo); 1103 1104 if (!is_null_oid(&orig_head) && !is_null_oid(&curr_head) && 1105 !oideq(&orig_head, &curr_head)) { 1106 /* 1107 * The fetch involved updating the current branch. 1108 * 1109 * The working tree and the index file are still based on 1110 * orig_head commit, but we are merging into curr_head. 1111 * Update the working tree to match curr_head. 1112 */ 1113 1114 warning(_("fetch updated the current branch head.\n" 1115 "fast-forwarding your working tree from\n" 1116 "commit %s."), oid_to_hex(&orig_head)); 1117 1118 if (checkout_fast_forward(the_repository, &orig_head, 1119 &curr_head, 0)) 1120 die(_("Cannot fast-forward your working tree.\n" 1121 "After making sure that you saved anything precious from\n" 1122 "$ git diff %s\n" 1123 "output, run\n" 1124 "$ git reset --hard\n" 1125 "to recover."), oid_to_hex(&orig_head)); 1126 } 1127 1128 get_merge_heads(&merge_heads); 1129 1130 if (!merge_heads.nr) 1131 die_no_merge_candidates(repo, refspecs); 1132 1133 if (is_null_oid(&orig_head)) { 1134 if (merge_heads.nr > 1) 1135 die(_("Cannot merge multiple branches into empty head.")); 1136 ret = pull_into_void(merge_heads.oid, &curr_head); 1137 goto cleanup; 1138 } 1139 if (merge_heads.nr > 1) { 1140 if (opt_rebase) 1141 die(_("Cannot rebase onto multiple branches.")); 1142 if (opt_ff && !strcmp(opt_ff, "--ff-only")) 1143 die(_("Cannot fast-forward to multiple branches.")); 1144 } 1145 1146 can_ff = get_can_ff(&orig_head, &merge_heads); 1147 divergent = !can_ff && !already_up_to_date(&orig_head, &merge_heads); 1148 1149 /* ff-only takes precedence over rebase */ 1150 if (opt_ff && !strcmp(opt_ff, "--ff-only")) { 1151 if (divergent) 1152 die_ff_impossible(); 1153 opt_rebase = REBASE_FALSE; 1154 } 1155 /* If no action specified and we can't fast forward, then warn. */ 1156 if (!opt_ff && rebase_unspecified && divergent) { 1157 show_advice_pull_non_ff(); 1158 die(_("Need to specify how to reconcile divergent branches.")); 1159 } 1160 1161 if (opt_rebase) { 1162 struct object_id newbase; 1163 struct object_id upstream; 1164 get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head, 1165 merge_heads.oid, &rebase_fork_point); 1166 1167 if ((recurse_submodules == RECURSE_SUBMODULES_ON || 1168 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) && 1169 submodule_touches_in_range(the_repository, &upstream, &curr_head)) 1170 die(_("cannot rebase with locally recorded submodule modifications")); 1171 1172 if (can_ff) { 1173 /* we can fast-forward this without invoking rebase */ 1174 free(opt_ff); 1175 opt_ff = xstrdup("--ff-only"); 1176 ret = run_merge(); 1177 } else { 1178 ret = run_rebase(&newbase, &upstream); 1179 } 1180 1181 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || 1182 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) 1183 ret = rebase_submodules(); 1184 1185 goto cleanup; 1186 } else { 1187 ret = run_merge(); 1188 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON || 1189 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)) 1190 ret = update_submodules(); 1191 goto cleanup; 1192 } 1193 1194 cleanup: 1195 oid_array_clear(&merge_heads); 1196 return ret; 1197 }