pull: fast-forward working tree if head is updated

Since b10ac50 (Fix pulling into the same branch., 2005-08-25), git-pull, upon detecting that git-fetch updated the current head, will fast-forward the working tree to the updated head commit. Re-implement this behavior. Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 18, 2015 at 18:54 UTC fe911b8ca0b466697b38880512ea2b28f2699dbd
1 file changed +30
builtin/pull.c
+30
@@ -412,6 +412,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
412 {
413 const char *repo, **refspecs;
414 struct sha1_array merge_heads = SHA1_ARRAY_INIT;
415 + unsigned char orig_head[GIT_SHA1_RAWSZ], curr_head[GIT_SHA1_RAWSZ];
416
417 if (!getenv("_GIT_USE_BUILTIN_PULL")) {
418 const char *path = mkpath("%s/git-pull", git_exec_path());
@@ -435,12 +436,41 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
436 if (file_exists(git_path("MERGE_HEAD")))
437 die_conclude_merge();
438
439 + if (get_sha1("HEAD", orig_head))
440 + hashclr(orig_head);
441 +
442 if (run_fetch(repo, refspecs))
443 return 1;
444
445 if (opt_dry_run)
446 return 0;
447
448 + if (get_sha1("HEAD", curr_head))
449 + hashclr(curr_head);
450 +
451 + if (!is_null_sha1(orig_head) && !is_null_sha1(curr_head) &&
452 + hashcmp(orig_head, curr_head)) {
453 + /*
454 + * The fetch involved updating the current branch.
455 + *
456 + * The working tree and the index file are still based on
457 + * orig_head commit, but we are merging into curr_head.
458 + * Update the working tree to match curr_head.
459 + */
460 +
461 + warning(_("fetch updated the current branch head.\n"
462 + "fast-forwarding your working tree from\n"
463 + "commit %s."), sha1_to_hex(orig_head));
464 +
465 + if (checkout_fast_forward(orig_head, curr_head, 0))
466 + die(_("Cannot fast-forward your working tree.\n"
467 + "After making sure that you saved anything precious from\n"
468 + "$ git diff %s\n"
469 + "output, run\n"
470 + "$ git reset --hard\n"
471 + "to recover."), sha1_to_hex(orig_head));
472 + }
473 +
474 get_merge_heads(&merge_heads);
475
476 if (!merge_heads.nr)