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