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

Commit dc7a4c3

Browse files
committed
Merge branch 'js/checkout-detach-count'
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 499e7b3 + 5d88639 commit dc7a4c3

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
@@ -672,10 +672,10 @@ static void suggest_reattach(struct commit *commit, struct rev_info *revs)
672672
* HEAD. If it is not reachable from any ref, this is the last chance
673673
* for the user to do so without resorting to reflog.
674674
*/
675-
static void orphaned_commit_warning(struct commit *commit)
675+
static void orphaned_commit_warning(struct commit *old, struct commit *new)
676676
{
677677
struct rev_info revs;
678-
struct object *object = &commit->object;
678+
struct object *object = &old->object;
679679
struct object_array refs;
680680

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

687687
for_each_ref(add_pending_uninteresting_ref, &revs);
688+
add_pending_sha1(&revs, "HEAD", new->object.sha1, UNINTERESTING);
688689

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

692693
if (prepare_revision_walk(&revs))
693694
die(_("internal error in revision walk"));
694-
if (!(commit->object.flags & UNINTERESTING))
695-
suggest_reattach(commit, &revs);
695+
if (!(old->object.flags & UNINTERESTING))
696+
suggest_reattach(old, &revs);
696697
else
697-
describe_detached_head(_("Previous HEAD position was"), commit);
698+
describe_detached_head(_("Previous HEAD position was"), old);
698699

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

733734
if (!opts->quiet && !old.path && old.commit && new->commit != old.commit)
734-
orphaned_commit_warning(old.commit);
735+
orphaned_commit_warning(old.commit, new->commit);
735736

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

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)