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