Skip to content

More logging when preparing the git repository #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace GitVersion
using System.IO;
using System.Linq;
using GitTools.Git;
using GitTools.Logging;
using LibGit2Sharp;

public class GitPreparer
Expand All @@ -28,6 +29,9 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic
};
this.noFetch = noFetch;
this.targetPath = targetPath.TrimEnd('/', '\\');

// GitTools has its own logging. So that it actually outputs something, it needs to be initialized.
LogProvider.SetCurrentLogProvider(new LoggerWrapper());
}

public string WorkingDirectory
Expand All @@ -48,7 +52,10 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
{
if (normaliseGitDirectory)
{
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", currentBranch)))
{
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
}
}
return;
}
Expand Down Expand Up @@ -144,23 +151,31 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth
{
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
}
Logger.WriteInfo(string.Format("Creating dynamic repository at '{0}'", targetPath));

var gitDirectory = Path.Combine(targetPath, ".git");
if (Directory.Exists(targetPath))
using (Logger.IndentLog(string.Format("Creating dynamic repository at '{0}'", targetPath)))
{
Logger.WriteInfo("Git repository already exists");
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
var gitDirectory = Path.Combine(targetPath, ".git");
if (Directory.Exists(targetPath))
{
Logger.WriteInfo("Git repository already exists");
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", targetBranch)))
{
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
}

return gitDirectory;
}
return gitDirectory;
}

CloneRepository(repositoryUrl, gitDirectory, authentication);
CloneRepository(repositoryUrl, gitDirectory, authentication);

// Normalize (download branches) before using the branch
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", targetBranch)))
{
// Normalize (download branches) before using the branch
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
}

return gitDirectory;
return gitDirectory;
}
}

static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
Expand All @@ -177,17 +192,20 @@ static void CloneRepository(string repositoryUrl, string gitDirectory, Authentic
};
}

Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", repositoryUrl));

try
{
var cloneOptions = new CloneOptions
using (Logger.IndentLog(string.Format("Cloning repository from url '{0}'", repositoryUrl)))
{
Checkout = false,
CredentialsProvider = (url, usernameFromUrl, types) => credentials
};
var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
Logger.WriteInfo(string.Format("Returned path after repository clone: {0}", returnedPath));
var cloneOptions = new CloneOptions
{
Checkout = false,
CredentialsProvider = (url, usernameFromUrl, types) => credentials
};

var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
Logger.WriteInfo(string.Format("Returned path after repository clone: {0}", returnedPath));
}
}
catch (LibGit2SharpException ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
<Compile Include="Helpers\ThreadSleep.cs" />
<Compile Include="IncrementStrategyFinder.cs" />
<Compile Include="LoggerWrapper.cs" />
<Compile Include="OutputVariables\VersionVariables.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="SemanticVersionFormatValues.cs" />
Expand Down
84 changes: 84 additions & 0 deletions src/GitVersionCore/LoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
namespace GitVersion
{
using System;
using GitTools.Logging;

/// <summary>
/// Wraps the <see cref="Logger" /> for use by GitTools.
/// </summary>
public class LoggerWrapper : ILogProvider
{
public GitTools.Logging.Logger GetLogger(string name)
{
return Log;
}

public IDisposable OpenNestedContext(string message)
{
throw new NotImplementedException();
}

public IDisposable OpenMappedContext(string key, string value)
{
throw new NotImplementedException();
}

private static bool Log(LogLevel loglevel, Func<string> messagefunc, Exception exception, object[] formatparameters)
{
// Create the main message. Careful of string format errors.
string message;
if (messagefunc == null)
{
message = null;
}
else
{
if (formatparameters == null || formatparameters.Length == 0)
{
message = messagefunc();
}
else
{
try
{
message = string.Format(messagefunc(), formatparameters);
}
catch (FormatException)
{
message = messagefunc();
Logger.WriteError(string.Format("LoggerWrapper.Log(): Incorrectly formatted string: message: '{0}'; formatparameters: {1}", message, string.Join(";", formatparameters)));
}
}
}

if (exception != null)
{
// Append the exception to the end of the message.
message = string.IsNullOrEmpty(message) ? exception.ToString() : string.Format("{0}\n{1}", message, exception);
}

if (!string.IsNullOrEmpty(message))
{
switch (loglevel)
{
case LogLevel.Trace:
case LogLevel.Debug:
Logger.WriteDebug(message);
break;
case LogLevel.Info:
Logger.WriteInfo(message);
break;
case LogLevel.Warn:
Logger.WriteWarning(message);
break;
case LogLevel.Error:
case LogLevel.Fatal:
Logger.WriteError(message);
break;
}
}

return true;
}
}
}