Skip to content

Commit a912913

Browse files
committed
Merge branch 'js/checkout-detach-count' into maint
When checking out another commit from an already detached state, we used to report all commits that are not reachable from any of the refs as lossage, but some of them might be reachable from the new HEAD, and there is no need to warn about them. By Johannes Sixt * js/checkout-detach-count: checkout (detached): truncate list of orphaned commits at the new HEAD t2020-checkout-detach: check for the number of orphaned commits
2 parents c8cf3ec + 5d88639 commit a912913

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ check_not_detached () {
1111
git symbolic-ref -q HEAD >/dev/null
1212
}
1313

14-
ORPHAN_WARNING='you are leaving .* commit.*behind'
1514
PREV_HEAD_DESC='Previous HEAD position was'
1615
check_orphan_warning() {
17-
test_i18ngrep "$ORPHAN_WARNING" "$1" &&
16+
test_i18ngrep "you are leaving $2 behind" "$1" &&
1817
test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
1918
}
2019
check_no_orphan_warning() {
21-
test_i18ngrep ! "$ORPHAN_WARNING" "$1" &&
20+
test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
2221
test_i18ngrep "$PREV_HEAD_DESC" "$1"
2322
}
2423

@@ -110,12 +109,24 @@ test_expect_success 'checkout warns on orphan commits' '
110109
git checkout --detach two &&
111110
echo content >orphan &&
112111
git add orphan &&
113-
git commit -a -m orphan &&
112+
git commit -a -m orphan1 &&
113+
echo new content >orphan &&
114+
git commit -a -m orphan2 &&
115+
orphan2=$(git rev-parse HEAD) &&
114116
git checkout master 2>stderr
115117
'
116118

117119
test_expect_success 'checkout warns on orphan commits: output' '
118-
check_orphan_warning stderr
120+
check_orphan_warning stderr "2 commits"
121+
'
122+
123+
test_expect_success 'checkout warns orphaning 1 of 2 commits' '
124+
git checkout "$orphan2" &&
125+
git checkout HEAD^ 2>stderr
126+
'
127+
128+
test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
129+
check_orphan_warning stderr "1 commit"
119130
'
120131

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

0 commit comments

Comments
 (0)