4 * Based on git-pull.sh by Junio C Hamano
6 * Fetch one or more remote refs and merge it/them into the current HEAD.
10 #include "parse-options.h"
12 #include "run-command.h"
13 #include "sha1-array.h"
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.
34 static enum rebase_type parse_config_rebase(const char *key, const char *value,
37 int v = git_config_maybe_bool("pull.rebase", value);
43 else if (!strcmp(value, "preserve"))
44 return REBASE_PRESERVE;
47 die(_("Invalid value for %s: %s"), key, value);
49 error(_("Invalid value for %s: %s"), key, value);
51 return REBASE_INVALID;
55 * Callback for --rebase, which parses arg with parse_config_rebase().
57 static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
59 enum rebase_type *value = opt->value;
62 *value = parse_config_rebase("--rebase", arg, 0);
64 *value = unset ? REBASE_FALSE : REBASE_TRUE;
65 return *value == REBASE_INVALID ? -1 : 0;
68 static const char * const pull_usage[] = {
69 N_("git pull [options] [<repository> [<refspec>...]]"),
74 static int opt_verbosity;
75 static char *opt_progress;
77 /* Options passed to git-merge or git-rebase */
78 static enum rebase_type opt_rebase = -1;
79 static char *opt_diffstat;
81 static char *opt_squash;
82 static char *opt_commit;
83 static char *opt_edit;
85 static char *opt_verify_signatures;
86 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
87 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
88 static char *opt_gpg_sign;
90 /* Options passed to git-fetch */
92 static char *opt_append;
93 static char *opt_upload_pack;
95 static char *opt_tags;
96 static char *opt_prune;
97 static char *opt_recurse_submodules;
98 static int opt_dry_run;
99 static char *opt_keep;
100 static char *opt_depth;
101 static char *opt_unshallow;
102 static char *opt_update_shallow;
103 static char *opt_refmap;
105 static struct option pull_options[] = {
107 OPT__VERBOSITY(&opt_verbosity),
108 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
109 N_("force progress reporting"),
112 /* Options passed to git-merge or git-rebase */
113 OPT_GROUP(N_("Options related to merging")),
114 { OPTION_CALLBACK, 'r', "rebase", &opt_rebase,
115 "false|true|preserve",
116 N_("incorporate changes by rebasing rather than merging"),
117 PARSE_OPT_OPTARG, parse_opt_rebase },
118 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
119 N_("do not show a diffstat at the end of the merge"),
120 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
121 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
122 N_("show a diffstat at the end of the merge"),
124 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
125 N_("(synonym to --stat)"),
126 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
127 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
128 N_("add (at most <n>) entries from shortlog to merge commit message"),
130 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
131 N_("create a single commit instead of doing a merge"),
133 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
134 N_("perform a commit if the merge succeeds (default)"),
136 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
137 N_("edit message before committing"),
139 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
140 N_("allow fast-forward"),
142 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
143 N_("abort if fast-forward is not possible"),
144 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
145 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
146 N_("verify that the named commit has a valid GPG signature"),
148 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
149 N_("merge strategy to use"),
151 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
153 N_("option for selected merge strategy"),
155 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
156 N_("GPG sign commit"),
159 /* Options passed to git-fetch */
160 OPT_GROUP(N_("Options related to fetching")),
161 OPT_PASSTHRU(0, "all", &opt_all, NULL,
162 N_("fetch from all remotes"),
164 OPT_PASSTHRU('a', "append", &opt_append, NULL,
165 N_("append to .git/FETCH_HEAD instead of overwriting"),
167 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
168 N_("path to upload pack on remote end"),
170 OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
171 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
172 N_("fetch all tags and associated objects"),
174 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
175 N_("prune remote-tracking branches no longer on remote"),
177 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
179 N_("control recursive fetching of submodules"),
181 OPT_BOOL(0, "dry-run", &opt_dry_run,
183 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
184 N_("keep downloaded pack"),
186 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
187 N_("deepen history of shallow clone"),
189 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
190 N_("convert to a complete repository"),
191 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
192 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
193 N_("accept refs that update .git/shallow"),
195 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
196 N_("specify fetch refmap"),
203 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
205 static void argv_push_verbosity(struct argv_array *arr)
209 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
210 argv_array_push(arr, "-v");
212 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
213 argv_array_push(arr, "-q");
217 * Pushes "-f" switches into arr to match the opt_force level.
219 static void argv_push_force(struct argv_array *arr)
221 int force = opt_force;
223 argv_array_push(arr, "-f");
227 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
229 static void set_reflog_message(int argc, const char **argv)
232 struct strbuf msg = STRBUF_INIT;
234 for (i = 0; i < argc; i++) {
236 strbuf_addch(&msg, ' ');
237 strbuf_addstr(&msg, argv[i]);
240 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
242 strbuf_release(&msg);
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
251 static const char *config_get_ff(void)
255 if (git_config_get_value("pull.ff", &value))
258 switch (git_config_maybe_bool("pull.ff", value)) {
265 if (!strcmp(value, "only"))
268 die(_("Invalid value for pull.ff: %s"), value);
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.
278 static enum rebase_type config_get_rebase(void)
280 struct branch *curr_branch = branch_get("HEAD");
284 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
286 if (!git_config_get_value(key, &value)) {
287 enum rebase_type ret = parse_config_rebase(key, value, 1);
295 if (!git_config_get_value("pull.rebase", &value))
296 return parse_config_rebase("pull.rebase", value, 1);
302 * Returns 1 if there are unstaged changes, 0 otherwise.
304 static int has_unstaged_changes(const char *prefix)
306 struct rev_info rev_info;
309 init_revisions(&rev_info, prefix);
310 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
311 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
312 diff_setup_done(&rev_info.diffopt);
313 result = run_diff_files(&rev_info, 0);
314 return diff_result_code(&rev_info.diffopt, result);
318 * Returns 1 if there are uncommitted changes, 0 otherwise.
320 static int has_uncommitted_changes(const char *prefix)
322 struct rev_info rev_info;
325 if (is_cache_unborn())
328 init_revisions(&rev_info, prefix);
329 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
330 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
331 add_head_to_pending(&rev_info);
332 diff_setup_done(&rev_info.diffopt);
333 result = run_diff_index(&rev_info, 1);
334 return diff_result_code(&rev_info.diffopt, result);
338 * If the work tree has unstaged or uncommitted changes, dies with the
339 * appropriate message.
341 static void die_on_unclean_work_tree(const char *prefix)
343 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
346 hold_locked_index(lock_file, 0);
347 refresh_cache(REFRESH_QUIET);
348 update_index_if_able(&the_index, lock_file);
349 rollback_lock_file(lock_file);
351 if (has_unstaged_changes(prefix)) {
352 error(_("Cannot pull with rebase: You have unstaged changes."));
356 if (has_uncommitted_changes(prefix)) {
358 error(_("Additionally, your index contains uncommitted changes."));
360 error(_("Cannot pull with rebase: Your index contains uncommitted changes."));
369 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
372 static void get_merge_heads(struct sha1_array *merge_heads)
374 const char *filename = git_path("FETCH_HEAD");
376 struct strbuf sb = STRBUF_INIT;
377 unsigned char sha1[GIT_SHA1_RAWSZ];
379 if (!(fp = fopen(filename, "r")))
380 die_errno(_("could not open '%s' for reading"), filename);
381 while (strbuf_getline(&sb, fp, '\n') != EOF) {
382 if (get_sha1_hex(sb.buf, sha1))
383 continue; /* invalid line: does not start with SHA1 */
384 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
385 continue; /* ref is not-for-merge */
386 sha1_array_append(merge_heads, sha1);
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.
396 static int get_only_remote(struct remote *remote, void *cb_data)
398 const char **remote_name = cb_data;
403 *remote_name = remote->name;
408 * Dies with the appropriate reason for why there are no merge candidates:
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.
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.
418 * 3. We fetched from the branch's or repo's default remote, but:
420 * a. We are not on a branch, so there will never be a configured branch to
423 * b. We are on a branch, but there is no configured branch to merge with.
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.)
429 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
431 struct branch *curr_branch = branch_get("HEAD");
432 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
436 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched."));
438 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
439 fprintf_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))) {
442 fprintf_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."),
446 } else if (!curr_branch) {
447 fprintf_ln(stderr, _("You are not currently on a branch."));
449 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
451 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
452 fprintf_ln(stderr, _("See git-pull(1) for details."));
453 fprintf(stderr, "\n");
454 fprintf_ln(stderr, " git pull <remote> <branch>");
455 fprintf(stderr, "\n");
456 } else if (!curr_branch->merge_nr) {
457 const char *remote_name = NULL;
459 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
460 remote_name = "<remote>";
462 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
464 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
466 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
467 fprintf_ln(stderr, _("See git-pull(1) for details."));
468 fprintf(stderr, "\n");
469 fprintf_ln(stderr, " git pull <remote> <branch>");
470 fprintf(stderr, "\n");
471 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
473 " git branch --set-upstream-to=%s/<branch> %s\n"),
474 remote_name, curr_branch->name);
476 fprintf_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);
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.
487 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
488 const char ***refspecs)
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.
502 static int run_fetch(const char *repo, const char **refspecs)
504 struct argv_array args = ARGV_ARRAY_INIT;
507 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
510 argv_push_verbosity(&args);
512 argv_array_push(&args, opt_progress);
514 /* Options passed to git-fetch */
516 argv_array_push(&args, opt_all);
518 argv_array_push(&args, opt_append);
520 argv_array_push(&args, opt_upload_pack);
521 argv_push_force(&args);
523 argv_array_push(&args, opt_tags);
525 argv_array_push(&args, opt_prune);
526 if (opt_recurse_submodules)
527 argv_array_push(&args, opt_recurse_submodules);
529 argv_array_push(&args, "--dry-run");
531 argv_array_push(&args, opt_keep);
533 argv_array_push(&args, opt_depth);
535 argv_array_push(&args, opt_unshallow);
536 if (opt_update_shallow)
537 argv_array_push(&args, opt_update_shallow);
539 argv_array_push(&args, opt_refmap);
542 argv_array_push(&args, repo);
543 argv_array_pushv(&args, refspecs);
544 } else if (*refspecs)
545 die("BUG: refspecs without repo?");
546 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
547 argv_array_clear(&args);
552 * "Pulls into void" by branching off merge_head.
554 static int pull_into_void(const unsigned char *merge_head,
555 const unsigned char *curr_head)
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
563 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
566 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
573 * Runs git-merge, returning its exit status.
575 static int run_merge(void)
578 struct argv_array args = ARGV_ARRAY_INIT;
580 argv_array_pushl(&args, "merge", NULL);
583 argv_push_verbosity(&args);
585 argv_array_push(&args, opt_progress);
587 /* Options passed to git-merge */
589 argv_array_push(&args, opt_diffstat);
591 argv_array_push(&args, opt_log);
593 argv_array_push(&args, opt_squash);
595 argv_array_push(&args, opt_commit);
597 argv_array_push(&args, opt_edit);
599 argv_array_push(&args, opt_ff);
600 if (opt_verify_signatures)
601 argv_array_push(&args, opt_verify_signatures);
602 argv_array_pushv(&args, opt_strategies.argv);
603 argv_array_pushv(&args, opt_strategy_opts.argv);
605 argv_array_push(&args, opt_gpg_sign);
607 argv_array_push(&args, "FETCH_HEAD");
608 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
609 argv_array_clear(&args);
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.
620 static const char *get_upstream_branch(const char *remote)
623 struct branch *curr_branch;
624 const char *curr_branch_remote;
626 rm = remote_get(remote);
630 curr_branch = branch_get("HEAD");
634 curr_branch_remote = remote_for_branch(curr_branch, NULL);
635 assert(curr_branch_remote);
637 if (strcmp(curr_branch_remote, rm->name))
640 return branch_get_upstream(curr_branch, NULL);
644 * Derives the remote tracking branch from the remote and refspec.
646 * FIXME: The current implementation assumes the default mapping of
647 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
649 static const char *get_tracking_branch(const char *remote, const char *refspec)
651 struct refspec *spec;
652 const char *spec_src;
653 const char *merge_branch;
655 spec = parse_fetch_refspec(1, &refspec);
656 spec_src = spec->src;
657 if (!*spec_src || !strcmp(spec_src, "HEAD"))
659 else if (skip_prefix(spec_src, "heads/", &spec_src))
661 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
663 else if (starts_with(spec_src, "refs/") ||
664 starts_with(spec_src, "tags/") ||
665 starts_with(spec_src, "remotes/"))
669 if (!strcmp(remote, "."))
670 merge_branch = mkpath("refs/heads/%s", spec_src);
672 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src);
676 free_refspec(1, spec);
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,
685 static int get_rebase_fork_point(unsigned char *fork_point, const char *repo,
689 struct branch *curr_branch;
690 const char *remote_branch;
691 struct child_process cp = CHILD_PROCESS_INIT;
692 struct strbuf sb = STRBUF_INIT;
694 curr_branch = branch_get("HEAD");
699 remote_branch = get_tracking_branch(repo, refspec);
701 remote_branch = get_upstream_branch(repo);
706 argv_array_pushl(&cp.args, "merge-base", "--fork-point",
707 remote_branch, curr_branch->name, NULL);
712 ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ);
716 ret = get_sha1_hex(sb.buf, fork_point);
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.
729 static int get_octopus_merge_base(unsigned char *merge_base,
730 const unsigned char *curr_head,
731 const unsigned char *merge_head,
732 const unsigned char *fork_point)
734 struct commit_list *revs = NULL, *result;
736 commit_list_insert(lookup_commit_reference(curr_head), &revs);
737 commit_list_insert(lookup_commit_reference(merge_head), &revs);
738 if (!is_null_sha1(fork_point))
739 commit_list_insert(lookup_commit_reference(fork_point), &revs);
741 result = reduce_heads(get_octopus_merge_bases(revs));
742 free_commit_list(revs);
746 hashcpy(merge_base, result->item->object.sha1);
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.
755 static int run_rebase(const unsigned char *curr_head,
756 const unsigned char *merge_head,
757 const unsigned char *fork_point)
760 unsigned char oct_merge_base[GIT_SHA1_RAWSZ];
761 struct argv_array args = ARGV_ARRAY_INIT;
763 if (!get_octopus_merge_base(oct_merge_base, curr_head, merge_head, fork_point))
764 if (!is_null_sha1(fork_point) && !hashcmp(oct_merge_base, fork_point))
767 argv_array_push(&args, "rebase");
770 argv_push_verbosity(&args);
772 /* Options passed to git-rebase */
773 if (opt_rebase == REBASE_PRESERVE)
774 argv_array_push(&args, "--preserve-merges");
776 argv_array_push(&args, opt_diffstat);
777 argv_array_pushv(&args, opt_strategies.argv);
778 argv_array_pushv(&args, opt_strategy_opts.argv);
780 argv_array_push(&args, opt_gpg_sign);
782 argv_array_push(&args, "--onto");
783 argv_array_push(&args, sha1_to_hex(merge_head));
785 if (fork_point && !is_null_sha1(fork_point))
786 argv_array_push(&args, sha1_to_hex(fork_point));
788 argv_array_push(&args, sha1_to_hex(merge_head));
790 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
791 argv_array_clear(&args);
795 int cmd_pull(int argc, const char **argv, const char *prefix)
797 const char *repo, **refspecs;
798 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
799 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
800 unsigned char rebase_fork_point[GIT_SHA1_RAWSZ];
802 if (!getenv("GIT_REFLOG_ACTION"))
803 set_reflog_message(argc, argv);
805 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
807 parse_repo_refspecs(argc, argv, &repo, &refspecs);
810 opt_ff = xstrdup_or_null(config_get_ff());
813 opt_rebase = config_get_rebase();
815 git_config(git_default_config, NULL);
817 if (read_cache_unmerged())
818 die_resolve_conflict("Pull");
820 if (file_exists(git_path("MERGE_HEAD")))
821 die_conclude_merge();
823 if (get_sha1("HEAD", orig_head))
829 if (is_null_sha1(orig_head) && !is_cache_unborn())
830 die(_("Updating an unborn branch with changes added to the index."));
832 git_config_get_bool("rebase.autostash", &autostash);
834 die_on_unclean_work_tree(prefix);
836 if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
837 hashclr(rebase_fork_point);
840 if (run_fetch(repo, refspecs))
846 if (get_sha1("HEAD", curr_head))
849 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
850 hashcmp(orig_head, curr_head)) {
852 * The fetch involved updating the current branch.
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.
859 warning(_("fetch updated the current branch head.\n"
860 "fast-forwarding your working tree from\n"
861 "commit %s."), sha1_to_hex(orig_head));
863 if (checkout_fast_forward(orig_head, curr_head, 0))
864 die(_("Cannot fast-forward your working tree.\n"
865 "After making sure that you saved anything precious from\n"
868 "$ git reset --hard\n"
869 "to recover."), sha1_to_hex(orig_head));
872 get_merge_heads(&merge_heads);
875 die_no_merge_candidates(repo, refspecs);
877 if (is_null_sha1(orig_head)) {
878 if (merge_heads.nr > 1)
879 die(_("Cannot merge multiple branches into empty head."));
880 return pull_into_void(*merge_heads.sha1, curr_head);
881 } else if (opt_rebase) {
882 if (merge_heads.nr > 1)
883 die(_("Cannot rebase onto multiple branches."));
884 return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);