From 38b53e70bd38a599891525ac48f4c4373a04ff11 Mon Sep 17 00:00:00 2001 From: Amal Saidov Date: Mon, 23 Nov 2026 13:15:33 +0700 Subject: [PATCH] fix(conventional_commit): preserve body text matching a footer The body was computed by stripping each extracted footer from the body with `String.replaceAll`, which removes every occurrence of that text anywhere in the body, not just the trailing footer line. When a body paragraph legitimately contains the same text as a footer (e.g. an inline "Refs #123" referenced in prose and again as a trailer), the inline occurrence was silently deleted, corrupting the parsed body. Remove footer lines by matching whole lines instead, so only the actual footer lines are stripped and body prose is left intact. --- .../lib/conventional_commit.dart | 17 ++++++++++------- .../test/conventional_commit_test.dart | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/conventional_commit/lib/conventional_commit.dart b/packages/conventional_commit/lib/conventional_commit.dart index 323d58b2e..617f4e28a 100644 --- a/packages/conventional_commit/lib/conventional_commit.dart +++ b/packages/conventional_commit/lib/conventional_commit.dart @@ -120,13 +120,16 @@ class ConventionalCommit { .toList(); // We assume that anything left over in the commit message after removing - // the header and footers is the body. This is a quick way to support - // multi-paragraph multi-line bodies. - String? body = commitWithoutHeader; - for (final footer in footers) { - body = body!.replaceAll(footer, ''); - } - body = body!.trim(); + // the header and footer lines is the body. This is a quick way to support + // multi-paragraph multi-line bodies. Footer lines are removed by matching + // whole lines rather than by substring replacement, so that body prose + // which happens to contain the same text as a footer is not corrupted. + final footerLines = footers.toSet(); + String? body = commitWithoutHeader + .split('\n') + .where((line) => !footerLines.contains(line.trim())) + .join('\n') + .trim(); if (body.isEmpty) { // Should be null if no body specified, or empty in this case. body = null; diff --git a/packages/conventional_commit/test/conventional_commit_test.dart b/packages/conventional_commit/test/conventional_commit_test.dart index c27c68cd8..889f8cfc5 100644 --- a/packages/conventional_commit/test/conventional_commit_test.dart +++ b/packages/conventional_commit/test/conventional_commit_test.dart @@ -413,6 +413,25 @@ void main() { ); }); + test('body is not corrupted when it contains text matching a footer', () { + const commitMessage = ''' +fix: resolve timeout + +The old Refs #123 approach was wrong and needed a rewrite. + +Refs #123 +'''; + final commit = ConventionalCommit.tryParse(commitMessage)!; + // The footer line should be extracted. + expect(commit.footers, equals(['Refs #123'])); + // The inline occurrence of "Refs #123" inside the body paragraph must be + // preserved; only the footer line should be stripped from the body. + expect( + commit.body, + equals('The old Refs #123 approach was wrong and needed a rewrite.'), + ); + }); + test('footers', () { // With a multi-line/paragraph body. final commitWithBodyParsed = ConventionalCommit.tryParse(