test-lib-functions: add commit_body helper
Extracting the message body of a commit -- running "git cat-file commit" and stripping everything up to and including the first blank line with "sed" -- is spelled out in about 60 places across the test suite. Add a helper for it, so that the operation is written once instead of being copied around. The commit object goes to a temporary file rather than into a pipe, because a pipeline reports only its last command's exit status, so a failure of "git cat-file" would go unnoticed. Signed-off-by: Shlok Kulshreshtha <diy2903@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Shlok Kulshreshtha committed
Jul 27, 2026 at 15:26 UTC
a592d6feb3d47a1fcd6be5b4c8136e116c26ffbc
2 files changed
+19
t/README
+11
@@ -945,6 +945,17 @@ see test-lib-functions.sh for the full list and their options.
945
Merges the given rev using the given message. Like test_commit,
946
creates a tag and calls test_tick before committing.
947
948
+ - commit_body <rev>
949
+
950
+ Print the message body of <rev>, i.e. the contents of its commit
951
+ object with the header removed. Use this instead of piping
952
+ "git cat-file commit" into "sed", which would hide a failure of
953
+ the git command.
954
+
955
+ Example:
956
+
957
+ commit_body HEAD >actual
958
+
959
- test_set_prereq <prereq>
960
961
Set a test prerequisite to be used later with test_have_prereq. The
t/test-lib-functions.sh
+8
@@ -1433,6 +1433,14 @@ test_commit_message () {
1433
test_cmp "$msg_file" actual.msg
1434
}
1435
1436
+# Print the message body of a commit
1437
+# Usage: commit_body <rev>
1438
+commit_body () {
1439
+ git cat-file commit "$1" >.commit &&
1440
+ sed -e "1,/^$/d" .commit &&
1441
+ rm -f .commit
1442
+}
1443
+
1444
# Compare paths respecting core.ignoreCase
1445
test_cmp_fspath () {
1446
if test "x$1" = "x$2"