checkout (detached): truncate list of orphaned commits at the new HEAD

When git checkout switches from a detached HEAD to any other commit, then all orphaned commits were listed in a warning: Warning: you are leaving 2 commits behind...: a5e5396 another fixup 6aa1af6 fixup foo But if the new commit is actually one from this list (6aa1af6 in this example), then the list in the warning can be truncated at the new HEAD, because history beginning at HEAD is not "left behind". This makes it so. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Sixt committed May 4, 2012 at 20:14 UTC 5d8863954f077d2c262d5cc4fc669947ff23d6d5
2 files changed +8 -7
builtin/checkout.c
+7 -6
@@ -671,10 +671,10 @@ static void suggest_reattach(struct commit *commit, struct rev_info *revs)
671 * HEAD. If it is not reachable from any ref, this is the last chance
672 * for the user to do so without resorting to reflog.
673 */
674 -static void orphaned_commit_warning(struct commit *commit)
674 +static void orphaned_commit_warning(struct commit *old, struct commit *new)
675 {
676 struct rev_info revs;
677 - struct object *object = &commit->object;
677 + struct object *object = &old->object;
678 struct object_array refs;
679
680 init_revisions(&revs, NULL);
@@ -684,16 +684,17 @@ static void orphaned_commit_warning(struct commit *commit)
684 add_pending_object(&revs, object, sha1_to_hex(object->sha1));
685
686 for_each_ref(add_pending_uninteresting_ref, &revs);
687 + add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING);
688
689 refs = revs.pending;
690 revs.leak_pending = 1;
691
692 if (prepare_revision_walk(&revs))
693 die(_("internal error in revision walk"));
693 - if (!(commit->object.flags & UNINTERESTING))
694 - suggest_reattach(commit, &revs);
694 + if (!(old->object.flags & UNINTERESTING))
695 + suggest_reattach(old, &revs);
696 else
696 - describe_detached_head(_("Previous HEAD position was"), commit);
697 + describe_detached_head(_("Previous HEAD position was"), old);
698
699 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
700 free(refs.objects);
@@ -730,7 +731,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
731 }
732
733 if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
733 - orphaned_commit_warning(old.commit);
734 + orphaned_commit_warning(old.commit, new->commit);
735
736 update_refs_for_switch(opts, &old, new);
737
t/t2020-checkout-detach.sh
+1 -1
@@ -126,7 +126,7 @@ test_expect_success 'checkout warns orphaning 1 of 2 commits' '
126 '
127
128 test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
129 - check_orphan_warning stderr "2 commits"
129 + check_orphan_warning stderr "1 commit"
130 '
131
132 test_expect_success 'checkout does not warn leaving ref tip' '