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