Merge branch 'bc/asciidoc-pretty-formats-fix'
[git] / t / t5304-prune.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Johannes E. Schindelin
4 #
5
6 test_description='prune'
7 . ./test-lib.sh
8
9 day=$((60*60*24))
10 week=$(($day*7))
11
12 add_blob() {
13         before=$(git count-objects | sed "s/ .*//") &&
14         BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
15         BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
16         test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
17         test -f $BLOB_FILE &&
18         test-chmtime =+0 $BLOB_FILE
19 }
20
21 test_expect_success setup '
22
23         : > file &&
24         git add file &&
25         test_tick &&
26         git commit -m initial &&
27         git gc
28
29 '
30
31 test_expect_success 'prune stale packs' '
32
33         orig_pack=$(echo .git/objects/pack/*.pack) &&
34         : > .git/objects/tmp_1.pack &&
35         : > .git/objects/tmp_2.pack &&
36         test-chmtime =-86501 .git/objects/tmp_1.pack &&
37         git prune --expire 1.day &&
38         test -f $orig_pack &&
39         test -f .git/objects/tmp_2.pack &&
40         ! test -f .git/objects/tmp_1.pack
41
42 '
43
44 test_expect_success 'prune --expire' '
45
46         add_blob &&
47         git prune --expire=1.hour.ago &&
48         test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
49         test -f $BLOB_FILE &&
50         test-chmtime =-86500 $BLOB_FILE &&
51         git prune --expire 1.day &&
52         test $before = $(git count-objects | sed "s/ .*//") &&
53         ! test -f $BLOB_FILE
54
55 '
56
57 test_expect_success 'gc: implicit prune --expire' '
58
59         add_blob &&
60         test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
61         git gc &&
62         test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
63         test -f $BLOB_FILE &&
64         test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
65         git gc &&
66         test $before = $(git count-objects | sed "s/ .*//") &&
67         ! test -f $BLOB_FILE
68
69 '
70
71 test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
72
73         git config gc.pruneExpire invalid &&
74         test_must_fail git gc
75
76 '
77
78 test_expect_success 'gc: start with ok gc.pruneExpire' '
79
80         git config gc.pruneExpire 2.days.ago &&
81         git gc
82
83 '
84
85 test_expect_success 'prune: prune nonsense parameters' '
86
87         test_must_fail git prune garbage &&
88         test_must_fail git prune --- &&
89         test_must_fail git prune --no-such-option
90
91 '
92
93 test_expect_success 'prune: prune unreachable heads' '
94
95         git config core.logAllRefUpdates false &&
96         mv .git/logs .git/logs.old &&
97         : > file2 &&
98         git add file2 &&
99         git commit -m temporary &&
100         tmp_head=$(git rev-list -1 HEAD) &&
101         git reset HEAD^ &&
102         git prune &&
103         test_must_fail git reset $tmp_head --
104
105 '
106
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
129 test_expect_success 'prune: do not prune heads listed as an argument' '
130
131         : > file2 &&
132         git add file2 &&
133         git commit -m temporary &&
134         tmp_head=$(git rev-list -1 HEAD) &&
135         git reset HEAD^ &&
136         git prune -- $tmp_head &&
137         git reset $tmp_head --
138
139 '
140
141 test_expect_success 'gc --no-prune' '
142
143         add_blob &&
144         test-chmtime =-$((5001*$day)) $BLOB_FILE &&
145         git config gc.pruneExpire 2.days.ago &&
146         git gc --no-prune &&
147         test 1 = $(git count-objects | sed "s/ .*//") &&
148         test -f $BLOB_FILE
149
150 '
151
152 test_expect_success 'gc respects gc.pruneExpire' '
153
154         git config gc.pruneExpire 5002.days.ago &&
155         git gc &&
156         test -f $BLOB_FILE &&
157         git config gc.pruneExpire 5000.days.ago &&
158         git gc &&
159         test ! -f $BLOB_FILE
160
161 '
162
163 test_expect_success 'gc --prune=<date>' '
164
165         add_blob &&
166         test-chmtime =-$((5001*$day)) $BLOB_FILE &&
167         git gc --prune=5002.days.ago &&
168         test -f $BLOB_FILE &&
169         git gc --prune=5000.days.ago &&
170         test ! -f $BLOB_FILE
171
172 '
173
174 test_expect_success 'gc --prune=never' '
175
176         add_blob &&
177         git gc --prune=never &&
178         test -f $BLOB_FILE &&
179         git gc --prune=now &&
180         test ! -f $BLOB_FILE
181
182 '
183
184 test_expect_success 'gc respects gc.pruneExpire=never' '
185
186         git config gc.pruneExpire never &&
187         add_blob &&
188         git gc &&
189         test -f $BLOB_FILE &&
190         git config gc.pruneExpire now &&
191         git gc &&
192         test ! -f $BLOB_FILE
193
194 '
195
196 test_expect_success 'prune --expire=never' '
197
198         add_blob &&
199         git prune --expire=never &&
200         test -f $BLOB_FILE &&
201         git prune &&
202         test ! -f $BLOB_FILE
203
204 '
205
206 test_expect_success 'gc: prune old objects after local clone' '
207         add_blob &&
208         test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
209         git clone --no-hardlinks . aclone &&
210         (
211                 cd aclone &&
212                 test 1 = $(git count-objects | sed "s/ .*//") &&
213                 test -f $BLOB_FILE &&
214                 git gc --prune &&
215                 test 0 = $(git count-objects | sed "s/ .*//") &&
216                 ! test -f $BLOB_FILE
217         )
218 '
219
220 test_expect_success 'garbage report in count-objects -v' '
221         : >.git/objects/pack/foo &&
222         : >.git/objects/pack/foo.bar &&
223         : >.git/objects/pack/foo.keep &&
224         : >.git/objects/pack/foo.pack &&
225         : >.git/objects/pack/fake.bar &&
226         : >.git/objects/pack/fake.keep &&
227         : >.git/objects/pack/fake.pack &&
228         : >.git/objects/pack/fake.idx &&
229         : >.git/objects/pack/fake2.keep &&
230         : >.git/objects/pack/fake3.idx &&
231         git count-objects -v 2>stderr &&
232         grep "index file .git/objects/pack/fake.idx is too small" stderr &&
233         grep "^warning:" stderr | sort >actual &&
234         cat >expected <<\EOF &&
235 warning: garbage found: .git/objects/pack/fake.bar
236 warning: garbage found: .git/objects/pack/foo
237 warning: garbage found: .git/objects/pack/foo.bar
238 warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
239 warning: no corresponding .idx: .git/objects/pack/foo.keep
240 warning: no corresponding .idx: .git/objects/pack/foo.pack
241 warning: no corresponding .pack: .git/objects/pack/fake3.idx
242 EOF
243         test_cmp expected actual
244 '
245
246 test_expect_success 'prune .git/shallow' '
247         SHA1=`echo hi|git commit-tree HEAD^{tree}` &&
248         echo $SHA1 >.git/shallow &&
249         git prune --dry-run >out &&
250         grep $SHA1 .git/shallow &&
251         grep $SHA1 out &&
252         git prune &&
253         ! test -f .git/shallow
254 '
255
256 test_done