diff --git a/src/GitVersionCore/BranchConfigurationCalculator.cs b/src/GitVersionCore/BranchConfigurationCalculator.cs index d63f83cf85..10a98ee588 100644 --- a/src/GitVersionCore/BranchConfigurationCalculator.cs +++ b/src/GitVersionCore/BranchConfigurationCalculator.cs @@ -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 @@ -35,11 +38,22 @@ public static KeyValuePair GetBranchConfiguration(Commit c throw new Exception(string.Format(format, currentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key)))); } - static KeyValuePair[] LookupBranchConfiguration(Config config, Branch currentBranch) + static KeyValuePair[] 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 InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair keyValuePair, BranchConfig branchConfiguration, Config config, IList excludedInheritBranches) { using (Logger.IndentLog("Attempting to inherit branch configuration from parent branch")) @@ -104,11 +118,15 @@ static KeyValuePair 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( keyValuePair.Key, new BranchConfig(branchConfiguration) diff --git a/src/GitVersionCore/LibGitExtensions.cs b/src/GitVersionCore/LibGitExtensions.cs index c50f1744ed..2bc9f6a4f6 100644 --- a/src/GitVersionCore/LibGitExtensions.cs +++ b/src/GitVersionCore/LibGitExtensions.cs @@ -6,6 +6,9 @@ namespace GitVersion using System.Linq; using System.Text; using GitVersion.Helpers; + + using JetBrains.Annotations; + using LibGit2Sharp; static class LibGitExtensions @@ -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)) { @@ -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 GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches) + public static IEnumerable 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; } diff --git a/src/GitVersionCore/Logger.cs b/src/GitVersionCore/Logger.cs index 6acffd0c10..d880b0f9cc 100644 --- a/src/GitVersionCore/Logger.cs +++ b/src/GitVersionCore/Logger.cs @@ -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 WriteInfo { get; private set; } public static Action WriteWarning { get; private set; } public static Action WriteError { get; private set; } + static Logger() { Reset(); @@ -33,8 +35,7 @@ static Action ObscurePassword(Action info) { Action logAction = s => { - var rgx = new Regex("(https?://)(.+)(:.+@)"); - s = rgx.Replace(s, "$1$2:*******@"); + s = ObscurePasswordRegex.Replace(s, "$1$2:*******@"); info(s); }; return logAction;