]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7701-repack-unpack-unreachable.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[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
ccc12972
BC
8. ./test-lib.sh
9
e896912c
BC
10fsha1=
11csha1=
12tsha1=
13
83d0289d 14test_expect_success '-A with -d option leaves unreachable objects unpacked' '
ccc12972
BC
15 echo content > file1 &&
16 git add . &&
713c79e8 17 test_tick &&
ccc12972
BC
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) &&
713c79e8 24 test_tick &&
ccc12972
BC
25 git commit -a -m more_content &&
26 csha1=$(git rev-parse HEAD^{commit}) &&
27 tsha1=$(git rev-parse HEAD^{tree}) &&
1e2ae142 28 git checkout main &&
ccc12972 29 echo even more content >> file1 &&
713c79e8 30 test_tick &&
ccc12972
BC
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 |
81580fa0 38 grep -E "^($fsha1|$csha1|$tsha1) " |
ccc12972
BC
39 sort | uniq | wc -l) &&
40 git show $fsha1 &&
41 git show $csha1 &&
42 git show $tsha1 &&
713c79e8
JH
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 &&
ccc12972
BC
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 |
81580fa0 52 grep -E "^($fsha1|$csha1|$tsha1) " |
ccc12972
BC
53 sort | uniq | wc -l) &&
54 git show $fsha1 &&
55 git show $csha1 &&
56 git show $tsha1
57'
58
e896912c
BC
59compare_mtimes ()
60{
decf711f
PSU
61 read tref &&
62 while read t; do
76e057db 63 test "$tref" = "$t" || return 1
d0409938 64 done
e896912c
BC
65}
66
83d0289d 67test_expect_success '-A without -d option leaves unreachable objects packed' '
e896912c
BC
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 &&
713c79e8 82 test_tick &&
e896912c 83 git repack -A -l &&
83d0289d
BC
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
92test_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 &&
deb9845a 96 test-tool chmtime --get "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \
d0409938
JS
97 > mtimes &&
98 compare_mtimes < mtimes
e896912c
BC
99'
100
7e52f566
JK
101test_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 &&
0e496492 109 test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
7e52f566
JK
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
c90f9e13
JK
115test_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 &&
0e496492 123 test-tool chmtime =-86400 .git/objects/pack/* &&
c90f9e13
JK
124 git gc --prune=1.hour.ago &&
125 git cat-file blob :file
126'
127
905f27b8
JK
128test_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
e26a8c47
JK
143test_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
ccc12972 156test_done