Skip to content

Commit 81ab590

Browse files
committed
Merge pull request #477 from JakeGinnivan/LoggingImprovements
Logging improvements
2 parents d796782 + 6aa60b6 commit 81ab590

File tree

10 files changed

+158
-112
lines changed

10 files changed

+158
-112
lines changed

GitVersionCore.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public static class ModuleInitializer
1111
/// </summary>
1212
public static void Initialize()
1313
{
14-
Logger.WriteInfo = s => Trace.WriteLine(s);
15-
Logger.WriteError = s => Trace.WriteLine(s);
16-
Logger.WriteWarning = s => Trace.WriteLine(s);
14+
Logger.SetLoggers(s => Trace.WriteLine(s), s => Trace.WriteLine(s), s => Trace.WriteLine(s));
1715
}
1816
}

GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -40,103 +40,105 @@ static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration(Config con
4040

4141
static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair<string, BranchConfig> keyValuePair, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
4242
{
43-
Logger.WriteInfo("Attempting to inherit branch configuration from parent branch");
44-
var excludedBranches = new [] { currentBranch };
45-
// Check if we are a merge commit. If so likely we are a pull request
46-
var parentCount = currentCommit.Parents.Count();
47-
if (parentCount == 2)
43+
using (Logger.IndentLog("Attempting to inherit branch configuration from parent branch"))
4844
{
49-
var parents = currentCommit.Parents.ToArray();
50-
var branch = repository.Branches.SingleOrDefault(b => !b.IsRemote && b.Tip == parents[1]);
51-
if (branch != null)
45+
var excludedBranches = new[] { currentBranch };
46+
// Check if we are a merge commit. If so likely we are a pull request
47+
var parentCount = currentCommit.Parents.Count();
48+
if (parentCount == 2)
5249
{
53-
excludedBranches = new[]
50+
var parents = currentCommit.Parents.ToArray();
51+
var branch = repository.Branches.SingleOrDefault(b => !b.IsRemote && b.Tip == parents[1]);
52+
if (branch != null)
5453
{
54+
excludedBranches = new[]
55+
{
5556
currentBranch,
5657
branch
5758
};
58-
currentBranch = branch;
59+
currentBranch = branch;
60+
}
61+
else
62+
{
63+
var possibleTargetBranches = repository.Branches.Where(b => !b.IsRemote && b.Tip == parents[0]).ToList();
64+
if (possibleTargetBranches.Count() > 1)
65+
{
66+
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name == "master") ?? possibleTargetBranches.First();
67+
}
68+
else
69+
{
70+
currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch;
71+
}
72+
}
73+
74+
Logger.WriteInfo("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name + " as base");
75+
}
76+
if (excludedInheritBranches == null)
77+
{
78+
excludedInheritBranches = repository.Branches.Where(b =>
79+
{
80+
var branchConfig = LookupBranchConfiguration(config, b);
81+
return branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit;
82+
}).ToList();
83+
}
84+
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);
85+
86+
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, excludedInheritBranches.ToArray());
87+
88+
List<Branch> possibleParents;
89+
if (branchPoint == null)
90+
{
91+
possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
5992
}
6093
else
6194
{
62-
var possibleTargetBranches = repository.Branches.Where(b => !b.IsRemote && b.Tip == parents[0]).ToList();
63-
if (possibleTargetBranches.Count() > 1)
95+
var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
96+
if (branches.Count > 1)
6497
{
65-
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name == "master") ?? possibleTargetBranches.First();
98+
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
99+
possibleParents = branches.Except(currentTipBranches).ToList();
66100
}
67101
else
68102
{
69-
currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch;
103+
possibleParents = branches;
70104
}
71105
}
72106

73-
Logger.WriteInfo("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name + " as base");
74-
}
75-
if (excludedInheritBranches == null)
76-
{
77-
excludedInheritBranches = repository.Branches.Where(b =>
78-
{
79-
var branchConfig = LookupBranchConfiguration(config, b);
80-
return branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit;
81-
}).ToList();
82-
}
83-
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);
84-
85-
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, excludedInheritBranches.ToArray());
107+
Logger.WriteInfo("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.Name)));
86108

87-
List<Branch> possibleParents;
88-
if (branchPoint == null)
89-
{
90-
possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
91-
}
92-
else
93-
{
94-
var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
95-
if (branches.Count > 1)
109+
if (possibleParents.Count == 1)
96110
{
97-
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
98-
possibleParents = branches.Except(currentTipBranches).ToList();
111+
var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0], excludedInheritBranches).Value;
112+
return new KeyValuePair<string, BranchConfig>(
113+
keyValuePair.Key,
114+
new BranchConfig(branchConfiguration)
115+
{
116+
Increment = branchConfig.Increment,
117+
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion
118+
});
99119
}
120+
121+
// If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
122+
// if develop exists and master if not
123+
string errorMessage;
124+
if (possibleParents.Count == 0)
125+
errorMessage = "Failed to inherit Increment branch configuration, no branches found.";
100126
else
101-
{
102-
possibleParents = branches;
103-
}
104-
}
127+
errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name));
105128

106-
Logger.WriteInfo("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.Name)));
129+
var developBranch = repository.Branches.FirstOrDefault(b => Regex.IsMatch(b.Name, "^develop", RegexOptions.IgnoreCase));
130+
var branchName = developBranch != null ? developBranch.Name : "master";
107131

108-
if (possibleParents.Count == 1)
109-
{
110-
var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0], excludedInheritBranches).Value;
132+
Logger.WriteWarning(errorMessage + Environment.NewLine + Environment.NewLine + "Falling back to " + branchName + " branch config");
133+
var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, repository.Branches[branchName]).Value;
111134
return new KeyValuePair<string, BranchConfig>(
112135
keyValuePair.Key,
113136
new BranchConfig(branchConfiguration)
114137
{
115-
Increment = branchConfig.Increment,
116-
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion
138+
Increment = value.Increment,
139+
PreventIncrementOfMergedBranchVersion = value.PreventIncrementOfMergedBranchVersion
117140
});
118141
}
119-
120-
// If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
121-
// if develop exists and master if not
122-
string errorMessage;
123-
if (possibleParents.Count == 0)
124-
errorMessage = "Failed to inherit Increment branch configuration, no branches found.";
125-
else
126-
errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name));
127-
128-
var developBranch = repository.Branches.FirstOrDefault(b => Regex.IsMatch(b.Name, "^develop", RegexOptions.IgnoreCase));
129-
var branchName = developBranch != null ? developBranch.Name : "master";
130-
131-
Logger.WriteWarning(errorMessage + Environment.NewLine + Environment.NewLine + "Falling back to " + branchName + " branch config");
132-
var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, repository.Branches[branchName]).Value;
133-
return new KeyValuePair<string, BranchConfig>(
134-
keyValuePair.Key,
135-
new BranchConfig(branchConfiguration)
136-
{
137-
Increment = value.Increment,
138-
PreventIncrementOfMergedBranchVersion = value.PreventIncrementOfMergedBranchVersion
139-
});
140142
}
141143
}
142144
}

GitVersionCore/Logger.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,65 @@
11
namespace GitVersion
22
{
33
using System;
4+
using System.Globalization;
45

56
public static class Logger
67
{
7-
public static Action<string> WriteInfo;
8-
public static Action<string> WriteWarning;
9-
public static Action<string> WriteError;
8+
static string indent = string.Empty;
9+
10+
public static Action<string> WriteInfo { get; private set; }
11+
public static Action<string> WriteWarning { get; private set; }
12+
public static Action<string> WriteError { get; private set; }
1013

1114
static Logger()
1215
{
1316
Reset();
1417
}
1518

19+
public static IDisposable IndentLog(string operationDescription)
20+
{
21+
var start = DateTime.Now;
22+
indent = indent + " ";
23+
WriteInfo("Begin: " + operationDescription);
24+
return new ActionDisposable(() =>
25+
{
26+
indent = indent.Substring(0, indent.Length - 2);
27+
WriteInfo(string.Format(CultureInfo.InvariantCulture, "End: {0} (Took: {1:N}ms)", operationDescription, DateTime.Now.Subtract(start).TotalMilliseconds));
28+
});
29+
}
30+
31+
public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
32+
{
33+
WriteInfo = LogMessage(info, "INFO");
34+
WriteWarning = LogMessage(warn, "WARN");
35+
WriteError = LogMessage(error, "ERROR");
36+
}
37+
38+
static Action<string> LogMessage(Action<string> logAction, string level)
39+
{
40+
return s => logAction(string.Format(CultureInfo.InvariantCulture, "{0}{1} [{2:MM/dd/yy H:mm:ss:ff}] {3}", indent, level, DateTime.Now, s));
41+
}
42+
1643
public static void Reset()
1744
{
1845
WriteInfo = s => { throw new Exception("Logger not defined."); };
1946
WriteWarning = s => { throw new Exception("Logger not defined."); };
2047
WriteError = s => { throw new Exception("Logger not defined."); };
2148
}
49+
50+
class ActionDisposable : IDisposable
51+
{
52+
readonly Action action;
53+
54+
public ActionDisposable(Action action)
55+
{
56+
this.action = action;
57+
}
58+
59+
public void Dispose()
60+
{
61+
action();
62+
}
63+
}
2264
}
2365
}

GitVersionCore/VersionCalculation/BaseVersionCalculator.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,34 @@ public BaseVersionCalculator(params BaseVersionStrategy[] strategies)
1414

1515
public BaseVersion GetBaseVersion(GitVersionContext context)
1616
{
17-
Logger.WriteInfo("Base Versions:");
18-
19-
var baseVersion = strategies
20-
.Select(s => s.GetVersion(context))
21-
.Where(v =>
22-
{
23-
if (v != null)
17+
using (Logger.IndentLog("Calculating base versions"))
18+
{
19+
var baseVersion = strategies
20+
.Select(s => s.GetVersion(context))
21+
.Where(v =>
2422
{
25-
Logger.WriteInfo(v.ToString());
26-
return true;
27-
}
28-
29-
return false;
30-
})
31-
.Aggregate((v1, v2) =>
32-
{
33-
if (v1.SemanticVersion > v2.SemanticVersion)
23+
if (v != null)
24+
{
25+
Logger.WriteInfo(v.ToString());
26+
return true;
27+
}
28+
29+
return false;
30+
})
31+
.Aggregate((v1, v2) =>
3432
{
35-
return new BaseVersion(v1.Source, v1.ShouldIncrement, v1.SemanticVersion, v1.BaseVersionSource ?? v2.BaseVersionSource, v1.BranchNameOverride);
36-
}
33+
if (v1.SemanticVersion > v2.SemanticVersion)
34+
{
35+
return new BaseVersion(v1.Source, v1.ShouldIncrement, v1.SemanticVersion, v1.BaseVersionSource ?? v2.BaseVersionSource, v1.BranchNameOverride);
36+
}
3737

38-
return new BaseVersion(v2.Source, v2.ShouldIncrement, v2.SemanticVersion, v2.BaseVersionSource ?? v1.BaseVersionSource, v2.BranchNameOverride);
39-
});
38+
return new BaseVersion(v2.Source, v2.ShouldIncrement, v2.SemanticVersion, v2.BaseVersionSource ?? v1.BaseVersionSource, v2.BranchNameOverride);
39+
});
4040

41-
Logger.WriteInfo(string.Format("Base version used: {0}", baseVersion));
41+
Logger.WriteInfo(string.Format("Base version used: {0}", baseVersion));
4242

43-
return baseVersion;
43+
return baseVersion;
44+
}
4445
}
4546
}
4647
}

GitVersionExe.Tests/GitPreparerTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public class GitPreparerTests
1111
{
1212
public GitPreparerTests()
1313
{
14-
Logger.WriteInfo = s => { };
15-
Logger.WriteWarning = s => { };
16-
Logger.WriteError = s => { };
14+
Logger.SetLoggers(s => { }, s => { }, s => { });
1715
}
1816

1917
const string DefaultBranchName = "master";

GitVersionExe/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ static void ConfigureLogging(Arguments arguments)
126126
}
127127
}
128128

129-
Logger.WriteInfo = s => writeActions.ForEach(a => a(s));
130-
Logger.WriteWarning = s => writeActions.ForEach(a => a(s));
131-
Logger.WriteError = s => writeActions.ForEach(a => a(s));
129+
Logger.SetLoggers(
130+
s => writeActions.ForEach(a => a(s)),
131+
s => writeActions.ForEach(a => a(s)),
132+
s => writeActions.ForEach(a => a(s)));
132133
}
133134

134135
static void WriteLogEntry(Arguments arguments, string s)

GitVersionTask.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public static class ModuleInitializer
1111
/// </summary>
1212
public static void Initialize()
1313
{
14-
Logger.WriteInfo = s => Trace.WriteLine(s);
15-
Logger.WriteError = s => Trace.WriteLine(s);
16-
Logger.WriteWarning = s => Trace.WriteLine(s);
14+
Logger.SetLoggers(s => Trace.WriteLine(s), s => Trace.WriteLine(s), s => Trace.WriteLine(s));
1715
}
1816

1917
}

GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public UpdateAssemblyInfo()
3535
CompileFiles = new ITaskItem[] { };
3636
logger = new TaskLogger(this);
3737
fileSystem = new FileSystem();
38-
Logger.WriteInfo = this.LogInfo;
39-
Logger.WriteWarning = this.LogWarning;
38+
Logger.SetLoggers(
39+
this.LogInfo,
40+
this.LogWarning,
41+
s => this.LogError(s));
4042
}
4143

4244
public override bool Execute()

GitVersionTask/GetVersion.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ public GetVersion()
7878
{
7979
logger = new TaskLogger(this);
8080
fileSystem = new FileSystem();
81-
Logger.WriteInfo = this.LogInfo;
82-
Logger.WriteWarning = this.LogWarning;
81+
Logger.SetLoggers(
82+
this.LogInfo,
83+
this.LogWarning,
84+
s => this.LogError(s));
8385
}
8486

8587
public override bool Execute()

GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public WriteVersionInfoToBuildLog()
2222
{
2323
logger = new TaskLogger(this);
2424
fileSystem = new FileSystem();
25-
Logger.WriteInfo = this.LogInfo;
26-
Logger.WriteWarning = this.LogWarning;
25+
Logger.SetLoggers(
26+
this.LogInfo,
27+
this.LogWarning,
28+
s => this.LogError(s));
2729
}
2830

2931
public override bool Execute()

0 commit comments

Comments
 (0)