Skip to content

Commit 421ec4f

Browse files
committed
Merge branch 'mk/reachable-protect-detached-head' into maint
Reachability check (used in "git prune" and friends) did not add a detached HEAD as a starting point to traverse objects still in use. * mk/reachable-protect-detached-head: reachable.c: add HEAD to reachability starting commits
2 parents 5b830a8 + c40fdd0 commit 421ec4f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

reachable.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
229229
/* Add all external refs */
230230
for_each_ref(add_one_ref, revs);
231231

232+
/* detached HEAD is not included in the list above */
233+
head_ref(add_one_ref, revs);
234+
232235
/* Add all reflog info */
233236
if (mark_reflog)
234237
for_each_reflog(add_one_reflog, revs);

t/t5304-prune.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ test_expect_success 'prune: prune unreachable heads' '
104104
105105
'
106106

107+
test_expect_success 'prune: do not prune detached HEAD with no reflog' '
108+
109+
git checkout --detach --quiet &&
110+
git commit --allow-empty -m "detached commit" &&
111+
# verify that there is no reflogs
112+
# (should be removed and disabled by previous test)
113+
test ! -e .git/logs &&
114+
git prune -n >prune_actual &&
115+
: >prune_expected &&
116+
test_cmp prune_actual prune_expected
117+
118+
'
119+
120+
test_expect_success 'prune: prune former HEAD after checking out branch' '
121+
122+
head_sha1=$(git rev-parse HEAD) &&
123+
git checkout --quiet master &&
124+
git prune -v >prune_actual &&
125+
grep "$head_sha1" prune_actual
126+
127+
'
128+
107129
test_expect_success 'prune: do not prune heads listed as an argument' '
108130
109131
: > file2 &&

0 commit comments

Comments
 (0)