<commit_before>﻿using System.IO;
using System.Linq;
using Kudu.Contracts.Tracing;
using Kudu.Core.Tracing;

namespace Kudu.Core.SourceControl.Git
{
    public class GitDeploymentRepository : IDeploymentRepository
    {
        private readonly GitExecutable _gitExe;
        private readonly ITraceFactory _traceFactory;
        private readonly GitExeRepository _repository;

        public GitDeploymentRepository(string path, ITraceFactory traceFactory)
        {
            _gitExe = new GitExecutable(path);
            _gitExe.SetTraceLevel(2);
            _traceFactory = traceFactory;
            _repository = new GitExeRepository(path, traceFactory);
            _repository.SetTraceLevel(2);
        }

        private string PostReceiveHookPath
        {
            get
            {
                return Path.Combine(_gitExe.WorkingDirectory, ".git", "hooks", "post-receive");
            }
        }

        private string PushInfoPath
        {
            get
            {
                return Path.Combine(_gitExe.WorkingDirectory, ".git", "pushinfo");
            }
        }

        public void Clean()
        {
            _repository.Clean();
        }

        public ChangeSet GetChangeSet(string id)
        {
            return _repository.GetChangeSet(id);
        }

        public PushInfo GetPushInfo()
        {
            string path = PushInfoPath;

            if (!File.Exists(path))
            {
                return null;
            }

            string[] pushDetails = File.ReadAllText(path).Split(' ');

            if (pushDetails.Length == 3)
            {
                string oldId = pushDetails[0];
                string newId = pushDetails[1];

                // When a branch gets deleted, the newId is an all-zero string.
                // In those cases, we never want to do anything, so return null
                if (newId.Trim('0').Length == 0)
                {
                    return null;
                }

                string reference = pushDetails[2];
                string branch = reference.Split('/').Last().Trim();
                string fullNewId = _repository.Resolve(branch);

                return new PushInfo
                {
                    OldId = oldId,
                    NewId = newId,
                    Branch = new GitBranch(fullNewId, branch, false)
                };
            }

            return null;
        }

        public void Update()
        {
            EnsureNoLockFile();
            _repository.Update();
        }

        public void Update(string id)
        {
            EnsureNoLockFile();
            _repository.Update(id);
        }

        private void EnsureNoLockFile()
        {
            ITracer tracer = _traceFactory.GetTracer();

            // Delete the lock file from the .git folder
            string lockFilePath = Path.Combine(_gitExe.WorkingDirectory, ".git", "index.lock");
            if (File.Exists(lockFilePath))
            {
                tracer.TraceWarning("Deleting left over index.lock file");
                File.Delete(lockFilePath);
            }

        }
    }
}
<commit_msg>Get tracer in the if.<commit_after>﻿using System.IO;
using System.Linq;
using Kudu.Contracts.Tracing;
using Kudu.Core.Tracing;

namespace Kudu.Core.SourceControl.Git
{
    public class GitDeploymentRepository : IDeploymentRepository
    {
        private readonly GitExecutable _gitExe;
        private readonly ITraceFactory _traceFactory;
        private readonly GitExeRepository _repository;

        public GitDeploymentRepository(string path, ITraceFactory traceFactory)
        {
            _gitExe = new GitExecutable(path);
            _gitExe.SetTraceLevel(2);
            _traceFactory = traceFactory;
            _repository = new GitExeRepository(path, traceFactory);
            _repository.SetTraceLevel(2);
        }

        private string PostReceiveHookPath
        {
            get
            {
                return Path.Combine(_gitExe.WorkingDirectory, ".git", "hooks", "post-receive");
            }
        }

        private string PushInfoPath
        {
            get
            {
                return Path.Combine(_gitExe.WorkingDirectory, ".git", "pushinfo");
            }
        }

        public void Clean()
        {
            _repository.Clean();
        }

        public ChangeSet GetChangeSet(string id)
        {
            return _repository.GetChangeSet(id);
        }

        public PushInfo GetPushInfo()
        {
            string path = PushInfoPath;

            if (!File.Exists(path))
            {
                return null;
            }

            string[] pushDetails = File.ReadAllText(path).Split(' ');

            if (pushDetails.Length == 3)
            {
                string oldId = pushDetails[0];
                string newId = pushDetails[1];

                // When a branch gets deleted, the newId is an all-zero string.
                // In those cases, we never want to do anything, so return null
                if (newId.Trim('0').Length == 0)
                {
                    return null;
                }

                string reference = pushDetails[2];
                string branch = reference.Split('/').Last().Trim();
                string fullNewId = _repository.Resolve(branch);

                return new PushInfo
                {
                    OldId = oldId,
                    NewId = newId,
                    Branch = new GitBranch(fullNewId, branch, false)
                };
            }

            return null;
        }

        public void Update()
        {
            EnsureNoLockFile();
            _repository.Update();
        }

        public void Update(string id)
        {
            EnsureNoLockFile();
            _repository.Update(id);
        }

        private void EnsureNoLockFile()
        {
            // Delete the lock file from the .git folder
            string lockFilePath = Path.Combine(_gitExe.WorkingDirectory, ".git", "index.lock");
            if (File.Exists(lockFilePath))
            {
                ITracer tracer = _traceFactory.GetTracer();
                tracer.TraceWarning("Deleting left over index.lock file");
                File.Delete(lockFilePath);
            }

        }
    }
}

{'fraction_non_alphanumeric': 0.08458498023715415, 'fraction_numerical': 0.0025296442687747036, 'mean_word_length': 1.4025826053930877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '975619', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1550, 'n_words': 364}