diff --git a/lib/src/read.dart b/lib/src/read.dart index a220fbc..cc8ac7a 100644 --- a/lib/src/read.dart +++ b/lib/src/read.dart @@ -1,6 +1,8 @@ import 'dart:io'; import 'package:path/path.dart'; +const _kDelimiter = '------------------------ >8 ------------------------'; + /// Read commit messages in given range([from], [to]), /// or in [edit] file. /// Return commit messages list. @@ -17,7 +19,7 @@ Future> read({ } final range = [if (from != null) from, to ?? 'HEAD'].join('..'); return _getRangeCommits( - gitLogArgs: ['--format=%B', range, ...?gitLogArgs], + gitLogArgs: ['--format=%B%n$_kDelimiter', range, ...?gitLogArgs], workingDirectory: workingDirectory, ); } @@ -35,8 +37,8 @@ Future> _getRangeCommits({ throw ProcessException( 'git', ['log', ...gitLogArgs], result.stderr, result.exitCode); } - return ((result.stdout as String).trim().split('\n')) - .where((message) => message.trim().isNotEmpty) + return ((result.stdout as String).split('$_kDelimiter\n')) + .where((message) => message.isNotEmpty) .toList(); } @@ -52,7 +54,8 @@ Future> _getEditingCommit({ final root = result.stdout.toString().trim(); final file = File(join(root, edit)); if (await file.exists()) { - return [await file.readAsString()]; + final message = await file.readAsString(); + return ['$message\n']; } return []; } diff --git a/test/lint_test.dart b/test/lint_test.dart index debac20..620ea13 100644 --- a/test/lint_test.dart +++ b/test/lint_test.dart @@ -91,4 +91,31 @@ void main() { }), throwsRangeError); }); + + test('positive on multi-line body message', () async { + final message = '''chore(deps): bump commitlint_cli from 0.5.0 to 0.6.0 +Bumps [commitlint_cli](https://github.com/hyiso/commitlint) from 0.5.0 to 0.6.0. +- [Release notes](https://github.com/hyiso/commitlint/releases) +- [Changelog](https://github.com/hyiso/commitlint/blob/main/CHANGELOG.md) +- [Commits](hyiso/commitlint@v0.5.0...v0.6.0) + +--- +updated-dependencies: +- dependency-name: commitlint_cli + dependency-type: direct:production + update-type: version-update:semver-minor +... + +Signed-off-by: dependabot[bot] + +'''; + final result = await lint(message, { + 'type-empty': Rule( + severity: RuleSeverity.error, + condition: RuleCondition.never, + ), + }); + expect(result.valid, true); + expect(result.input, equals(message)); + }); } diff --git a/test/read_test.dart b/test/read_test.dart index 1969af7..2f44934 100644 --- a/test/read_test.dart +++ b/test/read_test.dart @@ -10,7 +10,7 @@ void main() { final dir = await git.bootstrap(); await File(join(dir, 'commit-msg-file')).writeAsString('foo'); final commits = await read(edit: 'commit-msg-file', workingDirectory: dir); - expect(commits, equals(['foo'])); + expect(commits, equals(['foo\n'])); }); test('get edit commit message from git root', () async { @@ -19,7 +19,7 @@ void main() { await Process.run('git', ['add', '.'], workingDirectory: dir); await Process.run('git', ['commit', '-m', 'alpha'], workingDirectory: dir); final commits = await read(workingDirectory: dir); - expect(commits, equals(['alpha'])); + expect(commits, equals(['alpha\n\n'])); }); test('get history commit messages', () async { @@ -31,7 +31,7 @@ void main() { await Process.run('git', ['commit', '-m', 'remove alpha'], workingDirectory: dir); final commits = await read(workingDirectory: dir); - expect(commits, equals(['remove alpha', 'alpha'])); + expect(commits, equals(['remove alpha\n\n', 'alpha\n\n'])); }); test('get edit commit message from git subdirectory', () async { @@ -43,7 +43,7 @@ void main() { await Process.run('git', ['commit', '-m', 'beta'], workingDirectory: dir); final commits = await read(workingDirectory: dir); - expect(commits, equals(['beta'])); + expect(commits, equals(['beta\n\n'])); }); test('get edit commit message while skipping first commit', () async { @@ -65,6 +65,35 @@ void main() { from: 'HEAD~2', workingDirectory: dir, gitLogArgs: '--skip 1'.split(' ')); - expect(commits, equals(['beta'])); + expect(commits, equals(['beta\n\n'])); + }); + + test('get history commit messages - body contains multi lines', () async { + final bodyMultiLineMessage = + '''chore(deps): bump commitlint_cli from 0.5.0 to 0.6.0 +Bumps [commitlint_cli](https://github.com/hyiso/commitlint) from 0.5.0 to 0.6.0. +- [Release notes](https://github.com/hyiso/commitlint/releases) +- [Changelog](https://github.com/hyiso/commitlint/blob/main/CHANGELOG.md) +- [Commits](hyiso/commitlint@v0.5.0...v0.6.0) + +--- +updated-dependencies: +- dependency-name: commitlint_cli + dependency-type: direct:production + update-type: version-update:semver-minor +... + +Signed-off-by: dependabot[bot] '''; + final dir = await git.bootstrap(); + await File(join(dir, 'alpha.txt')).writeAsString('alpha'); + await Process.run('git', ['add', 'alpha.txt'], workingDirectory: dir); + await Process.run('git', ['commit', '-m', 'alpha'], workingDirectory: dir); + await File(join(dir, 'beta.txt')).writeAsString('beta'); + await Process.run('git', ['add', 'beta.txt'], workingDirectory: dir); + await Process.run('git', ['commit', '-m', bodyMultiLineMessage], + workingDirectory: dir); + + final commits = await read(from: 'HEAD~1', workingDirectory: dir); + expect(commits, equals(['$bodyMultiLineMessage\n\n'])); }); }