Skip to content

Commit b5643b6

Browse files
peffgitster
authored andcommitted
t: add lib-loose.sh
This commit adds a shell library for writing raw loose objects into the object database. Normally this is done with hash-object, but the specific intent here is to allow broken objects that hash-object may not support. We'll convert several cases that use "hash-object --literally" to write objects with invalid types. That works currently, but dropping this dependency will allow us to remove that feature and simplify the object-writing code. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f2ed511 commit b5643b6

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

t/lib-loose.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Support routines for hand-crafting loose objects.
2+
3+
# Write a loose object into the odb at $1, with object type $2 and contents
4+
# from stdin. Writes the oid to stdout. Example:
5+
#
6+
# oid=$(echo foo | loose_obj .git/objects blob)
7+
#
8+
loose_obj () {
9+
cat >tmp_loose.content &&
10+
size=$(wc -c <tmp_loose.content) &&
11+
{
12+
# Do not quote $size here; we want the shell
13+
# to strip whitespace that "wc" adds on some platforms.
14+
printf "%s %s\0" "$2" $size &&
15+
cat tmp_loose.content
16+
} >tmp_loose.raw &&
17+
18+
oid=$(test-tool $test_hash_algo <tmp_loose.raw) &&
19+
suffix=${oid#??} &&
20+
prefix=${oid%$suffix} &&
21+
dir=$1/$prefix &&
22+
file=$dir/$suffix &&
23+
24+
test-tool zlib deflate <tmp_loose.raw >tmp_loose.zlib &&
25+
mkdir -p "$dir" &&
26+
mv tmp_loose.zlib "$file" &&
27+
28+
rm tmp_loose.raw tmp_loose.content &&
29+
echo "$oid"
30+
}

t/t1006-cat-file.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='git cat-file'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY/lib-loose.sh"
67

78
test_cmdmode_usage () {
89
test_expect_code 129 "$@" 2>err &&
@@ -657,12 +658,12 @@ test_expect_success 'setup bogus data' '
657658
bogus_short_type="bogus" &&
658659
bogus_short_content="bogus" &&
659660
bogus_short_size=$(strlen "$bogus_short_content") &&
660-
bogus_short_oid=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
661+
bogus_short_oid=$(echo_without_newline "$bogus_short_content" | loose_obj .git/objects $bogus_short_type) &&
661662
662663
bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
663664
bogus_long_content="bogus" &&
664665
bogus_long_size=$(strlen "$bogus_long_content") &&
665-
bogus_long_oid=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
666+
bogus_long_oid=$(echo_without_newline "$bogus_long_content" | loose_obj .git/objects $bogus_long_type)
666667
'
667668

668669
for arg1 in -s -t -p

t/t1450-fsck.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test_description='git fsck random collection of tests
77
'
88

99
. ./test-lib.sh
10+
. "$TEST_DIRECTORY/lib-loose.sh"
1011

1112
test_expect_success setup '
1213
git config gc.auto 0 &&
@@ -973,7 +974,7 @@ test_expect_success 'fsck error and recovery on invalid object type' '
973974
(
974975
cd garbage-type &&
975976
976-
garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
977+
garbage_blob=$(loose_obj objects garbage </dev/null) &&
977978
978979
test_must_fail git fsck 2>err &&
979980
grep -e "^error" -e "^fatal" err >errors &&

t/t1512-rev-parse-disambiguation.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
2424
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
2525

2626
. ./test-lib.sh
27+
. "$TEST_DIRECTORY/lib-loose.sh"
2728

2829
test_cmp_failed_rev_parse () {
2930
dir=$1
@@ -67,8 +68,8 @@ test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' '
6768
cd blob.bad &&
6869
6970
# Both have the prefix "bad0"
70-
echo xyzfaowcoh | git hash-object -t bad -w --stdin --literally &&
71-
echo xyzhjpyvwl | git hash-object -t bad -w --stdin --literally
71+
echo xyzfaowcoh | loose_obj objects bad &&
72+
echo xyzhjpyvwl | loose_obj objects bad
7273
) &&
7374
7475
test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF

0 commit comments

Comments
 (0)