Skip to content

Commit 948f657

Browse files
authored
Merge pull request #14 from hyiso/fix/read-history-commits
fix: read history commits when body contains multi lines
2 parents bddeefb + 991972a commit 948f657

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

lib/src/read.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dart:io';
22
import 'package:path/path.dart';
33

4+
const _kDelimiter = '------------------------ >8 ------------------------';
5+
46
/// Read commit messages in given range([from], [to]),
57
/// or in [edit] file.
68
/// Return commit messages list.
@@ -17,7 +19,7 @@ Future<Iterable<String>> read({
1719
}
1820
final range = [if (from != null) from, to ?? 'HEAD'].join('..');
1921
return _getRangeCommits(
20-
gitLogArgs: ['--format=%B', range, ...?gitLogArgs],
22+
gitLogArgs: ['--format=%B%n$_kDelimiter', range, ...?gitLogArgs],
2123
workingDirectory: workingDirectory,
2224
);
2325
}
@@ -35,8 +37,8 @@ Future<Iterable<String>> _getRangeCommits({
3537
throw ProcessException(
3638
'git', ['log', ...gitLogArgs], result.stderr, result.exitCode);
3739
}
38-
return ((result.stdout as String).trim().split('\n'))
39-
.where((message) => message.trim().isNotEmpty)
40+
return ((result.stdout as String).split('$_kDelimiter\n'))
41+
.where((message) => message.isNotEmpty)
4042
.toList();
4143
}
4244

@@ -52,7 +54,8 @@ Future<Iterable<String>> _getEditingCommit({
5254
final root = result.stdout.toString().trim();
5355
final file = File(join(root, edit));
5456
if (await file.exists()) {
55-
return [await file.readAsString()];
57+
final message = await file.readAsString();
58+
return ['$message\n'];
5659
}
5760
return [];
5861
}

test/lint_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,31 @@ void main() {
9191
}),
9292
throwsRangeError);
9393
});
94+
95+
test('positive on multi-line body message', () async {
96+
final message = '''chore(deps): bump commitlint_cli from 0.5.0 to 0.6.0
97+
Bumps [commitlint_cli](https://github.com/hyiso/commitlint) from 0.5.0 to 0.6.0.
98+
- [Release notes](https://github.com/hyiso/commitlint/releases)
99+
- [Changelog](https://github.com/hyiso/commitlint/blob/main/CHANGELOG.md)
100+
- [Commits](hyiso/[email protected])
101+
102+
---
103+
updated-dependencies:
104+
- dependency-name: commitlint_cli
105+
dependency-type: direct:production
106+
update-type: version-update:semver-minor
107+
...
108+
109+
Signed-off-by: dependabot[bot] <[email protected]>
110+
111+
''';
112+
final result = await lint(message, {
113+
'type-empty': Rule(
114+
severity: RuleSeverity.error,
115+
condition: RuleCondition.never,
116+
),
117+
});
118+
expect(result.valid, true);
119+
expect(result.input, equals(message));
120+
});
94121
}

test/read_test.dart

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void main() {
1010
final dir = await git.bootstrap();
1111
await File(join(dir, 'commit-msg-file')).writeAsString('foo');
1212
final commits = await read(edit: 'commit-msg-file', workingDirectory: dir);
13-
expect(commits, equals(['foo']));
13+
expect(commits, equals(['foo\n']));
1414
});
1515

1616
test('get edit commit message from git root', () async {
@@ -19,7 +19,7 @@ void main() {
1919
await Process.run('git', ['add', '.'], workingDirectory: dir);
2020
await Process.run('git', ['commit', '-m', 'alpha'], workingDirectory: dir);
2121
final commits = await read(workingDirectory: dir);
22-
expect(commits, equals(['alpha']));
22+
expect(commits, equals(['alpha\n\n']));
2323
});
2424

2525
test('get history commit messages', () async {
@@ -31,7 +31,7 @@ void main() {
3131
await Process.run('git', ['commit', '-m', 'remove alpha'],
3232
workingDirectory: dir);
3333
final commits = await read(workingDirectory: dir);
34-
expect(commits, equals(['remove alpha', 'alpha']));
34+
expect(commits, equals(['remove alpha\n\n', 'alpha\n\n']));
3535
});
3636

3737
test('get edit commit message from git subdirectory', () async {
@@ -43,7 +43,7 @@ void main() {
4343
await Process.run('git', ['commit', '-m', 'beta'], workingDirectory: dir);
4444

4545
final commits = await read(workingDirectory: dir);
46-
expect(commits, equals(['beta']));
46+
expect(commits, equals(['beta\n\n']));
4747
});
4848

4949
test('get edit commit message while skipping first commit', () async {
@@ -65,6 +65,35 @@ void main() {
6565
from: 'HEAD~2',
6666
workingDirectory: dir,
6767
gitLogArgs: '--skip 1'.split(' '));
68-
expect(commits, equals(['beta']));
68+
expect(commits, equals(['beta\n\n']));
69+
});
70+
71+
test('get history commit messages - body contains multi lines', () async {
72+
final bodyMultiLineMessage =
73+
'''chore(deps): bump commitlint_cli from 0.5.0 to 0.6.0
74+
Bumps [commitlint_cli](https://github.com/hyiso/commitlint) from 0.5.0 to 0.6.0.
75+
- [Release notes](https://github.com/hyiso/commitlint/releases)
76+
- [Changelog](https://github.com/hyiso/commitlint/blob/main/CHANGELOG.md)
77+
- [Commits](hyiso/[email protected])
78+
79+
---
80+
updated-dependencies:
81+
- dependency-name: commitlint_cli
82+
dependency-type: direct:production
83+
update-type: version-update:semver-minor
84+
...
85+
86+
Signed-off-by: dependabot[bot] <[email protected]>''';
87+
final dir = await git.bootstrap();
88+
await File(join(dir, 'alpha.txt')).writeAsString('alpha');
89+
await Process.run('git', ['add', 'alpha.txt'], workingDirectory: dir);
90+
await Process.run('git', ['commit', '-m', 'alpha'], workingDirectory: dir);
91+
await File(join(dir, 'beta.txt')).writeAsString('beta');
92+
await Process.run('git', ['add', 'beta.txt'], workingDirectory: dir);
93+
await Process.run('git', ['commit', '-m', bodyMultiLineMessage],
94+
workingDirectory: dir);
95+
96+
final commits = await read(from: 'HEAD~1', workingDirectory: dir);
97+
expect(commits, equals(['$bodyMultiLineMessage\n\n']));
6998
});
7099
}

0 commit comments

Comments
 (0)