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