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