Skip to content

Commit c40fdd0

Browse files
max630gitster
authored andcommitted
reachable.c: add HEAD to reachability starting commits
HEAD is not explicitly used as a starting commit for calculating reachability, so if it's detached and reflogs are disabled it may be pruned. Add tests which demonstrate it. Test 'prune: prune former HEAD after checking out branch' also reverts changes to repository. Signed-off-by: Max Kirillov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96db324 commit c40fdd0

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)