Skip to content

Commit 38b0c36

Browse files
committed
Basic support for indenting parts of the log
1 parent f12a8fd commit 38b0c36

File tree

3 files changed

+123
-92
lines changed

3 files changed

+123
-92
lines changed

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: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace GitVersion
44

55
public static class Logger
66
{
7+
static string indent = string.Empty;
8+
79
public static Action<string> WriteInfo { get; private set; }
810
public static Action<string> WriteWarning { get; private set; }
911
public static Action<string> WriteError { get; private set; }
@@ -13,6 +15,17 @@ static Logger()
1315
Reset();
1416
}
1517

18+
public static IDisposable IndentLog(string operationDescription)
19+
{
20+
indent = indent + " ";
21+
WriteInfo("Begin: " + operationDescription);
22+
return new ActionDisposable(() =>
23+
{
24+
indent = indent.Substring(0, indent.Length - 2);
25+
WriteInfo("End: " + operationDescription);
26+
});
27+
}
28+
1629
public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
1730
{
1831
WriteInfo = LogMessage(info, "INFO");
@@ -22,7 +35,7 @@ public static void SetLoggers(Action<string> info, Action<string> warn, Action<s
2235

2336
static Action<string> LogMessage(Action<string> logAction, string level)
2437
{
25-
return s => logAction(string.Format("{0} [{1:MM/dd/yy H:mm:ss:ff}] {2}", level, DateTime.Now, s));
38+
return s => logAction(string.Format("{0}{1} [{2:MM/dd/yy H:mm:ss:ff}] {3}", indent, level, DateTime.Now, s));
2639
}
2740

2841
public static void Reset()
@@ -31,5 +44,20 @@ public static void Reset()
3144
WriteWarning = s => { throw new Exception("Logger not defined."); };
3245
WriteError = s => { throw new Exception("Logger not defined."); };
3346
}
47+
48+
class ActionDisposable : IDisposable
49+
{
50+
readonly Action action;
51+
52+
public ActionDisposable(Action action)
53+
{
54+
this.action = action;
55+
}
56+
57+
public void Dispose()
58+
{
59+
action();
60+
}
61+
}
3462
}
3563
}

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
}

0 commit comments

Comments
 (0)