Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 5d88639

Browse files
j6tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f949209 commit 5d88639

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

builtin/checkout.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,10 @@ static void suggest_reattach(struct commit *commit, struct rev_info *revs)
671671
* HEAD. If it is not reachable from any ref, this is the last chance
672672
* for the user to do so without resorting to reflog.
673673
*/
674-
static void orphaned_commit_warning(struct commit *commit)
674+
static void orphaned_commit_warning(struct commit *old, struct commit *new)
675675
{
676676
struct rev_info revs;
677-
struct object *object = &commit->object;
677+
struct object *object = &old->object;
678678
struct object_array refs;
679679

680680
init_revisions(&revs, NULL);
@@ -684,16 +684,17 @@ static void orphaned_commit_warning(struct commit *commit)
684684
add_pending_object(&revs, object, sha1_to_hex(object->sha1));
685685

686686
for_each_ref(add_pending_uninteresting_ref, &revs);
687+
add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING);
687688

688689
refs = revs.pending;
689690
revs.leak_pending = 1;
690691

691692
if (prepare_revision_walk(&revs))
692693
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);
695696
else
696-
describe_detached_head(_("Previous HEAD position was"), commit);
697+
describe_detached_head(_("Previous HEAD position was"), old);
697698

698699
clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
699700
free(refs.objects);
@@ -730,7 +731,7 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
730731
}
731732

732733
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);
734735

735736
update_refs_for_switch(opts, &old, new);
736737

t/t2020-checkout-detach.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test_expect_success 'checkout warns orphaning 1 of 2 commits' '
126126
'
127127

128128
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"
130130
'
131131

132132
test_expect_success 'checkout does not warn leaving ref tip' '

0 commit comments

Comments
 (0)