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