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#include"revision.h" 18#include"tempfile.h" 19#include"lockfile.h" 20 21enum rebase_type { 22 REBASE_INVALID = -1, 23 REBASE_FALSE =0, 24 REBASE_TRUE, 25 REBASE_PRESERVE 26}; 27 28/** 29 * Parses the value of --rebase. If value is a false value, returns 30 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is 31 * "preserve", returns REBASE_PRESERVE. If value is a invalid value, dies with 32 * a fatal error if fatal is true, otherwise returns REBASE_INVALID. 33 */ 34static enum rebase_type parse_config_rebase(const char*key,const char*value, 35int fatal) 36{ 37int v =git_config_maybe_bool("pull.rebase", value); 38 39if(!v) 40return REBASE_FALSE; 41else if(v >0) 42return REBASE_TRUE; 43else if(!strcmp(value,"preserve")) 44return REBASE_PRESERVE; 45 46if(fatal) 47die(_("Invalid value for%s:%s"), key, value); 48else 49error(_("Invalid value for%s:%s"), key, value); 50 51return REBASE_INVALID; 52} 53 54/** 55 * Callback for --rebase, which parses arg with parse_config_rebase(). 56 */ 57static intparse_opt_rebase(const struct option *opt,const char*arg,int unset) 58{ 59enum rebase_type *value = opt->value; 60 61if(arg) 62*value =parse_config_rebase("--rebase", arg,0); 63else 64*value = unset ? REBASE_FALSE : REBASE_TRUE; 65return*value == REBASE_INVALID ? -1:0; 66} 67 68static const char*const pull_usage[] = { 69N_("git pull [<options>] [<repository> [<refspec>...]]"), 70 NULL 71}; 72 73/* Shared options */ 74static int opt_verbosity; 75static char*opt_progress; 76 77/* Options passed to git-merge or git-rebase */ 78static enum rebase_type opt_rebase = -1; 79static char*opt_diffstat; 80static char*opt_log; 81static char*opt_squash; 82static char*opt_commit; 83static char*opt_edit; 84static char*opt_ff; 85static char*opt_verify_signatures; 86static struct argv_array opt_strategies = ARGV_ARRAY_INIT; 87static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT; 88static char*opt_gpg_sign; 89 90/* Options passed to git-fetch */ 91static char*opt_all; 92static char*opt_append; 93static char*opt_upload_pack; 94static int opt_force; 95static char*opt_tags; 96static char*opt_prune; 97static char*opt_recurse_submodules; 98static char*max_children; 99static int opt_dry_run; 100static char*opt_keep; 101static char*opt_depth; 102static char*opt_unshallow; 103static char*opt_update_shallow; 104static char*opt_refmap; 105 106static struct option pull_options[] = { 107/* Shared options */ 108OPT__VERBOSITY(&opt_verbosity), 109OPT_PASSTHRU(0,"progress", &opt_progress, NULL, 110N_("force progress reporting"), 111 PARSE_OPT_NOARG), 112 113/* Options passed to git-merge or git-rebase */ 114OPT_GROUP(N_("Options related to merging")), 115{ OPTION_CALLBACK,'r',"rebase", &opt_rebase, 116"false|true|preserve", 117N_("incorporate changes by rebasing rather than merging"), 118 PARSE_OPT_OPTARG, parse_opt_rebase }, 119OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL, 120N_("do not show a diffstat at the end of the merge"), 121 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 122OPT_PASSTHRU(0,"stat", &opt_diffstat, NULL, 123N_("show a diffstat at the end of the merge"), 124 PARSE_OPT_NOARG), 125OPT_PASSTHRU(0,"summary", &opt_diffstat, NULL, 126N_("(synonym to --stat)"), 127 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN), 128OPT_PASSTHRU(0,"log", &opt_log,N_("n"), 129N_("add (at most <n>) entries from shortlog to merge commit message"), 130 PARSE_OPT_OPTARG), 131OPT_PASSTHRU(0,"squash", &opt_squash, NULL, 132N_("create a single commit instead of doing a merge"), 133 PARSE_OPT_NOARG), 134OPT_PASSTHRU(0,"commit", &opt_commit, NULL, 135N_("perform a commit if the merge succeeds (default)"), 136 PARSE_OPT_NOARG), 137OPT_PASSTHRU(0,"edit", &opt_edit, NULL, 138N_("edit message before committing"), 139 PARSE_OPT_NOARG), 140OPT_PASSTHRU(0,"ff", &opt_ff, NULL, 141N_("allow fast-forward"), 142 PARSE_OPT_NOARG), 143OPT_PASSTHRU(0,"ff-only", &opt_ff, NULL, 144N_("abort if fast-forward is not possible"), 145 PARSE_OPT_NOARG | PARSE_OPT_NONEG), 146OPT_PASSTHRU(0,"verify-signatures", &opt_verify_signatures, NULL, 147N_("verify that the named commit has a valid GPG signature"), 148 PARSE_OPT_NOARG), 149OPT_PASSTHRU_ARGV('s',"strategy", &opt_strategies,N_("strategy"), 150N_("merge strategy to use"), 1510), 152OPT_PASSTHRU_ARGV('X',"strategy-option", &opt_strategy_opts, 153N_("option=value"), 154N_("option for selected merge strategy"), 1550), 156OPT_PASSTHRU('S',"gpg-sign", &opt_gpg_sign,N_("key-id"), 157N_("GPG sign commit"), 158 PARSE_OPT_OPTARG), 159 160/* Options passed to git-fetch */ 161OPT_GROUP(N_("Options related to fetching")), 162OPT_PASSTHRU(0,"all", &opt_all, NULL, 163N_("fetch from all remotes"), 164 PARSE_OPT_NOARG), 165OPT_PASSTHRU('a',"append", &opt_append, NULL, 166N_("append to .git/FETCH_HEAD instead of overwriting"), 167 PARSE_OPT_NOARG), 168OPT_PASSTHRU(0,"upload-pack", &opt_upload_pack,N_("path"), 169N_("path to upload pack on remote end"), 1700), 171OPT__FORCE(&opt_force,N_("force overwrite of local branch")), 172OPT_PASSTHRU('t',"tags", &opt_tags, NULL, 173N_("fetch all tags and associated objects"), 174 PARSE_OPT_NOARG), 175OPT_PASSTHRU('p',"prune", &opt_prune, NULL, 176N_("prune remote-tracking branches no longer on remote"), 177 PARSE_OPT_NOARG), 178OPT_PASSTHRU(0,"recurse-submodules", &opt_recurse_submodules, 179N_("on-demand"), 180N_("control recursive fetching of submodules"), 181 PARSE_OPT_OPTARG), 182OPT_PASSTHRU('j',"jobs", &max_children,N_("n"), 183N_("number of submodules pulled in parallel"), 184 PARSE_OPT_OPTARG), 185OPT_BOOL(0,"dry-run", &opt_dry_run, 186N_("dry run")), 187OPT_PASSTHRU('k',"keep", &opt_keep, NULL, 188N_("keep downloaded pack"), 189 PARSE_OPT_NOARG), 190OPT_PASSTHRU(0,"depth", &opt_depth,N_("depth"), 191N_("deepen history of shallow clone"), 1920), 193OPT_PASSTHRU(0,"unshallow", &opt_unshallow, NULL, 194N_("convert to a complete repository"), 195 PARSE_OPT_NONEG | PARSE_OPT_NOARG), 196OPT_PASSTHRU(0,"update-shallow", &opt_update_shallow, NULL, 197N_("accept refs that update .git/shallow"), 198 PARSE_OPT_NOARG), 199OPT_PASSTHRU(0,"refmap", &opt_refmap,N_("refmap"), 200N_("specify fetch refmap"), 201 PARSE_OPT_NONEG), 202 203OPT_END() 204}; 205 206/** 207 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level. 208 */ 209static voidargv_push_verbosity(struct argv_array *arr) 210{ 211int verbosity; 212 213for(verbosity = opt_verbosity; verbosity >0; verbosity--) 214argv_array_push(arr,"-v"); 215 216for(verbosity = opt_verbosity; verbosity <0; verbosity++) 217argv_array_push(arr,"-q"); 218} 219 220/** 221 * Pushes "-f" switches into arr to match the opt_force level. 222 */ 223static voidargv_push_force(struct argv_array *arr) 224{ 225int force = opt_force; 226while(force-- >0) 227argv_array_push(arr,"-f"); 228} 229 230/** 231 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv 232 */ 233static voidset_reflog_message(int argc,const char**argv) 234{ 235int i; 236struct strbuf msg = STRBUF_INIT; 237 238for(i =0; i < argc; i++) { 239if(i) 240strbuf_addch(&msg,' '); 241strbuf_addstr(&msg, argv[i]); 242} 243 244setenv("GIT_REFLOG_ACTION", msg.buf,0); 245 246strbuf_release(&msg); 247} 248 249/** 250 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If 251 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns 252 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an 253 * error. 254 */ 255static const char*config_get_ff(void) 256{ 257const char*value; 258 259if(git_config_get_value("pull.ff", &value)) 260return NULL; 261 262switch(git_config_maybe_bool("pull.ff", value)) { 263case0: 264return"--no-ff"; 265case1: 266return"--ff"; 267} 268 269if(!strcmp(value,"only")) 270return"--ff-only"; 271 272die(_("Invalid value for pull.ff:%s"), value); 273} 274 275/** 276 * Returns the default configured value for --rebase. It first looks for the 277 * value of "branch.$curr_branch.rebase", where $curr_branch is the current 278 * branch, and if HEAD is detached or the configuration key does not exist, 279 * looks for the value of "pull.rebase". If both configuration keys do not 280 * exist, returns REBASE_FALSE. 281 */ 282static enum rebase_type config_get_rebase(void) 283{ 284struct branch *curr_branch =branch_get("HEAD"); 285const char*value; 286 287if(curr_branch) { 288char*key =xstrfmt("branch.%s.rebase", curr_branch->name); 289 290if(!git_config_get_value(key, &value)) { 291enum rebase_type ret =parse_config_rebase(key, value,1); 292free(key); 293return ret; 294} 295 296free(key); 297} 298 299if(!git_config_get_value("pull.rebase", &value)) 300returnparse_config_rebase("pull.rebase", value,1); 301 302return REBASE_FALSE; 303} 304 305/** 306 * Returns 1 if there are unstaged changes, 0 otherwise. 307 */ 308static inthas_unstaged_changes(const char*prefix) 309{ 310struct rev_info rev_info; 311int result; 312 313init_revisions(&rev_info, prefix); 314DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES); 315DIFF_OPT_SET(&rev_info.diffopt, QUICK); 316diff_setup_done(&rev_info.diffopt); 317 result =run_diff_files(&rev_info,0); 318returndiff_result_code(&rev_info.diffopt, result); 319} 320 321/** 322 * Returns 1 if there are uncommitted changes, 0 otherwise. 323 */ 324static inthas_uncommitted_changes(const char*prefix) 325{ 326struct rev_info rev_info; 327int result; 328 329if(is_cache_unborn()) 330return0; 331 332init_revisions(&rev_info, prefix); 333DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES); 334DIFF_OPT_SET(&rev_info.diffopt, QUICK); 335add_head_to_pending(&rev_info); 336diff_setup_done(&rev_info.diffopt); 337 result =run_diff_index(&rev_info,1); 338returndiff_result_code(&rev_info.diffopt, result); 339} 340 341/** 342 * If the work tree has unstaged or uncommitted changes, dies with the 343 * appropriate message. 344 */ 345static voiddie_on_unclean_work_tree(const char*prefix) 346{ 347struct lock_file *lock_file =xcalloc(1,sizeof(*lock_file)); 348int do_die =0; 349 350hold_locked_index(lock_file,0); 351refresh_cache(REFRESH_QUIET); 352update_index_if_able(&the_index, lock_file); 353rollback_lock_file(lock_file); 354 355if(has_unstaged_changes(prefix)) { 356error(_("Cannot pull with rebase: You have unstaged changes.")); 357 do_die =1; 358} 359 360if(has_uncommitted_changes(prefix)) { 361if(do_die) 362error(_("Additionally, your index contains uncommitted changes.")); 363else 364error(_("Cannot pull with rebase: Your index contains uncommitted changes.")); 365 do_die =1; 366} 367 368if(do_die) 369exit(1); 370} 371 372/** 373 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge 374 * into merge_heads. 375 */ 376static voidget_merge_heads(struct sha1_array *merge_heads) 377{ 378const char*filename =git_path("FETCH_HEAD"); 379FILE*fp; 380struct strbuf sb = STRBUF_INIT; 381unsigned char sha1[GIT_SHA1_RAWSZ]; 382 383if(!(fp =fopen(filename,"r"))) 384die_errno(_("could not open '%s' for reading"), filename); 385while(strbuf_getline(&sb, fp,'\n') != EOF) { 386if(get_sha1_hex(sb.buf, sha1)) 387continue;/* invalid line: does not start with SHA1 */ 388if(starts_with(sb.buf + GIT_SHA1_HEXSZ,"\tnot-for-merge\t")) 389continue;/* ref is not-for-merge */ 390sha1_array_append(merge_heads, sha1); 391} 392fclose(fp); 393strbuf_release(&sb); 394} 395 396/** 397 * Used by die_no_merge_candidates() as a for_each_remote() callback to 398 * retrieve the name of the remote if the repository only has one remote. 399 */ 400static intget_only_remote(struct remote *remote,void*cb_data) 401{ 402const char**remote_name = cb_data; 403 404if(*remote_name) 405return-1; 406 407*remote_name = remote->name; 408return0; 409} 410 411/** 412 * Dies with the appropriate reason for why there are no merge candidates: 413 * 414 * 1. We fetched from a specific remote, and a refspec was given, but it ended 415 * up not fetching anything. This is usually because the user provided a 416 * wildcard refspec which had no matches on the remote end. 417 * 418 * 2. We fetched from a non-default remote, but didn't specify a branch to 419 * merge. We can't use the configured one because it applies to the default 420 * remote, thus the user must specify the branches to merge. 421 * 422 * 3. We fetched from the branch's or repo's default remote, but: 423 * 424 * a. We are not on a branch, so there will never be a configured branch to 425 * merge with. 426 * 427 * b. We are on a branch, but there is no configured branch to merge with. 428 * 429 * 4. We fetched from the branch's or repo's default remote, but the configured 430 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't 431 * part of the configured fetch refspec.) 432 */ 433static void NORETURN die_no_merge_candidates(const char*repo,const char**refspecs) 434{ 435struct branch *curr_branch =branch_get("HEAD"); 436const char*remote = curr_branch ? curr_branch->remote_name : NULL; 437 438if(*refspecs) { 439if(opt_rebase) 440fprintf_ln(stderr,_("There is no candidate for rebasing against among the refs that you just fetched.")); 441else 442fprintf_ln(stderr,_("There are no candidates for merging among the refs that you just fetched.")); 443fprintf_ln(stderr,_("Generally this means that you provided a wildcard refspec which had no\n" 444"matches on the remote end.")); 445}else if(repo && curr_branch && (!remote ||strcmp(repo, remote))) { 446fprintf_ln(stderr,_("You asked to pull from the remote '%s', but did not specify\n" 447"a branch. Because this is not the default configured remote\n" 448"for your current branch, you must specify a branch on the command line."), 449 repo); 450}else if(!curr_branch) { 451fprintf_ln(stderr,_("You are not currently on a branch.")); 452if(opt_rebase) 453fprintf_ln(stderr,_("Please specify which branch you want to rebase against.")); 454else 455fprintf_ln(stderr,_("Please specify which branch you want to merge with.")); 456fprintf_ln(stderr,_("See git-pull(1) for details.")); 457fprintf(stderr,"\n"); 458fprintf_ln(stderr," git pull <remote> <branch>"); 459fprintf(stderr,"\n"); 460}else if(!curr_branch->merge_nr) { 461const char*remote_name = NULL; 462 463if(for_each_remote(get_only_remote, &remote_name) || !remote_name) 464 remote_name ="<remote>"; 465 466fprintf_ln(stderr,_("There is no tracking information for the current branch.")); 467if(opt_rebase) 468fprintf_ln(stderr,_("Please specify which branch you want to rebase against.")); 469else 470fprintf_ln(stderr,_("Please specify which branch you want to merge with.")); 471fprintf_ln(stderr,_("See git-pull(1) for details.")); 472fprintf(stderr,"\n"); 473fprintf_ln(stderr," git pull <remote> <branch>"); 474fprintf(stderr,"\n"); 475fprintf_ln(stderr,_("If you wish to set tracking information for this branch you can do so with:\n" 476"\n" 477" git branch --set-upstream-to=%s/<branch>%s\n"), 478 remote_name, curr_branch->name); 479}else 480fprintf_ln(stderr,_("Your configuration specifies to merge with the ref '%s'\n" 481"from the remote, but no such ref was fetched."), 482*curr_branch->merge_name); 483exit(1); 484} 485 486/** 487 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo` 488 * as a string and `refspecs` as a null-terminated array of strings. If `repo` 489 * is not provided in argv, it is set to NULL. 490 */ 491static voidparse_repo_refspecs(int argc,const char**argv,const char**repo, 492const char***refspecs) 493{ 494if(argc >0) { 495*repo = *argv++; 496 argc--; 497}else 498*repo = NULL; 499*refspecs = argv; 500} 501 502/** 503 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the 504 * repository and refspecs to fetch, or NULL if they are not provided. 505 */ 506static intrun_fetch(const char*repo,const char**refspecs) 507{ 508struct argv_array args = ARGV_ARRAY_INIT; 509int ret; 510 511argv_array_pushl(&args,"fetch","--update-head-ok", NULL); 512 513/* Shared options */ 514argv_push_verbosity(&args); 515if(opt_progress) 516argv_array_push(&args, opt_progress); 517 518/* Options passed to git-fetch */ 519if(opt_all) 520argv_array_push(&args, opt_all); 521if(opt_append) 522argv_array_push(&args, opt_append); 523if(opt_upload_pack) 524argv_array_push(&args, opt_upload_pack); 525argv_push_force(&args); 526if(opt_tags) 527argv_array_push(&args, opt_tags); 528if(opt_prune) 529argv_array_push(&args, opt_prune); 530if(opt_recurse_submodules) 531argv_array_push(&args, opt_recurse_submodules); 532if(max_children) 533argv_array_push(&args, max_children); 534if(opt_dry_run) 535argv_array_push(&args,"--dry-run"); 536if(opt_keep) 537argv_array_push(&args, opt_keep); 538if(opt_depth) 539argv_array_push(&args, opt_depth); 540if(opt_unshallow) 541argv_array_push(&args, opt_unshallow); 542if(opt_update_shallow) 543argv_array_push(&args, opt_update_shallow); 544if(opt_refmap) 545argv_array_push(&args, opt_refmap); 546 547if(repo) { 548argv_array_push(&args, repo); 549argv_array_pushv(&args, refspecs); 550}else if(*refspecs) 551die("BUG: refspecs without repo?"); 552 ret =run_command_v_opt(args.argv, RUN_GIT_CMD); 553argv_array_clear(&args); 554return ret; 555} 556 557/** 558 * "Pulls into void" by branching off merge_head. 559 */ 560static intpull_into_void(const unsigned char*merge_head, 561const unsigned char*curr_head) 562{ 563/* 564 * Two-way merge: we treat the index as based on an empty tree, 565 * and try to fast-forward to HEAD. This ensures we will not lose 566 * index/worktree changes that the user already made on the unborn 567 * branch. 568 */ 569if(checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head,0)) 570return1; 571 572if(update_ref("initial pull","HEAD", merge_head, curr_head,0, UPDATE_REFS_DIE_ON_ERR)) 573return1; 574 575return0; 576} 577 578/** 579 * Runs git-merge, returning its exit status. 580 */ 581static intrun_merge(void) 582{ 583int ret; 584struct argv_array args = ARGV_ARRAY_INIT; 585 586argv_array_pushl(&args,"merge", NULL); 587 588/* Shared options */ 589argv_push_verbosity(&args); 590if(opt_progress) 591argv_array_push(&args, opt_progress); 592 593/* Options passed to git-merge */ 594if(opt_diffstat) 595argv_array_push(&args, opt_diffstat); 596if(opt_log) 597argv_array_push(&args, opt_log); 598if(opt_squash) 599argv_array_push(&args, opt_squash); 600if(opt_commit) 601argv_array_push(&args, opt_commit); 602if(opt_edit) 603argv_array_push(&args, opt_edit); 604if(opt_ff) 605argv_array_push(&args, opt_ff); 606if(opt_verify_signatures) 607argv_array_push(&args, opt_verify_signatures); 608argv_array_pushv(&args, opt_strategies.argv); 609argv_array_pushv(&args, opt_strategy_opts.argv); 610if(opt_gpg_sign) 611argv_array_push(&args, opt_gpg_sign); 612 613argv_array_push(&args,"FETCH_HEAD"); 614 ret =run_command_v_opt(args.argv, RUN_GIT_CMD); 615argv_array_clear(&args); 616return ret; 617} 618 619/** 620 * Returns remote's upstream branch for the current branch. If remote is NULL, 621 * the current branch's configured default remote is used. Returns NULL if 622 * `remote` does not name a valid remote, HEAD does not point to a branch, 623 * remote is not the branch's configured remote or the branch does not have any 624 * configured upstream branch. 625 */ 626static const char*get_upstream_branch(const char*remote) 627{ 628struct remote *rm; 629struct branch *curr_branch; 630const char*curr_branch_remote; 631 632 rm =remote_get(remote); 633if(!rm) 634return NULL; 635 636 curr_branch =branch_get("HEAD"); 637if(!curr_branch) 638return NULL; 639 640 curr_branch_remote =remote_for_branch(curr_branch, NULL); 641assert(curr_branch_remote); 642 643if(strcmp(curr_branch_remote, rm->name)) 644return NULL; 645 646returnbranch_get_upstream(curr_branch, NULL); 647} 648 649/** 650 * Derives the remote tracking branch from the remote and refspec. 651 * 652 * FIXME: The current implementation assumes the default mapping of 653 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>. 654 */ 655static const char*get_tracking_branch(const char*remote,const char*refspec) 656{ 657struct refspec *spec; 658const char*spec_src; 659const char*merge_branch; 660 661 spec =parse_fetch_refspec(1, &refspec); 662 spec_src = spec->src; 663if(!*spec_src || !strcmp(spec_src,"HEAD")) 664 spec_src ="HEAD"; 665else if(skip_prefix(spec_src,"heads/", &spec_src)) 666; 667else if(skip_prefix(spec_src,"refs/heads/", &spec_src)) 668; 669else if(starts_with(spec_src,"refs/") || 670starts_with(spec_src,"tags/") || 671starts_with(spec_src,"remotes/")) 672 spec_src =""; 673 674if(*spec_src) { 675if(!strcmp(remote,".")) 676 merge_branch =mkpath("refs/heads/%s", spec_src); 677else 678 merge_branch =mkpath("refs/remotes/%s/%s", remote, spec_src); 679}else 680 merge_branch = NULL; 681 682free_refspec(1, spec); 683return merge_branch; 684} 685 686/** 687 * Given the repo and refspecs, sets fork_point to the point at which the 688 * current branch forked from its remote tracking branch. Returns 0 on success, 689 * -1 on failure. 690 */ 691static intget_rebase_fork_point(unsigned char*fork_point,const char*repo, 692const char*refspec) 693{ 694int ret; 695struct branch *curr_branch; 696const char*remote_branch; 697struct child_process cp = CHILD_PROCESS_INIT; 698struct strbuf sb = STRBUF_INIT; 699 700 curr_branch =branch_get("HEAD"); 701if(!curr_branch) 702return-1; 703 704if(refspec) 705 remote_branch =get_tracking_branch(repo, refspec); 706else 707 remote_branch =get_upstream_branch(repo); 708 709if(!remote_branch) 710return-1; 711 712argv_array_pushl(&cp.args,"merge-base","--fork-point", 713 remote_branch, curr_branch->name, NULL); 714 cp.no_stdin =1; 715 cp.no_stderr =1; 716 cp.git_cmd =1; 717 718 ret =capture_command(&cp, &sb, GIT_SHA1_HEXSZ); 719if(ret) 720goto cleanup; 721 722 ret =get_sha1_hex(sb.buf, fork_point); 723if(ret) 724goto cleanup; 725 726cleanup: 727strbuf_release(&sb); 728return ret ? -1:0; 729} 730 731/** 732 * Sets merge_base to the octopus merge base of curr_head, merge_head and 733 * fork_point. Returns 0 if a merge base is found, 1 otherwise. 734 */ 735static intget_octopus_merge_base(unsigned char*merge_base, 736const unsigned char*curr_head, 737const unsigned char*merge_head, 738const unsigned char*fork_point) 739{ 740struct commit_list *revs = NULL, *result; 741 742commit_list_insert(lookup_commit_reference(curr_head), &revs); 743commit_list_insert(lookup_commit_reference(merge_head), &revs); 744if(!is_null_sha1(fork_point)) 745commit_list_insert(lookup_commit_reference(fork_point), &revs); 746 747 result =reduce_heads(get_octopus_merge_bases(revs)); 748free_commit_list(revs); 749if(!result) 750return1; 751 752hashcpy(merge_base, result->item->object.oid.hash); 753return0; 754} 755 756/** 757 * Given the current HEAD SHA1, the merge head returned from git-fetch and the 758 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the 759 * appropriate arguments and returns its exit status. 760 */ 761static intrun_rebase(const unsigned char*curr_head, 762const unsigned char*merge_head, 763const unsigned char*fork_point) 764{ 765int ret; 766unsigned char oct_merge_base[GIT_SHA1_RAWSZ]; 767struct argv_array args = ARGV_ARRAY_INIT; 768 769if(!get_octopus_merge_base(oct_merge_base, curr_head, merge_head, fork_point)) 770if(!is_null_sha1(fork_point) && !hashcmp(oct_merge_base, fork_point)) 771 fork_point = NULL; 772 773argv_array_push(&args,"rebase"); 774 775/* Shared options */ 776argv_push_verbosity(&args); 777 778/* Options passed to git-rebase */ 779if(opt_rebase == REBASE_PRESERVE) 780argv_array_push(&args,"--preserve-merges"); 781if(opt_diffstat) 782argv_array_push(&args, opt_diffstat); 783argv_array_pushv(&args, opt_strategies.argv); 784argv_array_pushv(&args, opt_strategy_opts.argv); 785if(opt_gpg_sign) 786argv_array_push(&args, opt_gpg_sign); 787 788argv_array_push(&args,"--onto"); 789argv_array_push(&args,sha1_to_hex(merge_head)); 790 791if(fork_point && !is_null_sha1(fork_point)) 792argv_array_push(&args,sha1_to_hex(fork_point)); 793else 794argv_array_push(&args,sha1_to_hex(merge_head)); 795 796 ret =run_command_v_opt(args.argv, RUN_GIT_CMD); 797argv_array_clear(&args); 798return ret; 799} 800 801intcmd_pull(int argc,const char**argv,const char*prefix) 802{ 803const char*repo, **refspecs; 804struct sha1_array merge_heads = SHA1_ARRAY_INIT; 805unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ]; 806unsigned char rebase_fork_point[GIT_SHA1_RAWSZ]; 807 808if(!getenv("GIT_REFLOG_ACTION")) 809set_reflog_message(argc, argv); 810 811 argc =parse_options(argc, argv, prefix, pull_options, pull_usage,0); 812 813parse_repo_refspecs(argc, argv, &repo, &refspecs); 814 815if(!opt_ff) 816 opt_ff =xstrdup_or_null(config_get_ff()); 817 818if(opt_rebase <0) 819 opt_rebase =config_get_rebase(); 820 821git_config(git_default_config, NULL); 822 823if(read_cache_unmerged()) 824die_resolve_conflict("Pull"); 825 826if(file_exists(git_path("MERGE_HEAD"))) 827die_conclude_merge(); 828 829if(get_sha1("HEAD", orig_head)) 830hashclr(orig_head); 831 832if(opt_rebase) { 833int autostash =0; 834 835if(is_null_sha1(orig_head) && !is_cache_unborn()) 836die(_("Updating an unborn branch with changes added to the index.")); 837 838git_config_get_bool("rebase.autostash", &autostash); 839if(!autostash) 840die_on_unclean_work_tree(prefix); 841 842if(get_rebase_fork_point(rebase_fork_point, repo, *refspecs)) 843hashclr(rebase_fork_point); 844} 845 846if(run_fetch(repo, refspecs)) 847return1; 848 849if(opt_dry_run) 850return0; 851 852if(get_sha1("HEAD", curr_head)) 853hashclr(curr_head); 854 855if(!is_null_sha1(orig_head) && !is_null_sha1(curr_head) && 856hashcmp(orig_head, curr_head)) { 857/* 858 * The fetch involved updating the current branch. 859 * 860 * The working tree and the index file are still based on 861 * orig_head commit, but we are merging into curr_head. 862 * Update the working tree to match curr_head. 863 */ 864 865warning(_("fetch updated the current branch head.\n" 866"fast-forwarding your working tree from\n" 867"commit%s."),sha1_to_hex(orig_head)); 868 869if(checkout_fast_forward(orig_head, curr_head,0)) 870die(_("Cannot fast-forward your working tree.\n" 871"After making sure that you saved anything precious from\n" 872"$ git diff%s\n" 873"output, run\n" 874"$ git reset --hard\n" 875"to recover."),sha1_to_hex(orig_head)); 876} 877 878get_merge_heads(&merge_heads); 879 880if(!merge_heads.nr) 881die_no_merge_candidates(repo, refspecs); 882 883if(is_null_sha1(orig_head)) { 884if(merge_heads.nr >1) 885die(_("Cannot merge multiple branches into empty head.")); 886returnpull_into_void(*merge_heads.sha1, curr_head); 887}else if(opt_rebase) { 888if(merge_heads.nr >1) 889die(_("Cannot rebase onto multiple branches.")); 890returnrun_rebase(curr_head, *merge_heads.sha1, rebase_fork_point); 891}else 892returnrun_merge(); 893}