config: unify code paths to get global config paths
[git] / t / t7701-repack-unpack-unreachable.sh
1 #!/bin/sh
2
3 test_description='git repack works correctly'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 fsha1=
11 csha1=
12 tsha1=
13
14 test_expect_success '-A with -d option leaves unreachable objects unpacked' '
15         echo content > file1 &&
16         git add . &&
17         test_tick &&
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) &&
24         test_tick &&
25         git commit -a -m more_content &&
26         csha1=$(git rev-parse HEAD^{commit}) &&
27         tsha1=$(git rev-parse HEAD^{tree}) &&
28         git checkout main &&
29         echo even more content >> file1 &&
30         test_tick &&
31         git commit -a -m even_more_content &&
32         # delete the transient branch
33         git branch -D transient_branch &&
34         # pack the repo
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                    egrep "^($fsha1|$csha1|$tsha1) " |
39                    sort | uniq | wc -l) &&
40         git show $fsha1 &&
41         git show $csha1 &&
42         git show $tsha1 &&
43         # now expire the reflog, while keeping reachable ones but expiring
44         # unreachables immediately
45         test_tick &&
46         sometimeago=$(( $test_tick - 10000 )) &&
47         git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all &&
48         # and repack
49         git repack -A -d -l &&
50         # verify objects are retained unpacked
51         test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
52                    egrep "^($fsha1|$csha1|$tsha1) " |
53                    sort | uniq | wc -l) &&
54         git show $fsha1 &&
55         git show $csha1 &&
56         git show $tsha1
57 '
58
59 compare_mtimes ()
60 {
61         read tref &&
62         while read t; do
63                 test "$tref" = "$t" || return 1
64         done
65 }
66
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 &&
82         test_tick &&
83         git repack -A -l &&
84         test ! -f "$fsha1path" &&
85         test ! -f "$csha1path" &&
86         test ! -f "$tsha1path" &&
87         git show $fsha1 &&
88         git show $csha1 &&
89         git show $tsha1
90 '
91
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" \
97                 > mtimes &&
98         compare_mtimes < mtimes
99 '
100
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) &&
106         git prune-packed &&
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
113 '
114
115 test_expect_success 'keep packed objects found only in index' '
116         echo my-unique-content >file &&
117         git add file &&
118         git commit -m "make it reachable" &&
119         git gc &&
120         git reset HEAD^ &&
121         git reflog expire --expire=now --all &&
122         git add file &&
123         test-tool chmtime =-86400 .git/objects/pack/* &&
124         git gc --prune=1.hour.ago &&
125         git cat-file blob :file
126 '
127
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) &&
132         git prune-packed &&
133
134         # -k should keep it
135         git repack -adk &&
136         git cat-file -p $sha1 &&
137
138         # and double check that without -k it would have been removed
139         git repack -ad &&
140         test_must_fail git cat-file -p $sha1
141 '
142
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 &&
148
149         # and confirm that the loose object goes away, but we can
150         # still access it (ergo, it is packed)
151         git repack -adk &&
152         test_path_is_missing $objpath &&
153         git cat-file -p $sha1
154 '
155
156 test_done