parse function
Commit
parse(
- String raw, {
- String headerPattern = _kDefaultHeaderPattern,
- List<
String> headerCorrespondence = _kDefaultHeaderCorrespondence, - List<
String> referenceActions = _kDefaultReferenceActions, - List<
String> issuePrefixes = _kDefaultIssuePrefixes, - List<
String> noteKeywords = _kDefaultNoteKeywords, - String fieldPattern = _kDefaultFieldPattern,
- String revertPattern = _kDefaultRevertPattern,
- List<
String> revertCorrespondence = _kDefaultRevertCorrespondence, - String? commentChar,
Implementation
Commit parse(String raw,
{String headerPattern = _kDefaultHeaderPattern,
List<String> headerCorrespondence = _kDefaultHeaderCorrespondence,
List<String> referenceActions = _kDefaultReferenceActions,
List<String> issuePrefixes = _kDefaultIssuePrefixes,
List<String> noteKeywords = _kDefaultNoteKeywords,
String fieldPattern = _kDefaultFieldPattern,
String revertPattern = _kDefaultRevertPattern,
List<String> revertCorrespondence = _kDefaultRevertCorrespondence,
String? commentChar}) {
final message = raw.trim();
if (message.isEmpty) {
throw ArgumentError.value(raw, 'raw message', 'must have content.');
}
String? body;
String? footer;
List<String> mentions = [];
List<CommitNote> notes = [];
List<CommitReference> references = [];
Map<String, String?>? revert;
final lines = truncateToScissor(message.split(RegExp(r'\r?\n')))
.where(_gpgFilter)
.toList();
if (commentChar != null) {
lines.removeWhere((line) => line.startsWith(commentChar));
}
final header = lines.removeAt(0);
final headerMatch = RegExp(headerPattern).matchAsPrefix(header);
final headerParts = <String, String?>{};
if (headerMatch != null) {
for (var i = 0; i < headerCorrespondence.length; i++) {
headerParts[headerCorrespondence[i]] = headerMatch.group(i + 1);
}
}
final referencesPattern = getReferenceRegex(referenceActions);
final referencePartsPattern = getReferencePartsRegex(issuePrefixes, false);
references.addAll(getReferences(header,
referencesPattern: referencesPattern,
referencePartsPattern: referencePartsPattern));
bool continueNote = false;
bool isBody = true;
final notesPattern = getNotesRegex(noteKeywords);
/// body or footer
for (var line in lines) {
bool referenceMatched = false;
final notesMatch = notesPattern.matchAsPrefix(line);
if (notesMatch != null) {
continueNote = true;
isBody = false;
footer = append(footer, line);
notes.add(CommitNote(
title: notesMatch.group(1)!, text: notesMatch.group(2)!.trim()));
break;
}
final lineReferences = getReferences(
line,
referencesPattern: referencesPattern,
referencePartsPattern: referencePartsPattern,
);
if (lineReferences.isNotEmpty) {
isBody = false;
referenceMatched = true;
continueNote = false;
}
references.addAll(lineReferences);
if (referenceMatched) {
footer = append(footer, line);
break;
}
if (continueNote) {
notes.last.text = append(notes.last.text, line).trim();
footer = append(footer, line);
break;
}
if (isBody) {
body = append(body, line);
} else {
footer = append(footer, line);
}
}
Match? mentionsMatch;
final mentionsPattern = RegExp(r'@([\w-]+)');
while ((mentionsMatch =
mentionsPattern.matchAsPrefix(raw, mentionsMatch?.end ?? 0)) !=
null) {
mentions.add(mentionsMatch!.group(1)!);
}
// does this commit revert any other commit?
final revertMatch = raw.matchAsPrefix(revertPattern);
if (revertMatch != null) {
revert = {};
for (var i = 0; i < revertCorrespondence.length; i++) {
revert[revertCorrespondence[i]] = revertMatch.group(i + 1);
}
}
return Commit(
header: header,
type: headerParts['type'],
scope: headerParts['scope'],
subject: headerParts['subject'],
body: body?.trim(),
footer: footer?.trim(),
notes: notes,
references: references,
mentions: mentions,
revert: revert,
);
}