1#!/bin/sh
2
3test_description='git repack works correctly'
4
5. ./test-lib.sh
6
7fsha1=
8csha1=
9tsha1=
10
11test_expect_success '-A with -d option leaves unreachable objects unpacked' '
12 echo content > file1 &&
13 git add . &&
14 test_tick &&
15 git commit -m initial_commit &&
16 # create a transient branch with unique content
17 git checkout -b transient_branch &&
18 echo more content >> file1 &&
19 # record the objects created in the database for file, commit, tree
20 fsha1=$(git hash-object file1) &&
21 test_tick &&
22 git commit -a -m more_content &&
23 csha1=$(git rev-parse HEAD^{commit}) &&
24 tsha1=$(git rev-parse HEAD^{tree}) &&
25 git checkout master &&
26 echo even more content >> file1 &&
27 test_tick &&
28 git commit -a -m even_more_content &&
29 # delete the transient branch
30 git branch -D transient_branch &&
31 # pack the repo
32 git repack -A -d -l &&
33 # verify objects are packed in repository
34 test 3 = $(git verify-pack -v -- .git/objects/pack/*.idx |
35 egrep "^($fsha1|$csha1|$tsha1) " |
36 sort | uniq | wc -l) &&
37 git show $fsha1 &&
38 git show $csha1 &&
39 git show $tsha1 &&
40 # now expire the reflog, while keeping reachable ones but expiring
41 # unreachables immediately
42 test_tick &&
43 sometimeago=$(( $test_tick - 10000 )) &&
44 git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all &&
45 # and repack
46 git repack -A -d -l &&
47 # verify objects are retained unpacked
48 test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
49 egrep "^($fsha1|$csha1|$tsha1) " |
50 sort | uniq | wc -l) &&
51 git show $fsha1 &&
52 git show $csha1 &&
53 git show $tsha1
54'
55
56compare_mtimes ()
57{
58 read tref rest &&
59 while read t rest; do
60 test "$tref" = "$t" || return 1
61 done
62}
63
64test_expect_success '-A without -d option leaves unreachable objects packed' '
65 fsha1path=$(echo "$fsha1" | sed -e "s|\(..\)|\1/|") &&
66 fsha1path=".git/objects/$fsha1path" &&
67 csha1path=$(echo "$csha1" | sed -e "s|\(..\)|\1/|") &&
68 csha1path=".git/objects/$csha1path" &&
69 tsha1path=$(echo "$tsha1" | sed -e "s|\(..\)|\1/|") &&
70 tsha1path=".git/objects/$tsha1path" &&
71 git branch transient_branch $csha1 &&
72 git repack -a -d -l &&
73 test ! -f "$fsha1path" &&
74 test ! -f "$csha1path" &&
75 test ! -f "$tsha1path" &&
76 test 1 = $(ls -1 .git/objects/pack/pack-*.pack | wc -l) &&
77 packfile=$(ls .git/objects/pack/pack-*.pack) &&
78 git branch -D transient_branch &&
79 test_tick &&
80 git repack -A -l &&
81 test ! -f "$fsha1path" &&
82 test ! -f "$csha1path" &&
83 test ! -f "$tsha1path" &&
84 git show $fsha1 &&
85 git show $csha1 &&
86 git show $tsha1
87'
88
89test_expect_success 'unpacked objects receive timestamp of pack file' '
90 tmppack=".git/objects/pack/tmp_pack" &&
91 ln "$packfile" "$tmppack" &&
92 git repack -A -l -d &&
93 test-tool chmtime -v +0 "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \
94 > mtimes &&
95 compare_mtimes < mtimes
96'
97
98test_expect_success 'do not bother loosening old objects' '
99 obj1=$(echo one | git hash-object -w --stdin) &&
100 obj2=$(echo two | git hash-object -w --stdin) &&
101 pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) &&
102 pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) &&
103 git prune-packed &&
104 git cat-file -p $obj1 &&
105 git cat-file -p $obj2 &&
106 test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
107 git repack -A -d --unpack-unreachable=1.hour.ago &&
108 git cat-file -p $obj1 &&
109 test_must_fail git cat-file -p $obj2
110'
111
112test_expect_success 'keep packed objects found only in index' '
113 echo my-unique-content >file &&
114 git add file &&
115 git commit -m "make it reachable" &&
116 git gc &&
117 git reset HEAD^ &&
118 git reflog expire --expire=now --all &&
119 git add file &&
120 test-tool chmtime =-86400 .git/objects/pack/* &&
121 git gc --prune=1.hour.ago &&
122 git cat-file blob :file
123'
124
125test_expect_success 'repack -k keeps unreachable packed objects' '
126 # create packed-but-unreachable object
127 sha1=$(echo unreachable-packed | git hash-object -w --stdin) &&
128 pack=$(echo $sha1 | git pack-objects .git/objects/pack/pack) &&
129 git prune-packed &&
130
131 # -k should keep it
132 git repack -adk &&
133 git cat-file -p $sha1 &&
134
135 # and double check that without -k it would have been removed
136 git repack -ad &&
137 test_must_fail git cat-file -p $sha1
138'
139
140test_expect_success 'repack -k packs unreachable loose objects' '
141 # create loose unreachable object
142 sha1=$(echo would-be-deleted-loose | git hash-object -w --stdin) &&
143 objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") &&
144 test_path_is_file $objpath &&
145
146 # and confirm that the loose object goes away, but we can
147 # still access it (ergo, it is packed)
148 git repack -adk &&
149 test_path_is_missing $objpath &&
150 git cat-file -p $sha1
151'
152
153test_done