1#!/bin/sh
23
test_description='git repack works correctly'
45
. ./test-lib.sh
67
test_expect_success 'objects in packs marked .keep are not repacked' '
8echo content1 > file1 &&
9echo content2 > file2 &&
10git add . &&
11test_tick &&
12git commit -m initial_commit &&
13# Create two packs
14# The first pack will contain all of the objects except one
15git rev-list --objects --all | grep -v file2 |
16git pack-objects pack > /dev/null &&
17# The second pack will contain the excluded object
18packsha1=$(git rev-list --objects --all | grep file2 |
19git pack-objects pack) &&
20>pack-$packsha1.keep &&
21objsha1=$(git verify-pack -v pack-$packsha1.idx | head -n 1 |
22sed -e "s/^\([0-9a-f]\{40\}\).*/\1/") &&
23mv pack-* .git/objects/pack/ &&
24git repack --no-pack-kept-objects -A -d -l &&
25git prune-packed &&
26for p in .git/objects/pack/*.idx; do
27idx=$(basename $p)
28test "pack-$packsha1.idx" = "$idx" && continue
29if git verify-pack -v $p | egrep "^$objsha1"; then
30found_duplicate_object=1
31echo "DUPLICATE OBJECT FOUND"
32break
33fi
34done &&
35test -z "$found_duplicate_object"
36'
3738
test_expect_success 'writing bitmaps can duplicate .keep objects' '
39# build on $objsha1, $packsha1, and .keep state from previous
40git repack -Adl &&
41test_when_finished "found_duplicate_object=" &&
42for p in .git/objects/pack/*.idx; do
43idx=$(basename $p)
44test "pack-$packsha1.idx" = "$idx" && continue
45if git verify-pack -v $p | egrep "^$objsha1"; then
46found_duplicate_object=1
47echo "DUPLICATE OBJECT FOUND"
48break
49fi
50done &&
51test "$found_duplicate_object" = 1
52'
5354
test_expect_success 'loose objects in alternate ODB are not repacked' '
55mkdir alt_objects &&
56echo `pwd`/alt_objects > .git/objects/info/alternates &&
57echo content3 > file3 &&
58objsha1=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
59git add file3 &&
60test_tick &&
61git commit -m commit_file3 &&
62git repack -a -d -l &&
63git prune-packed &&
64for p in .git/objects/pack/*.idx; do
65if git verify-pack -v $p | egrep "^$objsha1"; then
66found_duplicate_object=1
67echo "DUPLICATE OBJECT FOUND"
68break
69fi
70done &&
71test -z "$found_duplicate_object"
72'
7374
test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
75mkdir alt_objects/pack &&
76mv .git/objects/pack/* alt_objects/pack &&
77git repack -a &&
78myidx=$(ls -1 .git/objects/pack/*.idx) &&
79test -f "$myidx" &&
80for p in alt_objects/pack/*.idx; do
81git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"
82done | while read sha1 rest; do
83if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then
84echo "Missing object in local pack: $sha1"
85return 1
86fi
87done
88'
8990
test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
91rm -f .git/objects/pack/* &&
92echo new_content >> file1 &&
93git add file1 &&
94test_tick &&
95git commit -m more_content &&
96git repack &&
97git repack -a -d &&
98myidx=$(ls -1 .git/objects/pack/*.idx) &&
99test -f "$myidx" &&
100for p in alt_objects/pack/*.idx; do
101git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"
102done | while read sha1 rest; do
103if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then
104echo "Missing object in local pack: $sha1"
105return 1
106fi
107done
108'
109110
test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
111# swap the .keep so the commit object is in the pack with .keep
112for p in alt_objects/pack/*.pack
113do
114base_name=$(basename $p .pack) &&
115if test -f alt_objects/pack/$base_name.keep
116then
117rm alt_objects/pack/$base_name.keep
118else
119touch alt_objects/pack/$base_name.keep
120fi
121done &&
122git repack -a -d &&
123myidx=$(ls -1 .git/objects/pack/*.idx) &&
124test -f "$myidx" &&
125for p in alt_objects/pack/*.idx; do
126git verify-pack -v $p | sed -n -e "/^[0-9a-f]\{40\}/p"
127done | while read sha1 rest; do
128if ! ( git verify-pack -v $myidx | grep "^$sha1" ); then
129echo "Missing object in local pack: $sha1"
130return 1
131fi
132done
133'
134135
test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
136rm -f alt_objects/pack/*.keep &&
137mv .git/objects/pack/* alt_objects/pack/ &&
138csha1=$(git rev-parse HEAD^{commit}) &&
139git reset --hard HEAD^ &&
140test_tick &&
141git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
142# The pack-objects call on the next line is equivalent to
143# git repack -A -d without the call to prune-packed
144git pack-objects --honor-pack-keep --non-empty --all --reflog \
145--unpack-unreachable </dev/null pack &&
146rm -f .git/objects/pack/* &&
147mv pack-* .git/objects/pack/ &&
148test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
149egrep "^$csha1 " | sort | uniq | wc -l) &&
150echo > .git/objects/info/alternates &&
151test_must_fail git show $csha1
152'
153154
test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
155echo `pwd`/alt_objects > .git/objects/info/alternates &&
156echo "$csha1" | git pack-objects --non-empty --all --reflog pack &&
157rm -f .git/objects/pack/* &&
158mv pack-* .git/objects/pack/ &&
159# The pack-objects call on the next line is equivalent to
160# git repack -A -d without the call to prune-packed
161git pack-objects --honor-pack-keep --non-empty --all --reflog \
162--unpack-unreachable </dev/null pack &&
163rm -f .git/objects/pack/* &&
164mv pack-* .git/objects/pack/ &&
165test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
166egrep "^$csha1 " | sort | uniq | wc -l) &&
167echo > .git/objects/info/alternates &&
168test_must_fail git show $csha1
169'
170171
test_expect_success 'objects made unreachable by grafts only are kept' '
172test_tick &&
173git commit --allow-empty -m "commit 4" &&
174H0=$(git rev-parse HEAD) &&
175H1=$(git rev-parse HEAD^) &&
176H2=$(git rev-parse HEAD^^) &&
177echo "$H0 $H2" > .git/info/grafts &&
178git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
179git repack -a -d &&
180git cat-file -t $H1
181'
182183
test_done
184