Skip to content

FindCommitBranchWasBranchedFrom should not throw NullReferenceException #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 11, 2015
26 changes: 22 additions & 4 deletions src/GitVersionCore/BranchConfigurationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace GitVersion
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using JetBrains.Annotations;

using LibGit2Sharp;

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

static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration(Config config, Branch currentBranch)
static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration([NotNull] Config config, [NotNull] Branch currentBranch)
{
if (config == null)
{
throw new ArgumentNullException("config");
}

if (currentBranch == null)
{
throw new ArgumentNullException("currentBranch");
}

return config.Branches.Where(b => Regex.IsMatch(currentBranch.Name, "^" + b.Key, RegexOptions.IgnoreCase)).ToArray();
}


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

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

var branchName = chosenBranch.Name;
Logger.WriteWarning(errorMessage + Environment.NewLine + Environment.NewLine + "Falling back to " + branchName + " branch config");
var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, repository.Branches[branchName]).Value;

var value = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, chosenBranch).Value;
return new KeyValuePair<string, BranchConfig>(
keyValuePair.Key,
new BranchConfig(branchConfiguration)
Expand Down
35 changes: 32 additions & 3 deletions src/GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace GitVersion
using System.Linq;
using System.Text;
using GitVersion.Helpers;

using JetBrains.Annotations;

using LibGit2Sharp;

static class LibGitExtensions
Expand Down Expand Up @@ -44,13 +47,33 @@ public static SemanticVersion LastVersionTagOnBranch(this Branch branch, IReposi
.FirstOrDefault();
}

public static Commit FindCommitBranchWasBranchedFrom(this Branch branch, IRepository repository, params Branch[] excludedBranches)

public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
{
const string missingTipFormat = "{0} has no tip. Please see http://example.com/docs for information on how to fix this.";

if (branch == null)
{
throw new ArgumentNullException("branch");
}

using (Logger.IndentLog("Finding branch source"))
{
if (branch.Tip == null)
{
Logger.WriteWarning(String.Format(missingTipFormat, branch.Name));
return null;
}

var otherBranches = repository.Branches.Except(excludedBranches).Where(b => IsSameBranch(branch, b)).ToList();
var mergeBases = otherBranches.Select(b =>
{
if (b.Tip == null)
{
Logger.WriteWarning(String.Format(missingTipFormat, b.Name));
return null;
}

var otherCommit = b.Tip;
if (b.Tip.Parents.Contains(branch.Tip))
{
Expand All @@ -63,17 +86,23 @@ public static Commit FindCommitBranchWasBranchedFrom(this Branch branch, IReposi
}
}


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

public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)
public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Commit commit, IRepository repository, bool onlyTrackedBranches)
{
if (commit == null)
{
throw new ArgumentNullException("commit");
}

var directBranchHasBeenFound = false;
foreach (var branch in repository.Branches)
{
if (branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
if (branch.Tip != null && branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
{
continue;
}
Expand Down
5 changes: 3 additions & 2 deletions src/GitVersionCore/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace GitVersion

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

public static Action<string> WriteInfo { get; private set; }
public static Action<string> WriteWarning { get; private set; }
public static Action<string> WriteError { get; private set; }


static Logger()
{
Reset();
Expand All @@ -33,8 +35,7 @@ static Action<string> ObscurePassword(Action<string> info)
{
Action<string> logAction = s =>
{
var rgx = new Regex("(https?://)(.+)(:.+@)");
s = rgx.Replace(s, "$1$2:*******@");
s = ObscurePasswordRegex.Replace(s, "$1$2:*******@");
info(s);
};
return logAction;
Expand Down