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