getReferences function

List<CommitReference> getReferences(
  1. String input, {
  2. required Pattern referencesPattern,
  3. required Pattern referencePartsPattern,
})

Implementation

List<CommitReference> getReferences(
  String input, {
  required Pattern referencesPattern,
  required Pattern referencePartsPattern,
}) {
  final references = <CommitReference>[];
  Match? referenceSentences;
  Match? referenceMatch;

  final reApplicable = referencesPattern.allMatches(input).isNotEmpty
      ? referencesPattern
      : _kMatchAll;
  while ((referenceSentences =
          reApplicable.matchAsPrefix(input, referenceSentences?.end ?? 0)) !=
      null) {
    final action = referenceSentences!.group(1)!;
    final sentence = referenceSentences.group(2)!;
    while ((referenceMatch = referencePartsPattern.matchAsPrefix(
            sentence, referenceMatch?.end ?? 0)) !=
        null) {
      String? owner;
      String? repository = referenceMatch!.group(1);
      final ownerRepo = repository?.split('/') ?? [];

      if (ownerRepo.length > 1) {
        owner = ownerRepo.removeAt(0);
        repository = ownerRepo.join('/');
      }
      references.add(CommitReference(
        raw: referenceMatch.group(0)!,
        action: action,
        owner: owner,
        repository: repository,
        issue: referenceMatch.group(3),
        prefix: referenceMatch.group(2)!,
      ));
    }
  }
  return references;
}