<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

namespace ITGlobal.MarkDocs.Git
{
    internal sealed class GitHelper
    {
        private const string EXECUTABLE_NAME = "git";

        private readonly ILogger _log;

        public GitHelper(ILogger log)
        {
            _log = log;
        }

        public bool IsGitInstalled
        {
            get
            {
                try
                {
                    using (var exec = Execute(null, "version"))
                    {
                        exec.ThrowIfFailed();

                        var version = exec.StandardOutput.FirstOrDefault();
                        _log.LogDebug("Git is installed: {0}", version);

                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool IsRepository(string directory)
        {
            if (!Directory.Exists(directory))
            {
                return false;
            }

            using (var exec = Execute(directory, "status"))
            {
                return exec.ExitCode != 128;
            }
        }

        public string GetRemoteUrl(string directory)
        {
            // $ git remote -v
            // origin  https://github.com/username/repo.git (fetch)
            // origin  https://github.com/username/repo.git (push)

            using (var exec = Execute(directory, "remote", "-v"))
            {
                exec.ThrowIfFailed();

                var originFetchUrl =
                (
                    from line in exec.StandardOutput
                    let m = Regex.Match(line, @"^\s*origin\s+([^\s]+)\s+\(fetch\)\s*$")
                    where m.Success
                    select m.Groups[1].Value
                ).FirstOrDefault();
<commit_msg>Fix nested git repository detection<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

namespace ITGlobal.MarkDocs.Git
{
    internal sealed class GitHelper
    {
        private const string EXECUTABLE_NAME = "git";

        private readonly ILogger _log;

        public GitHelper(ILogger log)
        {
            _log = log;
        }

        public bool IsGitInstalled
        {
            get
            {
                try
                {
                    using (var exec = Execute(null, "version"))
                    {
                        exec.ThrowIfFailed();

                        var version = exec.StandardOutput.FirstOrDefault();
                        _log.LogDebug("Git is installed: {0}", version);

                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool IsRepository(string directory)
        {
            if (!Directory.Exists(directory))
            {
                return false;
            }

            if (!Directory.Exists(Path.Combine(directory, ".git")))
            {
                return false;
            }

            using (var exec = Execute(directory, "status"))
            {
                return exec.ExitCode != 128;
            }
        }

        public string GetRemoteUrl(string directory)
        {
            // $ git remote -v
            // origin  https://github.com/username/repo.git (fetch)
            // origin  https://github.com/username/repo.git (push)

            using (var exec = Execute(directory, "remote", "-v"))
            {
                exec.ThrowIfFailed();

                var originFetchUrl =
                (
                    from line in exec.StandardOutput
                    let m = Regex.Match(line, @"^\s*origin\s+([^\s]+)\s+\(fetch\)\s*$")
                    where m.Success
                    select m.Groups[1].Value
                ).FirstOrDefault();

{'fraction_non_alphanumeric': 0.09440641963653529, 'fraction_numerical': 0.002360160490913382, 'mean_word_length': 1.1534552845528456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '3615403', 'n_tokens_mistral': 1081, 'n_tokens_neox': 998, 'n_words': 229}