<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RepositoryVersionInformationLoader.cs" company="Appccelerate">
//   Copyright (c) 2008-2014
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Appccelerate.Version
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using LibGit2Sharp;

    public class RepositoryVersionInformationLoader
    {
        public RepositoryVersionInformation GetRepositoryVersionInformation(string startingPath)
        {
            string repositoryPath = Repository.Discover(startingPath);

            var repository = new Repository(repositoryPath);

            return this.GetRepositoryVersionInformation(repository);
        }

        public RepositoryVersionInformation GetRepositoryVersionInformation(Repository repository)
        {
            IEnumerable<Tag> allVersionTags = FindAllVersionTags(repository).ToArray();
            Commit lastTaggedCommit = FindLastVersionTaggedCommit(repository, allVersionTags);
            Tag latestVersionTag = FindLatestVersionTag(allVersionTags, lastTaggedCommit);
            int commits = CountCommitsSinceVersionTag(repository, lastTaggedCommit);
            string prereleaseOverride = DeterminePrereleaseOverride(repository);

            return new RepositoryVersionInformation(
                latestVersionTag.Name.Substring(2),
                commits,
                latestVersionTag.IsAnnotated ? latestVersionTag.Annotation.Message.Replace("\n", string.Empty).Replace("\r", string.Empty) : string.Empty,
                prereleaseOverride);
        }

        private static Commit FindLastVersionTaggedCommit(Repository repository, IEnumerable<Tag> allVersionTags)
        {
            Commit lastTaggedCommit =
                repository.Head.Commits.FirstOrDefault(commit => allVersionTags.Any(tag => tag.Target == commit));

            if (lastTaggedCommit == null)
            {
                throw new InvalidOperationException("No version tag found. Add a tag with name 'v=<version pattern>'");
            }<commit_msg>An InvalidOperationException is thrown when the startingPath is not inside a git repository<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RepositoryVersionInformationLoader.cs" company="Appccelerate">
//   Copyright (c) 2008-2014
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Appccelerate.Version
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using LibGit2Sharp;

    public class RepositoryVersionInformationLoader
    {
        public RepositoryVersionInformation GetRepositoryVersionInformation(string startingPath)
        {
            string repositoryPath = Repository.Discover(startingPath);

            if (string.IsNullOrEmpty(repositoryPath))
            {
                throw new InvalidOperationException("The path is not part of a git repository: " + startingPath);
            }

            var repository = new Repository(repositoryPath);

            return this.GetRepositoryVersionInformation(repository);
        }

        public RepositoryVersionInformation GetRepositoryVersionInformation(Repository repository)
        {
            IEnumerable<Tag> allVersionTags = FindAllVersionTags(repository).ToArray();
            Commit lastTaggedCommit = FindLastVersionTaggedCommit(repository, allVersionTags);
            Tag latestVersionTag = FindLatestVersionTag(allVersionTags, lastTaggedCommit);
            int commits = CountCommitsSinceVersionTag(repository, lastTaggedCommit);
            string prereleaseOverride = DeterminePrereleaseOverride(repository);

            return new RepositoryVersionInformation(
                latestVersionTag.Name.Substring(2),
                commits,
                latestVersionTag.IsAnnotated ? latestVersionTag.Annotation.Message.Replace("\n", string.Empty).Replace("\r", string.Empty) : string.Empty,
                prereleaseOverride);
        }

        private static Commit FindLastVersionTaggedCommit(Repository repository, IEnumerable<Tag> allVersionTags)
        {
            Commit lastTaggedCommit =
                repository.Head.Commits.FirstOrDefault(commit => allVersionTags.Any(tag => tag.Target == commit));

            if (lastTaggedCommit == null)
            {
                throw new InvalidOperationException("No version tag found. Add a tag with name 'v=<version pattern>'");
            }
{'fraction_non_alphanumeric': 0.15012594458438286, 'fraction_numerical': 0.004701931150293871, 'mean_word_length': 2.1833244254409405, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5147343', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1281, 'n_words': 374}