Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read history commits when body contains multi lines #14

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
11 changes: 7 additions & 4 deletions lib/src/read.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,7 +19,7 @@ Future<Iterable<String>> 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,
);
}
Expand All @@ -35,8 +37,8 @@ Future<Iterable<String>> _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();
}

Expand All @@ -52,7 +54,8 @@ Future<Iterable<String>> _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 [];
}
27 changes: 27 additions & 0 deletions test/lint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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] <support@github.com>

''';
final result = await lint(message, {
'type-empty': Rule(
severity: RuleSeverity.error,
condition: RuleCondition.never,
),
});
expect(result.valid, true);
expect(result.input, equals(message));
});
}
39 changes: 34 additions & 5 deletions test/read_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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] <support@github.com>''';
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']));
});
}