t7700: replace egrep with grep
[git] / t / t7700-repack.sh
1 #!/bin/sh
2
3 test_description='git repack works correctly'
4
5 . ./test-lib.sh
6
7 commit_and_pack () {
8         test_commit "$@" 1>&2 &&
9         SHA1=$(git pack-objects --all --unpacked --incremental .git/objects/pack/pack </dev/null) &&
10         echo pack-${SHA1}.pack
11 }
12
13 test_no_missing_in_packs () {
14         myidx=$(ls -1 .git/objects/pack/*.idx) &&
15         test_path_is_file "$myidx" &&
16         git verify-pack -v alt_objects/pack/*.idx >orig.raw &&
17         sed -n -e "s/^\([0-9a-f]\{40\}\).*/\1/p" orig.raw | sort >orig &&
18         git verify-pack -v $myidx >dest.raw &&
19         cut -d" " -f1 dest.raw | sort >dest &&
20         comm -23 orig dest >missing &&
21         test_must_be_empty missing
22 }
23
24 # we expect $packsha1 and $objsha1 to be defined
25 test_has_duplicate_object () {
26         want_duplicate_object="$1"
27         found_duplicate_object=false
28         for p in .git/objects/pack/*.idx
29         do
30                 idx=$(basename $p)
31                 test "pack-$packsha1.idx" = "$idx" && continue
32                 git verify-pack -v $p >packlist || return $?
33                 if grep "^$objsha1" packlist
34                 then
35                         found_duplicate_object=true
36                         echo "DUPLICATE OBJECT FOUND"
37                         break
38                 fi
39         done &&
40         test "$want_duplicate_object" = "$found_duplicate_object"
41 }
42
43 test_expect_success 'objects in packs marked .keep are not repacked' '
44         echo content1 >file1 &&
45         echo content2 >file2 &&
46         git add . &&
47         test_tick &&
48         git commit -m initial_commit &&
49         # Create two packs
50         # The first pack will contain all of the objects except one
51         git rev-list --objects --all | grep -v file2 |
52                 git pack-objects pack &&
53         # The second pack will contain the excluded object
54         packsha1=$(git rev-list --objects --all | grep file2 |
55                 git pack-objects pack) &&
56         >pack-$packsha1.keep &&
57         objsha1=$(git verify-pack -v pack-$packsha1.idx | head -n 1 |
58                 sed -e "s/^\([0-9a-f]\{40\}\).*/\1/") &&
59         mv pack-* .git/objects/pack/ &&
60         git repack -A -d -l &&
61         git prune-packed &&
62         test_has_duplicate_object false
63 '
64
65 test_expect_success 'writing bitmaps via command-line can duplicate .keep objects' '
66         # build on $objsha1, $packsha1, and .keep state from previous
67         git repack -Adbl &&
68         test_has_duplicate_object true
69 '
70
71 test_expect_success 'writing bitmaps via config can duplicate .keep objects' '
72         # build on $objsha1, $packsha1, and .keep state from previous
73         git -c repack.writebitmaps=true repack -Adl &&
74         test_has_duplicate_object true
75 '
76
77 test_expect_success 'loose objects in alternate ODB are not repacked' '
78         mkdir alt_objects &&
79         echo $(pwd)/alt_objects >.git/objects/info/alternates &&
80         echo content3 >file3 &&
81         objsha1=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
82         git add file3 &&
83         test_tick &&
84         git commit -m commit_file3 &&
85         git repack -a -d -l &&
86         git prune-packed &&
87         test_has_duplicate_object false
88 '
89
90 test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
91         mkdir alt_objects/pack &&
92         mv .git/objects/pack/* alt_objects/pack &&
93         git repack -a &&
94         test_no_missing_in_packs
95 '
96
97 test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
98         rm -f .git/objects/pack/* &&
99         echo new_content >>file1 &&
100         git add file1 &&
101         test_tick &&
102         git commit -m more_content &&
103         git repack &&
104         git repack -a -d &&
105         test_no_missing_in_packs
106 '
107
108 test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
109         # swap the .keep so the commit object is in the pack with .keep
110         for p in alt_objects/pack/*.pack
111         do
112                 base_name=$(basename $p .pack) &&
113                 if test_path_is_file alt_objects/pack/$base_name.keep
114                 then
115                         rm alt_objects/pack/$base_name.keep
116                 else
117                         touch alt_objects/pack/$base_name.keep
118                 fi
119         done &&
120         git repack -a -d &&
121         test_no_missing_in_packs
122 '
123
124 test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
125         rm -f alt_objects/pack/*.keep &&
126         mv .git/objects/pack/* alt_objects/pack/ &&
127         csha1=$(git rev-parse HEAD^{commit}) &&
128         git reset --hard HEAD^ &&
129         test_tick &&
130         git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
131         # The pack-objects call on the next line is equivalent to
132         # git repack -A -d without the call to prune-packed
133         git pack-objects --honor-pack-keep --non-empty --all --reflog \
134             --unpack-unreachable </dev/null pack &&
135         rm -f .git/objects/pack/* &&
136         mv pack-* .git/objects/pack/ &&
137         test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
138                 grep "^$csha1 " | sort | uniq | wc -l) &&
139         echo >.git/objects/info/alternates &&
140         test_must_fail git show $csha1
141 '
142
143 test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
144         echo $(pwd)/alt_objects >.git/objects/info/alternates &&
145         echo "$csha1" | git pack-objects --non-empty --all --reflog pack &&
146         rm -f .git/objects/pack/* &&
147         mv pack-* .git/objects/pack/ &&
148         # The pack-objects call on the next line is equivalent to
149         # git repack -A -d without the call to prune-packed
150         git pack-objects --honor-pack-keep --non-empty --all --reflog \
151             --unpack-unreachable </dev/null pack &&
152         rm -f .git/objects/pack/* &&
153         mv pack-* .git/objects/pack/ &&
154         test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
155                 grep "^$csha1 " | sort | uniq | wc -l) &&
156         echo >.git/objects/info/alternates &&
157         test_must_fail git show $csha1
158 '
159
160 test_expect_success 'objects made unreachable by grafts only are kept' '
161         test_tick &&
162         git commit --allow-empty -m "commit 4" &&
163         H0=$(git rev-parse HEAD) &&
164         H1=$(git rev-parse HEAD^) &&
165         H2=$(git rev-parse HEAD^^) &&
166         echo "$H0 $H2" >.git/info/grafts &&
167         git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
168         git repack -a -d &&
169         git cat-file -t $H1
170 '
171
172 test_expect_success 'repack --keep-pack' '
173         test_create_repo keep-pack &&
174         (
175                 cd keep-pack &&
176                 P1=$(commit_and_pack 1) &&
177                 P2=$(commit_and_pack 2) &&
178                 P3=$(commit_and_pack 3) &&
179                 P4=$(commit_and_pack 4) &&
180                 ls .git/objects/pack/*.pack >old-counts &&
181                 test_line_count = 4 old-counts &&
182                 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
183                 ls .git/objects/pack/*.pack >new-counts &&
184                 grep -q $P1 new-counts &&
185                 grep -q $P4 new-counts &&
186                 test_line_count = 3 new-counts &&
187                 git fsck
188         )
189 '
190
191 test_expect_success 'bitmaps are created by default in bare repos' '
192         git clone --bare .git bare.git &&
193         git -C bare.git repack -ad &&
194         bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
195         test_path_is_file "$bitmap"
196 '
197
198 test_expect_success 'incremental repack does not complain' '
199         git -C bare.git repack -q 2>repack.err &&
200         test_must_be_empty repack.err
201 '
202
203 test_expect_success 'bitmaps can be disabled on bare repos' '
204         git -c repack.writeBitmaps=false -C bare.git repack -ad &&
205         bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
206         test -z "$bitmap"
207 '
208
209 test_expect_success 'no bitmaps created if .keep files present' '
210         pack=$(ls bare.git/objects/pack/*.pack) &&
211         test_path_is_file "$pack" &&
212         keep=${pack%.pack}.keep &&
213         test_when_finished "rm -f \"\$keep\"" &&
214         >"$keep" &&
215         git -C bare.git repack -ad 2>stderr &&
216         test_must_be_empty stderr &&
217         find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
218         test_must_be_empty actual
219 '
220
221 test_expect_success 'auto-bitmaps do not complain if unavailable' '
222         test_config -C bare.git pack.packSizeLimit 1M &&
223         blob=$(test-tool genrandom big $((1024*1024)) |
224                git -C bare.git hash-object -w --stdin) &&
225         git -C bare.git update-ref refs/tags/big $blob &&
226         git -C bare.git repack -ad 2>stderr &&
227         test_must_be_empty stderr &&
228         find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
229         test_must_be_empty actual
230 '
231
232 test_done