1#!/bin/sh
2#
3# Copyright (c) 2007 Junio C Hamano
4#
56
test_description='Test prune and reflog expiration'
7. ./test-lib.sh
89
check_have () {
10gaah= &&
11for N in "$@"
12do
13eval "o=\$$N" && git cat-file -t $o || {
14echo Gaah $N
15gaah=$N
16break
17}
18done &&
19test -z "$gaah"
20}
2122
check_fsck () {
23output=$(git fsck --full)
24case "$1" in
25'')
26test -z "$output" ;;
27*)
28echo "$output" | grep "$1" ;;
29esac
30}
3132
corrupt () {
33aa=${1%??????????????????????????????????????} zz=${1#??}
34mv .git/objects/$aa/$zz .git/$aa$zz
35}
3637
recover () {
38aa=${1%??????????????????????????????????????} zz=${1#??}
39mkdir -p .git/objects/$aa
40mv .git/$aa$zz .git/objects/$aa/$zz
41}
4243
check_dont_have () {
44gaah= &&
45for N in "$@"
46do
47eval "o=\$$N"
48git cat-file -t $o && {
49echo Gaah $N
50gaah=$N
51break
52}
53done
54test -z "$gaah"
55}
5657
test_expect_success setup '
58mkdir -p A/B &&
59echo rat >C &&
60echo ox >A/D &&
61echo tiger >A/B/E &&
62git add . &&
6364
test_tick && git commit -m rabbit &&
65H=`git rev-parse --verify HEAD` &&
66A=`git rev-parse --verify HEAD:A` &&
67B=`git rev-parse --verify HEAD:A/B` &&
68C=`git rev-parse --verify HEAD:C` &&
69D=`git rev-parse --verify HEAD:A/D` &&
70E=`git rev-parse --verify HEAD:A/B/E` &&
71check_fsck &&
7273
test_chmod +x C &&
74git add C &&
75test_tick && git commit -m dragon &&
76L=`git rev-parse --verify HEAD` &&
77check_fsck &&
7879
rm -f C A/B/E &&
80echo snake >F &&
81echo horse >A/G &&
82git add F A/G &&
83test_tick && git commit -a -m sheep &&
84F=`git rev-parse --verify HEAD:F` &&
85G=`git rev-parse --verify HEAD:A/G` &&
86I=`git rev-parse --verify HEAD:A` &&
87J=`git rev-parse --verify HEAD` &&
88check_fsck &&
8990
rm -f A/G &&
91test_tick && git commit -a -m monkey &&
92K=`git rev-parse --verify HEAD` &&
93check_fsck &&
9495
check_have A B C D E F G H I J K L &&
9697
git prune &&
9899
check_have A B C D E F G H I J K L &&
100101
check_fsck &&
102103
loglen=$(wc -l <.git/logs/refs/heads/master) &&
104test $loglen = 4
105'
106107
test_expect_success rewind '
108test_tick && git reset --hard HEAD~2 &&
109test -f C &&
110test -f A/B/E &&
111! test -f F &&
112! test -f A/G &&
113114
check_have A B C D E F G H I J K L &&
115116
git prune &&
117118
check_have A B C D E F G H I J K L &&
119120
loglen=$(wc -l <.git/logs/refs/heads/master) &&
121test $loglen = 5
122'
123124
test_expect_success 'corrupt and check' '
125126
corrupt $F &&
127check_fsck "missing blob $F"
128129
'
130131
test_expect_success 'reflog expire --dry-run should not touch reflog' '
132133
git reflog expire --dry-run \
134--expire=$(($test_tick - 10000)) \
135--expire-unreachable=$(($test_tick - 10000)) \
136--stale-fix \
137--all &&
138139
loglen=$(wc -l <.git/logs/refs/heads/master) &&
140test $loglen = 5 &&
141142
check_fsck "missing blob $F"
143'
144145
test_expect_success 'reflog expire' '
146147
git reflog expire --verbose \
148--expire=$(($test_tick - 10000)) \
149--expire-unreachable=$(($test_tick - 10000)) \
150--stale-fix \
151--all &&
152153
loglen=$(wc -l <.git/logs/refs/heads/master) &&
154test $loglen = 2 &&
155156
check_fsck "dangling commit $K"
157'
158159
test_expect_success 'prune and fsck' '
160161
git prune &&
162check_fsck &&
163164
check_have A B C D E H L &&
165check_dont_have F G I J K
166167
'
168169
test_expect_success 'recover and check' '
170171
recover $F &&
172check_fsck "dangling blob $F"
173174
'
175176
test_expect_success 'delete' '
177echo 1 > C &&
178test_tick &&
179git commit -m rat C &&
180181
echo 2 > C &&
182test_tick &&
183git commit -m ox C &&
184185
echo 3 > C &&
186test_tick &&
187git commit -m tiger C &&
188189
HEAD_entry_count=$(git reflog | wc -l) &&
190master_entry_count=$(git reflog show master | wc -l) &&
191192
test $HEAD_entry_count = 5 &&
193test $master_entry_count = 5 &&
194195
196
git reflog delete master@{1} &&
197git reflog show master > output &&
198test $(($master_entry_count - 1)) = $(wc -l < output) &&
199test $HEAD_entry_count = $(git reflog | wc -l) &&
200! grep ox < output &&
201202
master_entry_count=$(wc -l < output) &&
203204
git reflog delete HEAD@{1} &&
205test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
206test $master_entry_count = $(git reflog show master | wc -l) &&
207208
HEAD_entry_count=$(git reflog | wc -l) &&
209210
git reflog delete master@{07.04.2005.15:15:00.-0700} &&
211git reflog show master > output &&
212test $(($master_entry_count - 1)) = $(wc -l < output) &&
213! grep dragon < output
214215
'
216217
test_expect_success 'rewind2' '
218219
test_tick && git reset --hard HEAD~2 &&
220loglen=$(wc -l <.git/logs/refs/heads/master) &&
221test $loglen = 4
222223
'
224225
test_expect_success '--expire=never' '
226227
git reflog expire --verbose \
228--expire=never \
229--expire-unreachable=never \
230--all &&
231loglen=$(wc -l <.git/logs/refs/heads/master) &&
232test $loglen = 4
233234
'
235236
test_expect_success 'gc.reflogexpire=never' '
237238
git config gc.reflogexpire never &&
239git config gc.reflogexpireunreachable never &&
240git reflog expire --verbose --all &&
241loglen=$(wc -l <.git/logs/refs/heads/master) &&
242test $loglen = 4
243'
244245
test_expect_success 'gc.reflogexpire=false' '
246247
git config gc.reflogexpire false &&
248git config gc.reflogexpireunreachable false &&
249git reflog expire --verbose --all &&
250loglen=$(wc -l <.git/logs/refs/heads/master) &&
251test $loglen = 4 &&
252253
git config --unset gc.reflogexpire &&
254git config --unset gc.reflogexpireunreachable
255256
'
257258
test_done