3 test_description
='git repack works correctly'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14 test_expect_success
'-A with -d option leaves unreachable objects unpacked' '
15 echo content > file1 &&
18 git commit -m initial_commit &&
19 # create a transient branch with unique content
20 git checkout -b transient_branch &&
21 echo more content >> file1 &&
22 # record the objects created in the database for file, commit, tree
23 fsha1=$(git hash-object file1) &&
25 git commit -a -m more_content &&
26 csha1=$(git rev-parse HEAD^{commit}) &&
27 tsha1=$(git rev-parse HEAD^{tree}) &&
29 echo even more content >> file1 &&
31 git commit -a -m even_more_content &&
32 # delete the transient branch
33 git branch -D transient_branch &&
35 git repack -A -d -l &&
36 # verify objects are packed in repository
37 test 3 = $(git verify-pack -v -- .git/objects/pack/*.idx |
38 grep -E "^($fsha1|$csha1|$tsha1) " |
39 sort | uniq | wc -l) &&
43 # now expire the reflog, while keeping reachable ones but expiring
44 # unreachables immediately
46 sometimeago=$(( $test_tick - 10000 )) &&
47 git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all &&
49 git repack -A -d -l &&
50 # verify objects are retained unpacked
51 test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
52 grep -E "^($fsha1|$csha1|$tsha1) " |
53 sort | uniq | wc -l) &&
63 test "$tref" = "$t" ||
return 1
67 test_expect_success
'-A without -d option leaves unreachable objects packed' '
68 fsha1path=$(echo "$fsha1" | sed -e "s|\(..\)|\1/|") &&
69 fsha1path=".git/objects/$fsha1path" &&
70 csha1path=$(echo "$csha1" | sed -e "s|\(..\)|\1/|") &&
71 csha1path=".git/objects/$csha1path" &&
72 tsha1path=$(echo "$tsha1" | sed -e "s|\(..\)|\1/|") &&
73 tsha1path=".git/objects/$tsha1path" &&
74 git branch transient_branch $csha1 &&
75 git repack -a -d -l &&
76 test ! -f "$fsha1path" &&
77 test ! -f "$csha1path" &&
78 test ! -f "$tsha1path" &&
79 test 1 = $(ls -1 .git/objects/pack/pack-*.pack | wc -l) &&
80 packfile=$(ls .git/objects/pack/pack-*.pack) &&
81 git branch -D transient_branch &&
84 test ! -f "$fsha1path" &&
85 test ! -f "$csha1path" &&
86 test ! -f "$tsha1path" &&
92 test_expect_success
'unpacked objects receive timestamp of pack file' '
93 tmppack=".git/objects/pack/tmp_pack" &&
94 ln "$packfile" "$tmppack" &&
95 git repack -A -l -d &&
96 test-tool chmtime --get "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \
98 compare_mtimes < mtimes
101 test_expect_success
'do not bother loosening old objects' '
102 obj1=$(echo one | git hash-object -w --stdin) &&
103 obj2=$(echo two | git hash-object -w --stdin) &&
104 pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) &&
105 pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) &&
107 git cat-file -p $obj1 &&
108 git cat-file -p $obj2 &&
109 test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
110 git repack -A -d --unpack-unreachable=1.hour.ago &&
111 git cat-file -p $obj1 &&
112 test_must_fail git cat-file -p $obj2
115 test_expect_success
'keep packed objects found only in index' '
116 echo my-unique-content >file &&
118 git commit -m "make it reachable" &&
121 git reflog expire --expire=now --all &&
123 test-tool chmtime =-86400 .git/objects/pack/* &&
124 git gc --prune=1.hour.ago &&
125 git cat-file blob :file
128 test_expect_success
'repack -k keeps unreachable packed objects' '
129 # create packed-but-unreachable object
130 sha1=$(echo unreachable-packed | git hash-object -w --stdin) &&
131 pack=$(echo $sha1 | git pack-objects .git/objects/pack/pack) &&
136 git cat-file -p $sha1 &&
138 # and double check that without -k it would have been removed
140 test_must_fail git cat-file -p $sha1
143 test_expect_success
'repack -k packs unreachable loose objects' '
144 # create loose unreachable object
145 sha1=$(echo would-be-deleted-loose | git hash-object -w --stdin) &&
146 objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") &&
147 test_path_is_file $objpath &&
149 # and confirm that the loose object goes away, but we can
150 # still access it (ergo, it is packed)
152 test_path_is_missing $objpath &&
153 git cat-file -p $sha1