Skip to content

Commit 1d2031a

Browse files
authored
Merge pull request #1116 from DanielRose/more-logging
More logging when preparing the git repository
2 parents e2b3aa1 + bc318b3 commit 1d2031a

File tree

3 files changed

+122
-19
lines changed

3 files changed

+122
-19
lines changed

src/GitVersionCore/GitPreparer.cs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace GitVersion
44
using System.IO;
55
using System.Linq;
66
using GitTools.Git;
7+
using GitTools.Logging;
78
using LibGit2Sharp;
89

910
public class GitPreparer
@@ -28,6 +29,9 @@ public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentic
2829
};
2930
this.noFetch = noFetch;
3031
this.targetPath = targetPath.TrimEnd('/', '\\');
32+
33+
// GitTools has its own logging. So that it actually outputs something, it needs to be initialized.
34+
LogProvider.SetCurrentLogProvider(new LoggerWrapper());
3135
}
3236

3337
public string WorkingDirectory
@@ -48,7 +52,10 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
4852
{
4953
if (normaliseGitDirectory)
5054
{
51-
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
55+
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", currentBranch)))
56+
{
57+
GitRepositoryHelper.NormalizeGitDirectory(GetDotGitDirectory(), authentication, noFetch, currentBranch);
58+
}
5259
}
5360
return;
5461
}
@@ -144,23 +151,31 @@ static string CreateDynamicRepository(string targetPath, AuthenticationInfo auth
144151
{
145152
throw new Exception("Dynamic Git repositories must have a target branch (/b)");
146153
}
147-
Logger.WriteInfo(string.Format("Creating dynamic repository at '{0}'", targetPath));
148154

149-
var gitDirectory = Path.Combine(targetPath, ".git");
150-
if (Directory.Exists(targetPath))
155+
using (Logger.IndentLog(string.Format("Creating dynamic repository at '{0}'", targetPath)))
151156
{
152-
Logger.WriteInfo("Git repository already exists");
153-
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
157+
var gitDirectory = Path.Combine(targetPath, ".git");
158+
if (Directory.Exists(targetPath))
159+
{
160+
Logger.WriteInfo("Git repository already exists");
161+
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", targetBranch)))
162+
{
163+
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
164+
}
154165

155-
return gitDirectory;
156-
}
166+
return gitDirectory;
167+
}
157168

158-
CloneRepository(repositoryUrl, gitDirectory, authentication);
169+
CloneRepository(repositoryUrl, gitDirectory, authentication);
159170

160-
// Normalize (download branches) before using the branch
161-
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
171+
using (Logger.IndentLog(string.Format("Normalizing git directory for branch '{0}'", targetBranch)))
172+
{
173+
// Normalize (download branches) before using the branch
174+
GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch);
175+
}
162176

163-
return gitDirectory;
177+
return gitDirectory;
178+
}
164179
}
165180

166181
static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
@@ -181,17 +196,20 @@ static void CloneRepository(string repositoryUrl, string gitDirectory, Authentic
181196
}
182197
}
183198

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

186200
try
187201
{
188-
var cloneOptions = new CloneOptions
202+
using (Logger.IndentLog(string.Format("Cloning repository from url '{0}'", repositoryUrl)))
189203
{
190-
Checkout = false,
191-
CredentialsProvider = (url, usernameFromUrl, types) => credentials
192-
};
193-
var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
194-
Logger.WriteInfo(string.Format("Returned path after repository clone: {0}", returnedPath));
204+
var cloneOptions = new CloneOptions
205+
{
206+
Checkout = false,
207+
CredentialsProvider = (url, usernameFromUrl, types) => credentials
208+
};
209+
210+
var returnedPath = Repository.Clone(repositoryUrl, gitDirectory, cloneOptions);
211+
Logger.WriteInfo(string.Format("Returned path after repository clone: {0}", returnedPath));
212+
}
195213
}
196214
catch (LibGit2SharpException ex)
197215
{

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
135135
<Compile Include="Helpers\ThreadSleep.cs" />
136136
<Compile Include="IncrementStrategyFinder.cs" />
137+
<Compile Include="LoggerWrapper.cs" />
137138
<Compile Include="OutputVariables\VersionVariables.cs" />
138139
<Compile Include="SemanticVersionExtensions.cs" />
139140
<Compile Include="SemanticVersionFormatValues.cs" />

src/GitVersionCore/LoggerWrapper.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using GitTools.Logging;
5+
6+
/// <summary>
7+
/// Wraps the <see cref="Logger" /> for use by GitTools.
8+
/// </summary>
9+
public class LoggerWrapper : ILogProvider
10+
{
11+
public GitTools.Logging.Logger GetLogger(string name)
12+
{
13+
return Log;
14+
}
15+
16+
public IDisposable OpenNestedContext(string message)
17+
{
18+
throw new NotImplementedException();
19+
}
20+
21+
public IDisposable OpenMappedContext(string key, string value)
22+
{
23+
throw new NotImplementedException();
24+
}
25+
26+
private static bool Log(LogLevel loglevel, Func<string> messagefunc, Exception exception, object[] formatparameters)
27+
{
28+
// Create the main message. Careful of string format errors.
29+
string message;
30+
if (messagefunc == null)
31+
{
32+
message = null;
33+
}
34+
else
35+
{
36+
if (formatparameters == null || formatparameters.Length == 0)
37+
{
38+
message = messagefunc();
39+
}
40+
else
41+
{
42+
try
43+
{
44+
message = string.Format(messagefunc(), formatparameters);
45+
}
46+
catch (FormatException)
47+
{
48+
message = messagefunc();
49+
Logger.WriteError(string.Format("LoggerWrapper.Log(): Incorrectly formatted string: message: '{0}'; formatparameters: {1}", message, string.Join(";", formatparameters)));
50+
}
51+
}
52+
}
53+
54+
if (exception != null)
55+
{
56+
// Append the exception to the end of the message.
57+
message = string.IsNullOrEmpty(message) ? exception.ToString() : string.Format("{0}\n{1}", message, exception);
58+
}
59+
60+
if (!string.IsNullOrEmpty(message))
61+
{
62+
switch (loglevel)
63+
{
64+
case LogLevel.Trace:
65+
case LogLevel.Debug:
66+
Logger.WriteDebug(message);
67+
break;
68+
case LogLevel.Info:
69+
Logger.WriteInfo(message);
70+
break;
71+
case LogLevel.Warn:
72+
Logger.WriteWarning(message);
73+
break;
74+
case LogLevel.Error:
75+
case LogLevel.Fatal:
76+
Logger.WriteError(message);
77+
break;
78+
}
79+
}
80+
81+
return true;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)