diff --git a/src/GitVersionCore/GitPreparer.cs b/src/GitVersionCore/GitPreparer.cs index 4bda46e1a9..2c0063d833 100644 --- a/src/GitVersionCore/GitPreparer.cs +++ b/src/GitVersionCore/GitPreparer.cs @@ -4,6 +4,7 @@ namespace GitVersion using System.IO; using System.Linq; using GitTools.Git; + using GitTools.Logging; using LibGit2Sharp; public class GitPreparer @@ -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 @@ -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; } @@ -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) @@ -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) { diff --git a/src/GitVersionCore/GitVersionCore.csproj b/src/GitVersionCore/GitVersionCore.csproj index ef715fea33..eded0daf6f 100644 --- a/src/GitVersionCore/GitVersionCore.csproj +++ b/src/GitVersionCore/GitVersionCore.csproj @@ -133,6 +133,7 @@ + diff --git a/src/GitVersionCore/LoggerWrapper.cs b/src/GitVersionCore/LoggerWrapper.cs new file mode 100644 index 0000000000..5c4489dc26 --- /dev/null +++ b/src/GitVersionCore/LoggerWrapper.cs @@ -0,0 +1,84 @@ +namespace GitVersion +{ + using System; + using GitTools.Logging; + + /// + /// Wraps the for use by GitTools. + /// + 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 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; + } + } +} \ No newline at end of file