public IEnumerable <ExternalLink> Parse(GitRevision revision, ExternalLinkDefinition definition)
        {
            var remoteMatches = ParseRemotes(definition);

            return(remoteMatches.SelectMany(remoteMatch => ParseRevision(revision, definition, remoteMatch)));
        }
        private static IEnumerable <ExternalLink> ParseRevision(GitRevision revision, ExternalLinkDefinition definition, Match remoteMatch)
        {
            var links = new List <IEnumerable <ExternalLink> >();

            if (definition.SearchInParts.Contains(ExternalLinkDefinition.RevisionPart.LocalBranches))
            {
                links.AddRange(
                    revision.Refs
                    .Where(b => !b.IsRemote)
                    .Select(head => ParseRevisionPart(revision, definition, remoteMatch, head.LocalName)));
            }

            if (definition.SearchInParts.Contains(ExternalLinkDefinition.RevisionPart.RemoteBranches))
            {
                links.AddRange(
                    revision.Refs
                    .Where(b => b.IsRemote)
                    .Select(head => ParseRevisionPart(revision, definition, remoteMatch, head.LocalName)));
            }

            if (definition.SearchInParts.Contains(ExternalLinkDefinition.RevisionPart.Message))
            {
                links.Add(ParseRevisionPart(revision, definition, remoteMatch, revision.Body));
            }

            return(links.SelectMany(list => list));
        }
        private static IEnumerable <ExternalLink> ParseRevisionPart(GitRevision revision, ExternalLinkDefinition definition, Match remoteMatch, string part)
        {
            if (string.IsNullOrEmpty(definition.SearchPattern) || definition.SearchPatternRegex.Value == null || part == null)
            {
                yield break;
            }

            var allMatches = new List <Match>();

            MatchCollection matches = definition.SearchPatternRegex.Value.Matches(part);

            for (var i = 0; i < matches.Count; i++)
            {
                var match = matches[i];
                if (match.Success)
                {
                    if (string.IsNullOrEmpty(definition.NestedSearchPattern))
                    {
                        allMatches.Add(match);
                    }
                    else if (definition.NestedSearchPatternRegex.Value != null)
                    {
                        MatchCollection nestedMatches = definition.NestedSearchPatternRegex.Value.Matches(match.Value);

                        for (var n = 0; n < nestedMatches.Count; n++)
                        {
                            allMatches.Add(nestedMatches[n]);
                        }
                    }
                }
            }

            foreach (var match in allMatches.Where(m => m.Success))
            {
                foreach (var format in definition.LinkFormats)
                {
                    yield return(format.Apply(remoteMatch, match, revision));
                }
            }
        }