Skip to content

Commit c5c2094

Browse files
committed
Merge pull request #677 from okb/feature/nullref-in-branched-from
FindCommitBranchWasBranchedFrom should not throw NullReferenceException
2 parents 5d8d989 + 240a0e0 commit c5c2094

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace GitVersion
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Text.RegularExpressions;
7+
8+
using JetBrains.Annotations;
9+
710
using LibGit2Sharp;
811

912
public class BranchConfigurationCalculator
@@ -35,11 +38,22 @@ public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit c
3538
throw new Exception(string.Format(format, currentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key))));
3639
}
3740

38-
static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration(Config config, Branch currentBranch)
41+
static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration([NotNull] Config config, [NotNull] Branch currentBranch)
3942
{
43+
if (config == null)
44+
{
45+
throw new ArgumentNullException("config");
46+
}
47+
48+
if (currentBranch == null)
49+
{
50+
throw new ArgumentNullException("currentBranch");
51+
}
52+
4053
return config.Branches.Where(b => Regex.IsMatch(currentBranch.Name, "^" + b.Key, RegexOptions.IgnoreCase)).ToArray();
4154
}
4255

56+
4357
static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair<string, BranchConfig> keyValuePair, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
4458
{
4559
using (Logger.IndentLog("Attempting to inherit branch configuration from parent branch"))
@@ -104,11 +118,15 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
104118
else
105119
errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name));
106120

107-
var developBranch = repository.Branches.FirstOrDefault(b => Regex.IsMatch(b.Name, "^develop", RegexOptions.IgnoreCase));
108-
var branchName = developBranch != null ? developBranch.Name : "master";
121+
var chosenBranch = repository.Branches.FirstOrDefault(b => Regex.IsMatch(b.Name, "^develop", RegexOptions.IgnoreCase)
122+
|| Regex.IsMatch(b.Name, "master$", RegexOptions.IgnoreCase));
123+
if (chosenBranch == null)
124+
throw new InvalidOperationException("Could not find a 'develop' or 'master' branch, neither locally nor remotely.");
109125

126+
var branchName = chosenBranch.Name;
110127
Logger.WriteWarning(errorMessage + Environment.NewLine + Environment.NewLine + "Falling back to " + branchName + " branch config");
111-
var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, repository.Branches[branchName]).Value;
128+
129+
var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, chosenBranch).Value;
112130
return new KeyValuePair<string, BranchConfig>(
113131
keyValuePair.Key,
114132
new BranchConfig(branchConfiguration)

src/GitVersionCore/LibGitExtensions.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace GitVersion
66
using System.Linq;
77
using System.Text;
88
using GitVersion.Helpers;
9+
10+
using JetBrains.Annotations;
11+
912
using LibGit2Sharp;
1013

1114
static class LibGitExtensions
@@ -44,13 +47,33 @@ public static SemanticVersion LastVersionTagOnBranch(this Branch branch, IReposi
4447
.FirstOrDefault();
4548
}
4649

47-
public static Commit FindCommitBranchWasBranchedFrom(this Branch branch, IRepository repository, params Branch[] excludedBranches)
50+
51+
public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
4852
{
53+
const string missingTipFormat = "{0} has no tip. Please see http://example.com/docs for information on how to fix this.";
54+
55+
if (branch == null)
56+
{
57+
throw new ArgumentNullException("branch");
58+
}
59+
4960
using (Logger.IndentLog("Finding branch source"))
5061
{
62+
if (branch.Tip == null)
63+
{
64+
Logger.WriteWarning(String.Format(missingTipFormat, branch.Name));
65+
return null;
66+
}
67+
5168
var otherBranches = repository.Branches.Except(excludedBranches).Where(b => IsSameBranch(branch, b)).ToList();
5269
var mergeBases = otherBranches.Select(b =>
5370
{
71+
if (b.Tip == null)
72+
{
73+
Logger.WriteWarning(String.Format(missingTipFormat, b.Name));
74+
return null;
75+
}
76+
5477
var otherCommit = b.Tip;
5578
if (b.Tip.Parents.Contains(branch.Tip))
5679
{
@@ -63,17 +86,23 @@ public static Commit FindCommitBranchWasBranchedFrom(this Branch branch, IReposi
6386
}
6487
}
6588

89+
6690
static bool IsSameBranch(Branch branch, Branch b)
6791
{
6892
return (b.IsRemote ? b.Name.Replace(b.Remote.Name + "/", string.Empty) : b.Name) != branch.Name;
6993
}
7094

71-
public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)
95+
public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Commit commit, IRepository repository, bool onlyTrackedBranches)
7296
{
97+
if (commit == null)
98+
{
99+
throw new ArgumentNullException("commit");
100+
}
101+
73102
var directBranchHasBeenFound = false;
74103
foreach (var branch in repository.Branches)
75104
{
76-
if (branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
105+
if (branch.Tip != null && branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
77106
{
78107
continue;
79108
}

src/GitVersionCore/Logger.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ namespace GitVersion
66

77
public static class Logger
88
{
9+
static readonly Regex ObscurePasswordRegex = new Regex("(https?://)(.+)(:.+@)", RegexOptions.Compiled);
910
static string indent = string.Empty;
1011

1112
public static Action<string> WriteInfo { get; private set; }
1213
public static Action<string> WriteWarning { get; private set; }
1314
public static Action<string> WriteError { get; private set; }
1415

16+
1517
static Logger()
1618
{
1719
Reset();
@@ -33,8 +35,7 @@ static Action<string> ObscurePassword(Action<string> info)
3335
{
3436
Action<string> logAction = s =>
3537
{
36-
var rgx = new Regex("(https?://)(.+)(:.+@)");
37-
s = rgx.Replace(s, "$1$2:*******@");
38+
s = ObscurePasswordRegex.Replace(s, "$1$2:*******@");
3839
info(s);
3940
};
4041
return logAction;

0 commit comments

Comments
 (0)