]> git.ipfire.org Git - thirdparty/git.git/commitdiff
test-lib-functions: add commit_body helper
authorShlok Kulshreshtha <diy2903@gmail.com>
Mon, 27 Jul 2026 09:56:55 +0000 (15:26 +0530)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Jul 2026 19:58:37 +0000 (12:58 -0700)
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>
t/README
t/test-lib-functions.sh

index 4252774f86c8ce89ed80f15fcea29df703ff3a10..9a9daaf2afe5e24ddc9d95ce691ce37bbf3ac4f0 100644 (file)
--- a/t/README
+++ b/t/README
@@ -945,6 +945,17 @@ see test-lib-functions.sh for the full list and their options.
    Merges the given rev using the given message.  Like test_commit,
    creates a tag and calls test_tick before committing.
 
+ - commit_body <rev>
+
+   Print the message body of <rev>, i.e. the contents of its commit
+   object with the header removed.  Use this instead of piping
+   "git cat-file commit" into "sed", which would hide a failure of
+   the git command.
+
+   Example:
+
+       commit_body HEAD >actual
+
  - test_set_prereq <prereq>
 
    Set a test prerequisite to be used later with test_have_prereq. The
index 809c6621241944fe9ea8fb042682bd086855eded..03bf31d8efea302d4150124f19355bf49895a9b3 100644 (file)
@@ -1433,6 +1433,14 @@ test_commit_message () {
        test_cmp "$msg_file" actual.msg
 }
 
+# Print the message body of a commit
+# Usage: commit_body <rev>
+commit_body () {
+       git cat-file commit "$1" >.commit &&
+       sed -e "1,/^$/d" .commit &&
+       rm -f .commit
+}
+
 # Compare paths respecting core.ignoreCase
 test_cmp_fspath () {
        if test "x$1" = "x$2"