public void Initialize()
{
var tracer = _tracerFactory.GetTracer();
using (tracer.Step("LibGit2SharpRepository Initialize"))
{
var dotGitPath = LibGit2Sharp.Repository.Init(RepositoryPath);
using (var repo = new LibGit2Sharp.Repository(dotGitPath))
{
repo.Config.Set("core.autocrlf", OSDetector.IsOnWindows());
// This speeds up git operations like 'git checkout', especially on slow drives like in Azure
repo.Config.Set("core.preloadindex", true);
repo.Config.Set("user.name", _settings.GetGitUsername());
repo.Config.Set("user.email", _settings.GetGitEmail());
// This is needed to make lfs work
repo.Config.Set("filter.lfs.clean", "git-lfs clean %f");
repo.Config.Set("filter.lfs.smudge", "git-lfs smudge %f");
repo.Config.Set("filter.lfs.required", true);
using (tracer.Step("Configure git server"))
{
// Allow getting pushes even though we're not bare
repo.Config.Set("receive.denyCurrentBranch", "ignore");
}
// to disallow browsing to this folder in case of in-place repo
using (tracer.Step("Create deny users for .git folder"))
{
string content = "<?xml version=\"1.0\"" + @"?>
<configuration>
<system.web>
<authorization>
<deny users=" + "\"*\"" + @"/>
</authorization>
</system.web>
<configuration>";
File.WriteAllText(Path.Combine(dotGitPath, "web.config"), content);
}
// Server env does not support interactive cred prompt; hence, we intercept any credential provision
// for git fetch/clone with http/https scheme and return random invalid u/p forcing 'fatal: Authentication failed.'
using (tracer.Step("Configure git-credential"))
{
FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(GitCredentialHookPath));
string content = @"#!/bin/sh
if [ " + "\"$1\" = \"get\"" + @" ]; then
echo username=dummyUser
echo password=dummyPassword
fi" + "\n";
File.WriteAllText(GitCredentialHookPath, content);
repo.Config.Set("credential.helper", string.Format("!'{0}'", GitCredentialHookPath));
}
}
using (tracer.Step("Setup post receive hook"))
{
FileSystemHelpers.EnsureDirectory(Path.GetDirectoryName(PostReceiveHookPath));
string content = @"#!/bin/sh
read i
echo $i > pushinfo
" + KnownEnvironment.KUDUCOMMAND + "\r\n";
File.WriteAllText(PostReceiveHookPath, content);
}
// NOTE: don't add any new init steps after creating the post receive hook,
// as it's also used to mark that Init was fully executed
}
}