<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace GitHub.Unity
{
    interface IGitClient
    {
        Task<NPath> FindGitInstallation();
        bool ValidateGitInstall(NPath path);

        ITask Init(IOutputProcessor<string> processor = null);

        ITask LfsInstall(IOutputProcessor<string> processor = null);

        ITask<GitStatus?> Status(IOutputProcessor<GitStatus?> processor = null);

        ITask<string> GetConfig(string key, GitConfigSource configSource,
            IOutputProcessor<string> processor = null);

        ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
            IOutputProcessor<string> processor = null);

        ITask<List<GitLock>> ListLocks(bool local,
            BaseOutputListProcessor<GitLock> processor = null);

        ITask<string> Pull(string remote, string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> Push(string remote, string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> Revert(string changeset,
            IOutputProcessor<string> processor = null);

        ITask<string> Fetch(string remote,
            IOutputProcessor<string> processor = null);

        ITask<string> SwitchBranch(string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> DeleteBranch(string branch, bool deleteUnmerged = false,
            IOutputProcessor<string> processor = null);

        ITask<string> CreateBranch(string branch, string baseBranch,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteAdd(string remote, string url,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteRemove(string remote,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteChange(string remote, string url,
            IOutputProcessor<string> processor = null);

        ITask<string> Commit(string message, string body,
            IOutputProcessor<string> processor = null);

        ITask<string> Add(IList<string> files,
            IOutputProcessor<string> processor = null);

        ITask<string> AddAll(IOutputProcessor<string> processor = null);

        ITask<string> Remove(IList<string> files,
            IOutputProcessor<string> processor = null);

        ITask<string> AddAndCommit(IList<string> files, string message, string body,
            IOutputProcessor<string> processor = null);

        ITask<string> Lock(string file,
            IOutputProcessor<string> processor = null);

        ITask<string> Unlock(string file, bool force,
            IOutputProcessor<string> processor = null);

        ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null);
    }

    class GitClient : IGitClient
    {
        private readonly IEnvironment environment;
        private readonly IProcessManager processManager;
        private readonly ICredentialManager credentialManager;
        private readonly ITaskManager taskManager;
        private readonly CancellationToken cancellationToken;


        public GitClient(IEnvironment environment, IProcessManager processManager,
            ICredentialManager credentialManager, ITaskManager taskManager)
        {
            this.environment = environment;
            this.processManager = processManager;
            this.credentialManager = credentialManager;
            this.taskManager = taskManager;
            this.cancellationToken = taskManager.Token;
        }

        public async Task<NPath> FindGitInstallation()
        {
            if (!String.IsNullOrEmpty(environment.GitExecutablePath))
                return environment.GitExecutablePath;

            var path = await LookForPortableGit();
            if (path == null)
                path = await LookForSystemGit();

            Logger.Trace("Git Installation folder {0} discovered: '{1}'", path == null ? "not" : "", path);

            return path;
        }

        private Task<NPath> LookForPortableGit()
        {
            var gitHubLocalAppDataPath = environment.UserCachePath;
            if (!gitHubLocalAppDataPath.DirectoryExists())
                return null;

            var searchPath = "PortableGit_";

            var portableGitPath = gitHubLocalAppDataPath.Directories()
                .Where(s => s.FileName.StartsWith(searchPath, StringComparison.OrdinalIgnoreCase))
                .FirstOrDefault();

            if (portableGitPath != null)
            {
                portableGitPath = portableGitPath.Combine("cmd", $"git{environment.ExecutableExtension}");
            }

            return TaskEx.FromResult(portableGitPath);
        }

        private async Task<NPath> LookForSystemGit()
        {
            NPath path = null;
            if (!environment.IsWindows)
            {
                var p = new NPath("/usr/local/bin/git");
                if (p.FileExists())
                    path = p;
            }

            if (path == null)
                path = await new FindExecTask("git", taskManager.Token).StartAwait();

            return path;
        }

        public bool ValidateGitInstall(NPath path)
        {<commit_msg>Missing call to Configure on FindExecTask<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace GitHub.Unity
{
    interface IGitClient
    {
        Task<NPath> FindGitInstallation();
        bool ValidateGitInstall(NPath path);

        ITask Init(IOutputProcessor<string> processor = null);

        ITask LfsInstall(IOutputProcessor<string> processor = null);

        ITask<GitStatus?> Status(IOutputProcessor<GitStatus?> processor = null);

        ITask<string> GetConfig(string key, GitConfigSource configSource,
            IOutputProcessor<string> processor = null);

        ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
            IOutputProcessor<string> processor = null);

        ITask<List<GitLock>> ListLocks(bool local,
            BaseOutputListProcessor<GitLock> processor = null);

        ITask<string> Pull(string remote, string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> Push(string remote, string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> Revert(string changeset,
            IOutputProcessor<string> processor = null);

        ITask<string> Fetch(string remote,
            IOutputProcessor<string> processor = null);

        ITask<string> SwitchBranch(string branch,
            IOutputProcessor<string> processor = null);

        ITask<string> DeleteBranch(string branch, bool deleteUnmerged = false,
            IOutputProcessor<string> processor = null);

        ITask<string> CreateBranch(string branch, string baseBranch,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteAdd(string remote, string url,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteRemove(string remote,
            IOutputProcessor<string> processor = null);

        ITask<string> RemoteChange(string remote, string url,
            IOutputProcessor<string> processor = null);

        ITask<string> Commit(string message, string body,
            IOutputProcessor<string> processor = null);

        ITask<string> Add(IList<string> files,
            IOutputProcessor<string> processor = null);

        ITask<string> AddAll(IOutputProcessor<string> processor = null);

        ITask<string> Remove(IList<string> files,
            IOutputProcessor<string> processor = null);

        ITask<string> AddAndCommit(IList<string> files, string message, string body,
            IOutputProcessor<string> processor = null);

        ITask<string> Lock(string file,
            IOutputProcessor<string> processor = null);

        ITask<string> Unlock(string file, bool force,
            IOutputProcessor<string> processor = null);

        ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null);
    }

    class GitClient : IGitClient
    {
        private readonly IEnvironment environment;
        private readonly IProcessManager processManager;
        private readonly ICredentialManager credentialManager;
        private readonly ITaskManager taskManager;
        private readonly CancellationToken cancellationToken;


        public GitClient(IEnvironment environment, IProcessManager processManager,
            ICredentialManager credentialManager, ITaskManager taskManager)
        {
            this.environment = environment;
            this.processManager = processManager;
            this.credentialManager = credentialManager;
            this.taskManager = taskManager;
            this.cancellationToken = taskManager.Token;
        }

        public async Task<NPath> FindGitInstallation()
        {
            if (!String.IsNullOrEmpty(environment.GitExecutablePath))
                return environment.GitExecutablePath;

            var path = await LookForPortableGit();
            if (path == null)
                path = await LookForSystemGit();

            Logger.Trace("Git Installation folder {0} discovered: '{1}'", path == null ? "not" : "", path);

            return path;
        }

        private Task<NPath> LookForPortableGit()
        {
            var gitHubLocalAppDataPath = environment.UserCachePath;
            if (!gitHubLocalAppDataPath.DirectoryExists())
                return null;

            var searchPath = "PortableGit_";

            var portableGitPath = gitHubLocalAppDataPath.Directories()
                .Where(s => s.FileName.StartsWith(searchPath, StringComparison.OrdinalIgnoreCase))
                .FirstOrDefault();

            if (portableGitPath != null)
            {
                portableGitPath = portableGitPath.Combine("cmd", $"git{environment.ExecutableExtension}");
            }

            return TaskEx.FromResult(portableGitPath);
        }

        private async Task<NPath> LookForSystemGit()
        {
            NPath path = null;
            if (!environment.IsWindows)
            {
                var p = new NPath("/usr/local/bin/git");
                if (p.FileExists())
                    path = p;
            }

            if (path == null)
            {
                path = await new FindExecTask("git", taskManager.Token)
                    .Configure(processManager).StartAwait();
            }

            return path;
        }

        public bool ValidateGitInstall(NPath path)
        {
{'fraction_non_alphanumeric': 0.08487496513897927, 'fraction_numerical': 0.0003718508877939946, 'mean_word_length': 2.4658505154639174, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 115, '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': '4099602', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2625, 'n_words': 628}