The eleventh batch
[git/gitster.git] / t / t1016-compatObjectFormat.sh
blob32e0af392eff42a7c75d3d74e08652c8d24d04f3
1 #!/bin/sh
3 # Copyright (c) 2023 Eric Biederman
6 test_description='Test how well compatObjectFormat works'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-gpg.sh
12 # All of the follow variables must be defined in the environment:
13 # GIT_AUTHOR_NAME
14 # GIT_AUTHOR_EMAIL
15 # GIT_AUTHOR_DATE
16 # GIT_COMMITTER_NAME
17 # GIT_COMMITTER_EMAIL
18 # GIT_COMMITTER_DATE
20 # The test relies on these variables being set so that the two
21 # different commits in two different repositories encoded with two
22 # different hash functions result in the same content in the commits.
23 # This means that when the commit is translated between hash functions
24 # the commit is identical to the commit in the other repository.
26 compat_hash () {
27 case "$1" in
28 "sha1")
29 echo "sha256"
31 "sha256")
32 echo "sha1"
34 esac
37 hello_oid () {
38 case "$1" in
39 "sha1")
40 echo "$hello_sha1_oid"
42 "sha256")
43 echo "$hello_sha256_oid"
45 esac
48 tree_oid () {
49 case "$1" in
50 "sha1")
51 echo "$tree_sha1_oid"
53 "sha256")
54 echo "$tree_sha256_oid"
56 esac
59 commit_oid () {
60 case "$1" in
61 "sha1")
62 echo "$commit_sha1_oid"
64 "sha256")
65 echo "$commit_sha256_oid"
67 esac
70 commit2_oid () {
71 case "$1" in
72 "sha1")
73 echo "$commit2_sha1_oid"
75 "sha256")
76 echo "$commit2_sha256_oid"
78 esac
81 del_sigcommit () {
82 local delete="$1"
84 if test "$delete" = "sha256" ; then
85 local pattern="gpgsig-sha256"
86 else
87 local pattern="gpgsig"
89 test-tool delete-gpgsig "$pattern"
92 del_sigtag () {
93 local storage="$1"
94 local delete="$2"
96 if test "$storage" = "$delete" ; then
97 local pattern="trailer"
98 elif test "$storage" = "sha256" ; then
99 local pattern="gpgsig"
100 else
101 local pattern="gpgsig-sha256"
103 test-tool delete-gpgsig "$pattern"
106 base=$(pwd)
107 for hash in sha1 sha256
109 cd "$base"
110 mkdir -p repo-$hash
111 cd repo-$hash
113 test_expect_success "setup $hash repository" '
114 git init --object-format=$hash &&
115 git config core.repositoryformatversion 1 &&
116 git config extensions.objectformat $hash &&
117 git config extensions.compatobjectformat $(compat_hash $hash) &&
118 test_config gpg.program $TEST_DIRECTORY/t1016/gpg &&
119 echo "Hello World!" >hello &&
120 eval hello_${hash}_oid=$(git hash-object hello) &&
121 git update-index --add hello &&
122 git commit -m "Initial commit" &&
123 eval commit_${hash}_oid=$(git rev-parse HEAD) &&
124 eval tree_${hash}_oid=$(git rev-parse HEAD^{tree})
126 test_expect_success "create a $hash tagged blob" '
127 git tag --no-sign -m "This is a tag" hellotag $(hello_oid $hash) &&
128 eval hellotag_${hash}_oid=$(git rev-parse hellotag)
130 test_expect_success "create a $hash tagged tree" '
131 git tag --no-sign -m "This is a tag" treetag $(tree_oid $hash) &&
132 eval treetag_${hash}_oid=$(git rev-parse treetag)
134 test_expect_success "create a $hash tagged commit" '
135 git tag --no-sign -m "This is a tag" committag $(commit_oid $hash) &&
136 eval committag_${hash}_oid=$(git rev-parse committag)
138 test_expect_success GPG2 "create a $hash signed commit" '
139 git commit --gpg-sign --allow-empty -m "This is a signed commit" &&
140 eval signedcommit_${hash}_oid=$(git rev-parse HEAD)
142 test_expect_success GPG2 "create a $hash signed tag" '
143 git tag -s -m "This is a signed tag" signedtag HEAD &&
144 eval signedtag_${hash}_oid=$(git rev-parse signedtag)
146 test_expect_success "create a $hash branch" '
147 git checkout -b branch $(commit_oid $hash) &&
148 echo "More more more give me more!" >more &&
149 eval more_${hash}_oid=$(git hash-object more) &&
150 echo "Another and another and another" >another &&
151 eval another_${hash}_oid=$(git hash-object another) &&
152 git update-index --add more another &&
153 git commit -m "Add more files!" &&
154 eval commit2_${hash}_oid=$(git rev-parse HEAD) &&
155 eval tree2_${hash}_oid=$(git rev-parse HEAD^{tree})
157 test_expect_success GPG2 "create another $hash signed tag" '
158 git tag -s -m "This is another signed tag" signedtag2 $(commit2_oid $hash) &&
159 eval signedtag2_${hash}_oid=$(git rev-parse signedtag2)
161 test_expect_success GPG2 "merge the $hash branches together" '
162 git merge -S -m "merge some signed tags together" signedtag signedtag2 &&
163 eval signedcommit2_${hash}_oid=$(git rev-parse HEAD)
165 test_expect_success GPG2 "create additional $hash signed commits" '
166 git commit --gpg-sign --allow-empty -m "This is an additional signed commit" &&
167 git cat-file commit HEAD | del_sigcommit sha256 >"../${hash}_signedcommit3" &&
168 git cat-file commit HEAD | del_sigcommit sha1 >"../${hash}_signedcommit4" &&
169 eval signedcommit3_${hash}_oid=$(git hash-object -t commit -w ../${hash}_signedcommit3) &&
170 eval signedcommit4_${hash}_oid=$(git hash-object -t commit -w ../${hash}_signedcommit4)
172 test_expect_success GPG2 "create additional $hash signed tags" '
173 git tag -s -m "This is an additional signed tag" signedtag34 HEAD &&
174 git cat-file tag signedtag34 | del_sigtag "${hash}" sha256 >../${hash}_signedtag3 &&
175 git cat-file tag signedtag34 | del_sigtag "${hash}" sha1 >../${hash}_signedtag4 &&
176 eval signedtag3_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag3) &&
177 eval signedtag4_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag4)
179 done
180 cd "$base"
182 compare_oids () {
183 test "$#" = 5 && { local PREREQ="$1"; shift; } || PREREQ=
184 local type="$1"
185 local name="$2"
186 local sha1_oid="$3"
187 local sha256_oid="$4"
189 echo ${sha1_oid} >${name}_sha1_expected
190 echo ${sha256_oid} >${name}_sha256_expected
191 echo ${type} >${name}_type_expected
193 git --git-dir=repo-sha1/.git rev-parse --output-object-format=sha256 ${sha1_oid} >${name}_sha1_sha256_found
194 git --git-dir=repo-sha256/.git rev-parse --output-object-format=sha1 ${sha256_oid} >${name}_sha256_sha1_found
195 local sha1_sha256_oid="$(cat ${name}_sha1_sha256_found)"
196 local sha256_sha1_oid="$(cat ${name}_sha256_sha1_found)"
198 test_expect_success $PREREQ "Verify ${type} ${name}'s sha1 oid" '
199 git --git-dir=repo-sha256/.git rev-parse --output-object-format=sha1 ${sha256_oid} >${name}_sha1 &&
200 test_cmp ${name}_sha1 ${name}_sha1_expected
203 test_expect_success $PREREQ "Verify ${type} ${name}'s sha256 oid" '
204 git --git-dir=repo-sha1/.git rev-parse --output-object-format=sha256 ${sha1_oid} >${name}_sha256 &&
205 test_cmp ${name}_sha256 ${name}_sha256_expected
208 test_expect_success $PREREQ "Verify ${name}'s sha1 type" '
209 git --git-dir=repo-sha1/.git cat-file -t ${sha1_oid} >${name}_type1 &&
210 git --git-dir=repo-sha256/.git cat-file -t ${sha256_sha1_oid} >${name}_type2 &&
211 test_cmp ${name}_type1 ${name}_type2 &&
212 test_cmp ${name}_type1 ${name}_type_expected
215 test_expect_success $PREREQ "Verify ${name}'s sha256 type" '
216 git --git-dir=repo-sha256/.git cat-file -t ${sha256_oid} >${name}_type3 &&
217 git --git-dir=repo-sha1/.git cat-file -t ${sha1_sha256_oid} >${name}_type4 &&
218 test_cmp ${name}_type3 ${name}_type4 &&
219 test_cmp ${name}_type3 ${name}_type_expected
222 test_expect_success $PREREQ "Verify ${name}'s sha1 size" '
223 git --git-dir=repo-sha1/.git cat-file -s ${sha1_oid} >${name}_size1 &&
224 git --git-dir=repo-sha256/.git cat-file -s ${sha256_sha1_oid} >${name}_size2 &&
225 test_cmp ${name}_size1 ${name}_size2
228 test_expect_success $PREREQ "Verify ${name}'s sha256 size" '
229 git --git-dir=repo-sha256/.git cat-file -s ${sha256_oid} >${name}_size3 &&
230 git --git-dir=repo-sha1/.git cat-file -s ${sha1_sha256_oid} >${name}_size4 &&
231 test_cmp ${name}_size3 ${name}_size4
234 test_expect_success $PREREQ "Verify ${name}'s sha1 pretty content" '
235 git --git-dir=repo-sha1/.git cat-file -p ${sha1_oid} >${name}_content1 &&
236 git --git-dir=repo-sha256/.git cat-file -p ${sha256_sha1_oid} >${name}_content2 &&
237 test_cmp ${name}_content1 ${name}_content2
240 test_expect_success $PREREQ "Verify ${name}'s sha256 pretty content" '
241 git --git-dir=repo-sha256/.git cat-file -p ${sha256_oid} >${name}_content3 &&
242 git --git-dir=repo-sha1/.git cat-file -p ${sha1_sha256_oid} >${name}_content4 &&
243 test_cmp ${name}_content3 ${name}_content4
246 test_expect_success $PREREQ "Verify ${name}'s sha1 content" '
247 git --git-dir=repo-sha1/.git cat-file ${type} ${sha1_oid} >${name}_content5 &&
248 git --git-dir=repo-sha256/.git cat-file ${type} ${sha256_sha1_oid} >${name}_content6 &&
249 test_cmp ${name}_content5 ${name}_content6
252 test_expect_success $PREREQ "Verify ${name}'s sha256 content" '
253 git --git-dir=repo-sha256/.git cat-file ${type} ${sha256_oid} >${name}_content7 &&
254 git --git-dir=repo-sha1/.git cat-file ${type} ${sha1_sha256_oid} >${name}_content8 &&
255 test_cmp ${name}_content7 ${name}_content8
259 compare_oids 'blob' hello "$hello_sha1_oid" "$hello_sha256_oid"
260 compare_oids 'tree' tree "$tree_sha1_oid" "$tree_sha256_oid"
261 compare_oids 'commit' commit "$commit_sha1_oid" "$commit_sha256_oid"
262 compare_oids GPG2 'commit' signedcommit "$signedcommit_sha1_oid" "$signedcommit_sha256_oid"
263 compare_oids 'tag' hellotag "$hellotag_sha1_oid" "$hellotag_sha256_oid"
264 compare_oids 'tag' treetag "$treetag_sha1_oid" "$treetag_sha256_oid"
265 compare_oids 'tag' committag "$committag_sha1_oid" "$committag_sha256_oid"
266 compare_oids GPG2 'tag' signedtag "$signedtag_sha1_oid" "$signedtag_sha256_oid"
268 compare_oids 'blob' more "$more_sha1_oid" "$more_sha256_oid"
269 compare_oids 'blob' another "$another_sha1_oid" "$another_sha256_oid"
270 compare_oids 'tree' tree2 "$tree2_sha1_oid" "$tree2_sha256_oid"
271 compare_oids 'commit' commit2 "$commit2_sha1_oid" "$commit2_sha256_oid"
272 compare_oids GPG2 'tag' signedtag2 "$signedtag2_sha1_oid" "$signedtag2_sha256_oid"
273 compare_oids GPG2 'commit' signedcommit2 "$signedcommit2_sha1_oid" "$signedcommit2_sha256_oid"
274 compare_oids GPG2 'commit' signedcommit3 "$signedcommit3_sha1_oid" "$signedcommit3_sha256_oid"
275 compare_oids GPG2 'commit' signedcommit4 "$signedcommit4_sha1_oid" "$signedcommit4_sha256_oid"
276 compare_oids GPG2 'tag' signedtag3 "$signedtag3_sha1_oid" "$signedtag3_sha256_oid"
277 compare_oids GPG2 'tag' signedtag4 "$signedtag4_sha1_oid" "$signedtag4_sha256_oid"
279 test_done