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"
28 * Parses the value of --rebase. If value is a false value, returns
29 * REBASE_FALSE. If value is a true value, returns REBASE_TRUE. If value is
30 * "preserve", returns REBASE_PRESERVE. If value is a invalid value, dies with
31 * a fatal error if fatal is true, otherwise returns REBASE_INVALID.
33 static enum rebase_type parse_config_rebase(const char *key, const char *value,
36 int v = git_config_maybe_bool("pull.rebase", value);
42 else if (!strcmp(value, "preserve"))
43 return REBASE_PRESERVE;
46 die(_("Invalid value for %s: %s"), key, value);
48 error(_("Invalid value for %s: %s"), key, value);
50 return REBASE_INVALID;
54 * Callback for --rebase, which parses arg with parse_config_rebase().
56 static int parse_opt_rebase(const struct option *opt, const char *arg, int unset)
58 enum rebase_type *value = opt->value;
61 *value = parse_config_rebase("--rebase", arg, 0);
63 *value = unset ? REBASE_FALSE : REBASE_TRUE;
64 return *value == REBASE_INVALID ? -1 : 0;
67 static const char * const pull_usage[] = {
68 N_("git pull [options] [<repository> [<refspec>...]]"),
73 static int opt_verbosity;
74 static char *opt_progress;
76 /* Options passed to git-merge or git-rebase */
77 static enum rebase_type opt_rebase = -1;
78 static char *opt_diffstat;
80 static char *opt_squash;
81 static char *opt_commit;
82 static char *opt_edit;
84 static char *opt_verify_signatures;
85 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
86 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
87 static char *opt_gpg_sign;
89 /* Options passed to git-fetch */
91 static char *opt_append;
92 static char *opt_upload_pack;
94 static char *opt_tags;
95 static char *opt_prune;
96 static char *opt_recurse_submodules;
97 static int opt_dry_run;
98 static char *opt_keep;
99 static char *opt_depth;
100 static char *opt_unshallow;
101 static char *opt_update_shallow;
102 static char *opt_refmap;
104 static struct option pull_options[] = {
106 OPT__VERBOSITY(&opt_verbosity),
107 OPT_PASSTHRU(0, "progress", &opt_progress, NULL,
108 N_("force progress reporting"),
111 /* Options passed to git-merge or git-rebase */
112 OPT_GROUP(N_("Options related to merging")),
113 { OPTION_CALLBACK, 'r', "rebase", &opt_rebase,
114 N_("false|true|preserve"),
115 N_("incorporate changes by rebasing rather than merging"),
116 PARSE_OPT_OPTARG, parse_opt_rebase },
117 OPT_PASSTHRU('n', NULL, &opt_diffstat, NULL,
118 N_("do not show a diffstat at the end of the merge"),
119 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
120 OPT_PASSTHRU(0, "stat", &opt_diffstat, NULL,
121 N_("show a diffstat at the end of the merge"),
123 OPT_PASSTHRU(0, "summary", &opt_diffstat, NULL,
124 N_("(synonym to --stat)"),
125 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
126 OPT_PASSTHRU(0, "log", &opt_log, N_("n"),
127 N_("add (at most <n>) entries from shortlog to merge commit message"),
129 OPT_PASSTHRU(0, "squash", &opt_squash, NULL,
130 N_("create a single commit instead of doing a merge"),
132 OPT_PASSTHRU(0, "commit", &opt_commit, NULL,
133 N_("perform a commit if the merge succeeds (default)"),
135 OPT_PASSTHRU(0, "edit", &opt_edit, NULL,
136 N_("edit message before committing"),
138 OPT_PASSTHRU(0, "ff", &opt_ff, NULL,
139 N_("allow fast-forward"),
141 OPT_PASSTHRU(0, "ff-only", &opt_ff, NULL,
142 N_("abort if fast-forward is not possible"),
143 PARSE_OPT_NOARG | PARSE_OPT_NONEG),
144 OPT_PASSTHRU(0, "verify-signatures", &opt_verify_signatures, NULL,
145 N_("verify that the named commit has a valid GPG signature"),
147 OPT_PASSTHRU_ARGV('s', "strategy", &opt_strategies, N_("strategy"),
148 N_("merge strategy to use"),
150 OPT_PASSTHRU_ARGV('X', "strategy-option", &opt_strategy_opts,
152 N_("option for selected merge strategy"),
154 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
155 N_("GPG sign commit"),
158 /* Options passed to git-fetch */
159 OPT_GROUP(N_("Options related to fetching")),
160 OPT_PASSTHRU(0, "all", &opt_all, NULL,
161 N_("fetch from all remotes"),
163 OPT_PASSTHRU('a', "append", &opt_append, NULL,
164 N_("append to .git/FETCH_HEAD instead of overwriting"),
166 OPT_PASSTHRU(0, "upload-pack", &opt_upload_pack, N_("path"),
167 N_("path to upload pack on remote end"),
169 OPT__FORCE(&opt_force, N_("force overwrite of local branch")),
170 OPT_PASSTHRU('t', "tags", &opt_tags, NULL,
171 N_("fetch all tags and associated objects"),
173 OPT_PASSTHRU('p', "prune", &opt_prune, NULL,
174 N_("prune remote-tracking branches no longer on remote"),
176 OPT_PASSTHRU(0, "recurse-submodules", &opt_recurse_submodules,
178 N_("control recursive fetching of submodules"),
180 OPT_BOOL(0, "dry-run", &opt_dry_run,
182 OPT_PASSTHRU('k', "keep", &opt_keep, NULL,
183 N_("keep downloaded pack"),
185 OPT_PASSTHRU(0, "depth", &opt_depth, N_("depth"),
186 N_("deepen history of shallow clone"),
188 OPT_PASSTHRU(0, "unshallow", &opt_unshallow, NULL,
189 N_("convert to a complete repository"),
190 PARSE_OPT_NONEG | PARSE_OPT_NOARG),
191 OPT_PASSTHRU(0, "update-shallow", &opt_update_shallow, NULL,
192 N_("accept refs that update .git/shallow"),
194 OPT_PASSTHRU(0, "refmap", &opt_refmap, N_("refmap"),
195 N_("specify fetch refmap"),
202 * Pushes "-q" or "-v" switches into arr to match the opt_verbosity level.
204 static void argv_push_verbosity(struct argv_array *arr)
208 for (verbosity = opt_verbosity; verbosity > 0; verbosity--)
209 argv_array_push(arr, "-v");
211 for (verbosity = opt_verbosity; verbosity < 0; verbosity++)
212 argv_array_push(arr, "-q");
216 * Pushes "-f" switches into arr to match the opt_force level.
218 static void argv_push_force(struct argv_array *arr)
220 int force = opt_force;
222 argv_array_push(arr, "-f");
226 * Sets the GIT_REFLOG_ACTION environment variable to the concatenation of argv
228 static void set_reflog_message(int argc, const char **argv)
231 struct strbuf msg = STRBUF_INIT;
233 for (i = 0; i < argc; i++) {
235 strbuf_addch(&msg, ' ');
236 strbuf_addstr(&msg, argv[i]);
239 setenv("GIT_REFLOG_ACTION", msg.buf, 0);
241 strbuf_release(&msg);
245 * If pull.ff is unset, returns NULL. If pull.ff is "true", returns "--ff". If
246 * pull.ff is "false", returns "--no-ff". If pull.ff is "only", returns
247 * "--ff-only". Otherwise, if pull.ff is set to an invalid value, die with an
250 static const char *config_get_ff(void)
254 if (git_config_get_value("pull.ff", &value))
257 switch (git_config_maybe_bool("pull.ff", value)) {
264 if (!strcmp(value, "only"))
267 die(_("Invalid value for pull.ff: %s"), value);
271 * Returns the default configured value for --rebase. It first looks for the
272 * value of "branch.$curr_branch.rebase", where $curr_branch is the current
273 * branch, and if HEAD is detached or the configuration key does not exist,
274 * looks for the value of "pull.rebase". If both configuration keys do not
275 * exist, returns REBASE_FALSE.
277 static enum rebase_type config_get_rebase(void)
279 struct branch *curr_branch = branch_get("HEAD");
283 char *key = xstrfmt("branch.%s.rebase", curr_branch->name);
285 if (!git_config_get_value(key, &value)) {
286 enum rebase_type ret = parse_config_rebase(key, value, 1);
294 if (!git_config_get_value("pull.rebase", &value))
295 return parse_config_rebase("pull.rebase", value, 1);
301 * Returns 1 if there are unstaged changes, 0 otherwise.
303 static int has_unstaged_changes(const char *prefix)
305 struct rev_info rev_info;
308 init_revisions(&rev_info, prefix);
309 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
310 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
311 diff_setup_done(&rev_info.diffopt);
312 result = run_diff_files(&rev_info, 0);
313 return diff_result_code(&rev_info.diffopt, result);
317 * Returns 1 if there are uncommitted changes, 0 otherwise.
319 static int has_uncommitted_changes(const char *prefix)
321 struct rev_info rev_info;
324 if (is_cache_unborn())
327 init_revisions(&rev_info, prefix);
328 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
329 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
330 add_head_to_pending(&rev_info);
331 diff_setup_done(&rev_info.diffopt);
332 result = run_diff_index(&rev_info, 1);
333 return diff_result_code(&rev_info.diffopt, result);
337 * If the work tree has unstaged or uncommitted changes, dies with the
338 * appropriate message.
340 static void die_on_unclean_work_tree(const char *prefix)
342 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
345 hold_locked_index(lock_file, 0);
346 refresh_cache(REFRESH_QUIET);
347 update_index_if_able(&the_index, lock_file);
348 rollback_lock_file(lock_file);
350 if (has_unstaged_changes(prefix)) {
351 error(_("Cannot pull with rebase: You have unstaged changes."));
355 if (has_uncommitted_changes(prefix)) {
357 error(_("Additionally, your index contains uncommitted changes."));
359 error(_("Cannot pull with rebase: Your index contains uncommitted changes."));
368 * Appends merge candidates from FETCH_HEAD that are not marked not-for-merge
371 static void get_merge_heads(struct sha1_array *merge_heads)
373 const char *filename = git_path("FETCH_HEAD");
375 struct strbuf sb = STRBUF_INIT;
376 unsigned char sha1[GIT_SHA1_RAWSZ];
378 if (!(fp = fopen(filename, "r")))
379 die_errno(_("could not open '%s' for reading"), filename);
380 while (strbuf_getline(&sb, fp, '\n') != EOF) {
381 if (get_sha1_hex(sb.buf, sha1))
382 continue; /* invalid line: does not start with SHA1 */
383 if (starts_with(sb.buf + GIT_SHA1_HEXSZ, "\tnot-for-merge\t"))
384 continue; /* ref is not-for-merge */
385 sha1_array_append(merge_heads, sha1);
392 * Used by die_no_merge_candidates() as a for_each_remote() callback to
393 * retrieve the name of the remote if the repository only has one remote.
395 static int get_only_remote(struct remote *remote, void *cb_data)
397 const char **remote_name = cb_data;
402 *remote_name = remote->name;
407 * Dies with the appropriate reason for why there are no merge candidates:
409 * 1. We fetched from a specific remote, and a refspec was given, but it ended
410 * up not fetching anything. This is usually because the user provided a
411 * wildcard refspec which had no matches on the remote end.
413 * 2. We fetched from a non-default remote, but didn't specify a branch to
414 * merge. We can't use the configured one because it applies to the default
415 * remote, thus the user must specify the branches to merge.
417 * 3. We fetched from the branch's or repo's default remote, but:
419 * a. We are not on a branch, so there will never be a configured branch to
422 * b. We are on a branch, but there is no configured branch to merge with.
424 * 4. We fetched from the branch's or repo's default remote, but the configured
425 * branch to merge didn't get fetched. (Either it doesn't exist, or wasn't
426 * part of the configured fetch refspec.)
428 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
430 struct branch *curr_branch = branch_get("HEAD");
431 const char *remote = curr_branch ? curr_branch->remote_name : NULL;
435 fprintf_ln(stderr, _("There is no candidate for rebasing against among the refs that you just fetched."));
437 fprintf_ln(stderr, _("There are no candidates for merging among the refs that you just fetched."));
438 fprintf_ln(stderr, _("Generally this means that you provided a wildcard refspec which had no\n"
439 "matches on the remote end."));
440 } else if (repo && curr_branch && (!remote || strcmp(repo, remote))) {
441 fprintf_ln(stderr, _("You asked to pull from the remote '%s', but did not specify\n"
442 "a branch. Because this is not the default configured remote\n"
443 "for your current branch, you must specify a branch on the command line."),
445 } else if (!curr_branch) {
446 fprintf_ln(stderr, _("You are not currently on a branch."));
448 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
450 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
451 fprintf_ln(stderr, _("See git-pull(1) for details."));
452 fprintf(stderr, "\n");
453 fprintf_ln(stderr, " git pull <remote> <branch>");
454 fprintf(stderr, "\n");
455 } else if (!curr_branch->merge_nr) {
456 const char *remote_name = NULL;
458 if (for_each_remote(get_only_remote, &remote_name) || !remote_name)
459 remote_name = "<remote>";
461 fprintf_ln(stderr, _("There is no tracking information for the current branch."));
463 fprintf_ln(stderr, _("Please specify which branch you want to rebase against."));
465 fprintf_ln(stderr, _("Please specify which branch you want to merge with."));
466 fprintf_ln(stderr, _("See git-pull(1) for details."));
467 fprintf(stderr, "\n");
468 fprintf_ln(stderr, " git pull <remote> <branch>");
469 fprintf(stderr, "\n");
470 fprintf_ln(stderr, _("If you wish to set tracking information for this branch you can do so with:\n"
472 " git branch --set-upstream-to=%s/<branch> %s\n"),
473 remote_name, curr_branch->name);
475 fprintf_ln(stderr, _("Your configuration specifies to merge with the ref '%s'\n"
476 "from the remote, but no such ref was fetched."),
477 *curr_branch->merge_name);
482 * Parses argv into [<repo> [<refspecs>...]], returning their values in `repo`
483 * as a string and `refspecs` as a null-terminated array of strings. If `repo`
484 * is not provided in argv, it is set to NULL.
486 static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
487 const char ***refspecs)
498 * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the
499 * repository and refspecs to fetch, or NULL if they are not provided.
501 static int run_fetch(const char *repo, const char **refspecs)
503 struct argv_array args = ARGV_ARRAY_INIT;
506 argv_array_pushl(&args, "fetch", "--update-head-ok", NULL);
509 argv_push_verbosity(&args);
511 argv_array_push(&args, opt_progress);
513 /* Options passed to git-fetch */
515 argv_array_push(&args, opt_all);
517 argv_array_push(&args, opt_append);
519 argv_array_push(&args, opt_upload_pack);
520 argv_push_force(&args);
522 argv_array_push(&args, opt_tags);
524 argv_array_push(&args, opt_prune);
525 if (opt_recurse_submodules)
526 argv_array_push(&args, opt_recurse_submodules);
528 argv_array_push(&args, "--dry-run");
530 argv_array_push(&args, opt_keep);
532 argv_array_push(&args, opt_depth);
534 argv_array_push(&args, opt_unshallow);
535 if (opt_update_shallow)
536 argv_array_push(&args, opt_update_shallow);
538 argv_array_push(&args, opt_refmap);
541 argv_array_push(&args, repo);
542 argv_array_pushv(&args, refspecs);
543 } else if (*refspecs)
544 die("BUG: refspecs without repo?");
545 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
546 argv_array_clear(&args);
551 * "Pulls into void" by branching off merge_head.
553 static int pull_into_void(const unsigned char *merge_head,
554 const unsigned char *curr_head)
557 * Two-way merge: we treat the index as based on an empty tree,
558 * and try to fast-forward to HEAD. This ensures we will not lose
559 * index/worktree changes that the user already made on the unborn
562 if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
565 if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
572 * Runs git-merge, returning its exit status.
574 static int run_merge(void)
577 struct argv_array args = ARGV_ARRAY_INIT;
579 argv_array_pushl(&args, "merge", NULL);
582 argv_push_verbosity(&args);
584 argv_array_push(&args, opt_progress);
586 /* Options passed to git-merge */
588 argv_array_push(&args, opt_diffstat);
590 argv_array_push(&args, opt_log);
592 argv_array_push(&args, opt_squash);
594 argv_array_push(&args, opt_commit);
596 argv_array_push(&args, opt_edit);
598 argv_array_push(&args, opt_ff);
599 if (opt_verify_signatures)
600 argv_array_push(&args, opt_verify_signatures);
601 argv_array_pushv(&args, opt_strategies.argv);
602 argv_array_pushv(&args, opt_strategy_opts.argv);
604 argv_array_push(&args, opt_gpg_sign);
606 argv_array_push(&args, "FETCH_HEAD");
607 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
608 argv_array_clear(&args);
613 * Returns remote's upstream branch for the current branch. If remote is NULL,
614 * the current branch's configured default remote is used. Returns NULL if
615 * `remote` does not name a valid remote, HEAD does not point to a branch,
616 * remote is not the branch's configured remote or the branch does not have any
617 * configured upstream branch.
619 static const char *get_upstream_branch(const char *remote)
622 struct branch *curr_branch;
623 const char *curr_branch_remote;
625 rm = remote_get(remote);
629 curr_branch = branch_get("HEAD");
633 curr_branch_remote = remote_for_branch(curr_branch, NULL);
634 assert(curr_branch_remote);
636 if (strcmp(curr_branch_remote, rm->name))
639 return branch_get_upstream(curr_branch, NULL);
643 * Derives the remote tracking branch from the remote and refspec.
645 * FIXME: The current implementation assumes the default mapping of
646 * refs/heads/<branch_name> to refs/remotes/<remote_name>/<branch_name>.
648 static const char *get_tracking_branch(const char *remote, const char *refspec)
650 struct refspec *spec;
651 const char *spec_src;
652 const char *merge_branch;
654 spec = parse_fetch_refspec(1, &refspec);
655 spec_src = spec->src;
656 if (!*spec_src || !strcmp(spec_src, "HEAD"))
658 else if (skip_prefix(spec_src, "heads/", &spec_src))
660 else if (skip_prefix(spec_src, "refs/heads/", &spec_src))
662 else if (starts_with(spec_src, "refs/") ||
663 starts_with(spec_src, "tags/") ||
664 starts_with(spec_src, "remotes/"))
668 if (!strcmp(remote, "."))
669 merge_branch = mkpath("refs/heads/%s", spec_src);
671 merge_branch = mkpath("refs/remotes/%s/%s", remote, spec_src);
675 free_refspec(1, spec);
680 * Given the repo and refspecs, sets fork_point to the point at which the
681 * current branch forked from its remote tracking branch. Returns 0 on success,
684 static int get_rebase_fork_point(unsigned char *fork_point, const char *repo,
688 struct branch *curr_branch;
689 const char *remote_branch;
690 struct child_process cp = CHILD_PROCESS_INIT;
691 struct strbuf sb = STRBUF_INIT;
693 curr_branch = branch_get("HEAD");
698 remote_branch = get_tracking_branch(repo, refspec);
700 remote_branch = get_upstream_branch(repo);
705 argv_array_pushl(&cp.args, "merge-base", "--fork-point",
706 remote_branch, curr_branch->name, NULL);
711 ret = capture_command(&cp, &sb, GIT_SHA1_HEXSZ);
715 ret = get_sha1_hex(sb.buf, fork_point);
725 * Sets merge_base to the octopus merge base of curr_head, merge_head and
726 * fork_point. Returns 0 if a merge base is found, 1 otherwise.
728 static int get_octopus_merge_base(unsigned char *merge_base,
729 const unsigned char *curr_head,
730 const unsigned char *merge_head,
731 const unsigned char *fork_point)
733 struct commit_list *revs = NULL, *result;
735 commit_list_insert(lookup_commit_reference(curr_head), &revs);
736 commit_list_insert(lookup_commit_reference(merge_head), &revs);
737 if (!is_null_sha1(fork_point))
738 commit_list_insert(lookup_commit_reference(fork_point), &revs);
740 result = reduce_heads(get_octopus_merge_bases(revs));
741 free_commit_list(revs);
745 hashcpy(merge_base, result->item->object.sha1);
750 * Given the current HEAD SHA1, the merge head returned from git-fetch and the
751 * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
752 * appropriate arguments and returns its exit status.
754 static int run_rebase(const unsigned char *curr_head,
755 const unsigned char *merge_head,
756 const unsigned char *fork_point)
759 unsigned char oct_merge_base[GIT_SHA1_RAWSZ];
760 struct argv_array args = ARGV_ARRAY_INIT;
762 if (!get_octopus_merge_base(oct_merge_base, curr_head, merge_head, fork_point))
763 if (!is_null_sha1(fork_point) && !hashcmp(oct_merge_base, fork_point))
766 argv_array_push(&args, "rebase");
769 argv_push_verbosity(&args);
771 /* Options passed to git-rebase */
772 if (opt_rebase == REBASE_PRESERVE)
773 argv_array_push(&args, "--preserve-merges");
775 argv_array_push(&args, opt_diffstat);
776 argv_array_pushv(&args, opt_strategies.argv);
777 argv_array_pushv(&args, opt_strategy_opts.argv);
779 argv_array_push(&args, opt_gpg_sign);
781 argv_array_push(&args, "--onto");
782 argv_array_push(&args, sha1_to_hex(merge_head));
784 if (fork_point && !is_null_sha1(fork_point))
785 argv_array_push(&args, sha1_to_hex(fork_point));
787 argv_array_push(&args, sha1_to_hex(merge_head));
789 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
790 argv_array_clear(&args);
794 int cmd_pull(int argc, const char **argv, const char *prefix)
796 const char *repo, **refspecs;
797 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
798 unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
799 unsigned char rebase_fork_point[GIT_SHA1_RAWSZ];
801 if (!getenv("GIT_REFLOG_ACTION"))
802 set_reflog_message(argc, argv);
804 argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
806 parse_repo_refspecs(argc, argv, &repo, &refspecs);
809 opt_ff = xstrdup_or_null(config_get_ff());
812 opt_rebase = config_get_rebase();
814 git_config(git_default_config, NULL);
816 if (read_cache_unmerged())
817 die_resolve_conflict("Pull");
819 if (file_exists(git_path("MERGE_HEAD")))
820 die_conclude_merge();
822 if (get_sha1("HEAD", orig_head))
828 if (is_null_sha1(orig_head) && !is_cache_unborn())
829 die(_("Updating an unborn branch with changes added to the index."));
831 git_config_get_bool("rebase.autostash", &autostash);
833 die_on_unclean_work_tree(prefix);
835 if (get_rebase_fork_point(rebase_fork_point, repo, *refspecs))
836 hashclr(rebase_fork_point);
839 if (run_fetch(repo, refspecs))
845 if (get_sha1("HEAD", curr_head))
848 if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
849 hashcmp(orig_head, curr_head)) {
851 * The fetch involved updating the current branch.
853 * The working tree and the index file are still based on
854 * orig_head commit, but we are merging into curr_head.
855 * Update the working tree to match curr_head.
858 warning(_("fetch updated the current branch head.\n"
859 "fast-forwarding your working tree from\n"
860 "commit %s."), sha1_to_hex(orig_head));
862 if (checkout_fast_forward(orig_head, curr_head, 0))
863 die(_("Cannot fast-forward your working tree.\n"
864 "After making sure that you saved anything precious from\n"
867 "$ git reset --hard\n"
868 "to recover."), sha1_to_hex(orig_head));
871 get_merge_heads(&merge_heads);
874 die_no_merge_candidates(repo, refspecs);
876 if (is_null_sha1(orig_head)) {
877 if (merge_heads.nr > 1)
878 die(_("Cannot merge multiple branches into empty head."));
879 return pull_into_void(*merge_heads.sha1, curr_head);
880 } else if (opt_rebase) {
881 if (merge_heads.nr > 1)
882 die(_("Cannot rebase onto multiple branches."));
883 return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);