<commit_before>namespace GitVersion
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using GitVersion.Helpers;

    using JetBrains.Annotations;

    using LibGit2Sharp;

    static class LibGitExtensions
    {
        public static DateTimeOffset When(this Commit commit)
        {
            return commit.Committer.When;
        }

        public static Branch FindBranch(this IRepository repository, string branchName)
        {
            var exact = repository.Branches.FirstOrDefault(x => x.Name == branchName);
            if (exact != null)
            {
                return exact;
            }

            return repository.Branches.FirstOrDefault(x => x.Name == "origin/" + branchName);
        }

        public static SemanticVersion LastVersionTagOnBranch(this Branch branch, IRepository repository, string tagPrefixRegex)
        {
            var tags = repository.Tags.Select(t => t).ToList();

            return repository.Commits.QueryBy(new CommitFilter
            {
                Since = branch.Tip
            })
            .SelectMany(c => tags.Where(t => c.Sha == t.Target.Sha).SelectMany(t =>
            {
                SemanticVersion semver;
                if (SemanticVersion.TryParse(t.Name, tagPrefixRegex, out semver))
                    return new [] { semver };
                return new SemanticVersion[0];
            }))
            .FirstOrDefault();
        }


        public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
        {
            if (branch == null)
            {
                throw new ArgumentNullException("branch");
            }

            using (Logger.IndentLog("Finding branch source"))
            {
                var otherBranches = repository.Branches.Except(excludedBranches).Where(b => IsSameBranch(branch, b)).ToList();
                var mergeBases = otherBranches.Select(b =>
                {
                    var otherCommit = b.Tip;
                    if (b.Tip.Parents.Contains(branch.Tip))
                    {
                        otherCommit = b.Tip.Parents.First();
                    }
                    var mergeBase = repository.Commits.FindMergeBase(otherCommit, branch.Tip);
                    return mergeBase;
                }).Where(b => b != null).ToList();
                return mergeBases.OrderByDescending(b => b.Committer.When).FirstOrDefault();
            }
        }

        static bool IsSameBranch(Branch branch, Branch b)
        {
            return (b.IsRemote ? b.Name.Replace(b.Remote.Name + "/", string.Empty) : b.Name) != branch.Name;
        }

        public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)
        {
            var directBranchHasBeenFound = false;
            foreach (var branch in repository.Branches)
            {
                if (branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
                {
                    continue;
                }

                directBranchHasBeenFound = true;
                yield return branch;
            }

            if (directBranchHasBeenFound)<commit_msg>Ensure that the branch Tip is not null and throw a more intuitive exception than `NulReferenceException` if it is.<commit_after>namespace GitVersion
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using GitVersion.Helpers;

    using JetBrains.Annotations;

    using LibGit2Sharp;

    static class LibGitExtensions
    {
        public static DateTimeOffset When(this Commit commit)
        {
            return commit.Committer.When;
        }

        public static Branch FindBranch(this IRepository repository, string branchName)
        {
            var exact = repository.Branches.FirstOrDefault(x => x.Name == branchName);
            if (exact != null)
            {
                return exact;
            }

            return repository.Branches.FirstOrDefault(x => x.Name == "origin/" + branchName);
        }

        public static SemanticVersion LastVersionTagOnBranch(this Branch branch, IRepository repository, string tagPrefixRegex)
        {
            var tags = repository.Tags.Select(t => t).ToList();

            return repository.Commits.QueryBy(new CommitFilter
            {
                Since = branch.Tip
            })
            .SelectMany(c => tags.Where(t => c.Sha == t.Target.Sha).SelectMany(t =>
            {
                SemanticVersion semver;
                if (SemanticVersion.TryParse(t.Name, tagPrefixRegex, out semver))
                    return new [] { semver };
                return new SemanticVersion[0];
            }))
            .FirstOrDefault();
        }


        public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
        {
            const string missingTipFormat = "{0} has no tip. Please see http://example.com/docs for information on how to fix this.";

            if (branch == null)
            {
                throw new ArgumentNullException("branch");
            }

            if (branch.Tip == null)
            {
                throw new ArgumentException(String.Format(missingTipFormat, branch.Name));
            }

            using (Logger.IndentLog("Finding branch source"))
            {
                var otherBranches = repository.Branches.Except(excludedBranches).Where(b => IsSameBranch(branch, b)).ToList();
                var mergeBases = otherBranches.Select(b =>
                {
                    if (b.Tip == null)
                    {
                        throw new InvalidOperationException(String.Format(missingTipFormat, b.Name));
                    }

                    var otherCommit = b.Tip;
                    if (b.Tip.Parents.Contains(branch.Tip))
                    {
                        otherCommit = b.Tip.Parents.First();
                    }
                    var mergeBase = repository.Commits.FindMergeBase(otherCommit, branch.Tip);
                    return mergeBase;
                }).Where(b => b != null).ToList();
                return mergeBases.OrderByDescending(b => b.Committer.When).FirstOrDefault();
            }
        }


        static bool IsSameBranch(Branch branch, Branch b)
        {
            return (b.IsRemote ? b.Name.Replace(b.Remote.Name + "/", string.Empty) : b.Name) != branch.Name;
        }

        public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)
        {
            var directBranchHasBeenFound = false;
            foreach (var branch in repository.Branches)
            {
                if (branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
                {
                    continue;
                }

                directBranchHasBeenFound = true;
                yield return branch;
            }

            if (directBranchHasBeenFound)
{'fraction_non_alphanumeric': 0.0894208281744394, 'fraction_numerical': 0.0006878525244187646, 'mean_word_length': 1.6552227903579255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2477946', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1773, 'n_words': 410}