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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 16, 2025 at 00:50 UTC b5643b60acb71e3c117558b37020a8db8fe17c69
4 files changed +38 -5
t/lib-loose.sh new
+30
@@ -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
+3 -2
@@ -3,6 +3,7 @@
3 test_description='git cat-file'
4
5 . ./test-lib.sh
6 +. "$TEST_DIRECTORY/lib-loose.sh"
7
8 test_cmdmode_usage () {
9 test_expect_code 129 "$@" 2>err &&
@@ -657,12 +658,12 @@ test_expect_success 'setup bogus data' '
658 bogus_short_type="bogus" &&
659 bogus_short_content="bogus" &&
660 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) &&
662
663 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
664 bogus_long_content="bogus" &&
665 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)
667 '
668
669 for arg1 in -s -t -p
t/t1450-fsck.sh
+2 -1
@@ -7,6 +7,7 @@ test_description='git fsck random collection of tests
7 '
8
9 . ./test-lib.sh
10 +. "$TEST_DIRECTORY/lib-loose.sh"
11
12 test_expect_success setup '
13 git config gc.auto 0 &&
@@ -973,7 +974,7 @@ test_expect_success 'fsck error and recovery on invalid object type' '
974 (
975 cd garbage-type &&
976
976 - garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
977 + garbage_blob=$(loose_obj objects garbage </dev/null) &&
978
979 test_must_fail git fsck 2>err &&
980 grep -e "^error" -e "^fatal" err >errors &&
t/t1512-rev-parse-disambiguation.sh
+3 -2
@@ -24,6 +24,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
25
26 . ./test-lib.sh
27 +. "$TEST_DIRECTORY/lib-loose.sh"
28
29 test_cmp_failed_rev_parse () {
30 dir=$1
@@ -67,8 +68,8 @@ test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' '
68 cd blob.bad &&
69
70 # 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
73 ) &&
74
75 test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF