<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Kudu.Client.Deployment;
using Kudu.Core.Infrastructure;
using Kudu.Core.SourceControl.Git;
using SystemEnvironment = System.Environment;

namespace Kudu.TestHarness
{
    public static class Git
    {
        public static string Push(string repositoryPath, string url, string localBranchName = "master", string remoteBranchName = "master")
        {
            Executable gitExe = GetGitExe(repositoryPath);

            string stdErr = null;

            if (localBranchName.Equals("master"))
            {
                stdErr = gitExe.Execute("push {0} {1}", url, remoteBranchName).Item2;

                // Dump out the error stream (git curl verbose)
                Debug.WriteLine(stdErr);
            }
            else
            {
                // Dump out the error stream (git curl verbose)
                stdErr = gitExe.Execute("push {0} {1}:{2}", url, localBranchName, remoteBranchName).Item2;

                Debug.WriteLine(stdErr);
            }

            return stdErr;
        }

        public static TestRepository Init(string repositoryPath)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("init");

            return new TestRepository(repositoryPath);
        }

        public static void Revert(string repositoryPath, string commit = "HEAD")
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("revert --no-edit \"{0}\"", commit);
        }

        public static void Reset(string repositoryPath, string commit = "HEAD^")
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("reset --hard \"{0}\"", commit);
        }

        public static void CheckOut(string repositoryPath, string branchName)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("checkout -b {0} -t origin/{0}", branchName);
        }

        public static void Commit(string repositoryPath, string message)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            try
            {
                gitExe.Execute("add -A", message);
                gitExe.Execute("commit -m \"{0}\"", message);
            }
            catch (Exception ex)
            {
                // Swallow exceptions on comit, since things like changing line endings
                // show up as an error
                Debug.WriteLine(ex.Message);
            }
        }

        public static void Add(string repositoryPath, string path)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("add \"{0}\"", path);
        }

        public static TestRepository Clone(string repositoryName, string source, IDictionary<string, string> environments = null, bool requiresEditableRepository = false, bool noCache = false)
        {
            // Check if we have a cached instance of the repository available locally
            string cachedPath = noCache ? null : CreateCachedRepo(repositoryName, source, environments);

            if (cachedPath != null && !requiresEditableRepository)
            {
                return new TestRepository(cachedPath, obliterateOnDispose: false);
            }

            string repositoryPath = GetRepositoryPath(repositoryName);<commit_msg>Adding retry code to Git.Clone for parity with private<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Kudu.Client.Deployment;
using Kudu.Core.Infrastructure;
using Kudu.Core.SourceControl.Git;
using SystemEnvironment = System.Environment;

namespace Kudu.TestHarness
{
    public static class Git
    {
        public static string Push(string repositoryPath, string url, string localBranchName = "master", string remoteBranchName = "master")
        {
            Executable gitExe = GetGitExe(repositoryPath);

            string stdErr = null;

            if (localBranchName.Equals("master"))
            {
                stdErr = gitExe.Execute("push {0} {1}", url, remoteBranchName).Item2;

                // Dump out the error stream (git curl verbose)
                Debug.WriteLine(stdErr);
            }
            else
            {
                // Dump out the error stream (git curl verbose)
                stdErr = gitExe.Execute("push {0} {1}:{2}", url, localBranchName, remoteBranchName).Item2;

                Debug.WriteLine(stdErr);
            }

            return stdErr;
        }

        public static TestRepository Init(string repositoryPath)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("init");

            return new TestRepository(repositoryPath);
        }

        public static void Revert(string repositoryPath, string commit = "HEAD")
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("revert --no-edit \"{0}\"", commit);
        }

        public static void Reset(string repositoryPath, string commit = "HEAD^")
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("reset --hard \"{0}\"", commit);
        }

        public static void CheckOut(string repositoryPath, string branchName)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("checkout -b {0} -t origin/{0}", branchName);
        }

        public static void Commit(string repositoryPath, string message)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            try
            {
                gitExe.Execute("add -A", message);
                gitExe.Execute("commit -m \"{0}\"", message);
            }
            catch (Exception ex)
            {
                // Swallow exceptions on comit, since things like changing line endings
                // show up as an error
                Debug.WriteLine(ex.Message);
            }
        }

        public static void Add(string repositoryPath, string path)
        {
            Executable gitExe = GetGitExe(repositoryPath);
            gitExe.Execute("add \"{0}\"", path);
        }

        public static TestRepository Clone(string repositoryName, string source, IDictionary<string, string> environments = null, bool requiresEditableRepository = false, bool noCache = false)
        {
            return OperationManager.Attempt(() => CloneInternal(repositoryName, source, environments, requiresEditableRepository, noCache));
        }

        private static TestRepository CloneInternal(string repositoryName, string source, IDictionary<string, string> environments, bool requiresEditableRepository, bool noCache)
        {
            // Check if we have a cached instance of the repository available locally
            string cachedPath = noCache ? null : CreateCachedRepo(repositoryName, source, environments);

            if (cachedPath != null && !requiresEditableRepository)
            {
                return new TestRepository(cachedPath, obliterateOnDispose: false);
            }

            string repositoryPath = GetRepositoryPath(repositoryName);
{'fraction_non_alphanumeric': 0.08992903930131005, 'fraction_numerical': 0.0035480349344978166, 'mean_word_length': 2.0976331360946747, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '3706580', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1815, 'n_words': 502}