1#!/bin/sh
2#
3# Copyright (c) 2007 Junio C Hamano
4#
5
6test_description='Test prune and reflog expiration'
7. ./test-lib.sh
8
9check_have () {
10 gaah= &&
11 for N in "$@"
12 do
13 eval "o=\$$N" && git cat-file -t $o || {
14 echo Gaah $N
15 gaah=$N
16 break
17 }
18 done &&
19 test -z "$gaah"
20}
21
22check_fsck () {
23 output=$(git fsck --full)
24 case "$1" in
25 '')
26 test -z "$output" ;;
27 *)
28 echo "$output" | grep "$1" ;;
29 esac
30}
31
32corrupt () {
33 aa=${1%??????????????????????????????????????} zz=${1#??}
34 mv .git/objects/$aa/$zz .git/$aa$zz
35}
36
37recover () {
38 aa=${1%??????????????????????????????????????} zz=${1#??}
39 mkdir -p .git/objects/$aa
40 mv .git/$aa$zz .git/objects/$aa/$zz
41}
42
43check_dont_have () {
44 gaah= &&
45 for N in "$@"
46 do
47 eval "o=\$$N"
48 git cat-file -t $o && {
49 echo Gaah $N
50 gaah=$N
51 break
52 }
53 done
54 test -z "$gaah"
55}
56
57test_expect_success setup '
58 mkdir -p A/B &&
59 echo rat >C &&
60 echo ox >A/D &&
61 echo tiger >A/B/E &&
62 git add . &&
63
64 test_tick && git commit -m rabbit &&
65 H=`git rev-parse --verify HEAD` &&
66 A=`git rev-parse --verify HEAD:A` &&
67 B=`git rev-parse --verify HEAD:A/B` &&
68 C=`git rev-parse --verify HEAD:C` &&
69 D=`git rev-parse --verify HEAD:A/D` &&
70 E=`git rev-parse --verify HEAD:A/B/E` &&
71 check_fsck &&
72
73 chmod +x C &&
74 ( test "`git config --bool core.filemode`" != false ||
75 echo executable >>C ) &&
76 git add C &&
77 test_tick && git commit -m dragon &&
78 L=`git rev-parse --verify HEAD` &&
79 check_fsck &&
80
81 rm -f C A/B/E &&
82 echo snake >F &&
83 echo horse >A/G &&
84 git add F A/G &&
85 test_tick && git commit -a -m sheep &&
86 F=`git rev-parse --verify HEAD:F` &&
87 G=`git rev-parse --verify HEAD:A/G` &&
88 I=`git rev-parse --verify HEAD:A` &&
89 J=`git rev-parse --verify HEAD` &&
90 check_fsck &&
91
92 rm -f A/G &&
93 test_tick && git commit -a -m monkey &&
94 K=`git rev-parse --verify HEAD` &&
95 check_fsck &&
96
97 check_have A B C D E F G H I J K L &&
98
99 git prune &&
100
101 check_have A B C D E F G H I J K L &&
102
103 check_fsck &&
104
105 loglen=$(wc -l <.git/logs/refs/heads/master) &&
106 test $loglen = 4
107'
108
109test_expect_success rewind '
110 test_tick && git reset --hard HEAD~2 &&
111 test -f C &&
112 test -f A/B/E &&
113 ! test -f F &&
114 ! test -f A/G &&
115
116 check_have A B C D E F G H I J K L &&
117
118 git prune &&
119
120 check_have A B C D E F G H I J K L &&
121
122 loglen=$(wc -l <.git/logs/refs/heads/master) &&
123 test $loglen = 5
124'
125
126test_expect_success 'corrupt and check' '
127
128 corrupt $F &&
129 check_fsck "missing blob $F"
130
131'
132
133test_expect_success 'reflog expire --dry-run should not touch reflog' '
134
135 git reflog expire --dry-run \
136 --expire=$(($test_tick - 10000)) \
137 --expire-unreachable=$(($test_tick - 10000)) \
138 --stale-fix \
139 --all &&
140
141 loglen=$(wc -l <.git/logs/refs/heads/master) &&
142 test $loglen = 5 &&
143
144 check_fsck "missing blob $F"
145'
146
147test_expect_success 'reflog expire' '
148
149 git reflog expire --verbose \
150 --expire=$(($test_tick - 10000)) \
151 --expire-unreachable=$(($test_tick - 10000)) \
152 --stale-fix \
153 --all &&
154
155 loglen=$(wc -l <.git/logs/refs/heads/master) &&
156 test $loglen = 2 &&
157
158 check_fsck "dangling commit $K"
159'
160
161test_expect_success 'prune and fsck' '
162
163 git prune &&
164 check_fsck &&
165
166 check_have A B C D E H L &&
167 check_dont_have F G I J K
168
169'
170
171test_expect_success 'recover and check' '
172
173 recover $F &&
174 check_fsck "dangling blob $F"
175
176'
177
178test_expect_success 'delete' '
179 echo 1 > C &&
180 test_tick &&
181 git commit -m rat C &&
182
183 echo 2 > C &&
184 test_tick &&
185 git commit -m ox C &&
186
187 echo 3 > C &&
188 test_tick &&
189 git commit -m tiger C &&
190
191 test 5 = $(git reflog | wc -l) &&
192
193 git reflog delete master@{1} &&
194 git reflog show master > output &&
195 test 4 = $(wc -l < output) &&
196 ! grep ox < output &&
197
198 git reflog delete master@{07.04.2005.15:15:00.-0700} &&
199 git reflog show master > output &&
200 test 3 = $(wc -l < output) &&
201 ! grep dragon < output
202
203'
204
205test_done