Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/conventional_commit/lib/conventional_commit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions packages/conventional_commit/test/conventional_commit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading