Skip to content

Fix/release issues #1342

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
Dec 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void Build(string configuration, string nugetVersion, string semVersion, string
}
}

// This build task can be run to just build
Task("DogfoodBuild")
.IsDependentOn("NuGet-Package-Restore")
.Does(() =>
Expand Down Expand Up @@ -92,7 +93,7 @@ Task("Build")
});

Task("Run-NUnit-Tests")
.IsDependentOn("Build")
.IsDependentOn("DogfoodBuild")
.Does(() =>
{
var settings = new NUnit3Settings();
Expand All @@ -113,6 +114,7 @@ Task("Run-NUnit-Tests")
});

Task("Zip-Files")
.IsDependentOn("Build")
.IsDependentOn("Run-NUnit-Tests")
.Does(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void ShouldNotAllowIncrementOfVersion()
[TestCase("Finish 0.14.1", true, "0.14.1")] //Support Syntevo SmartGit/Hg's Gitflow merge commit messages for finishing a 'Hotfix' branch
[TestCase("Merge branch 'Release-v0.2.0'", true, "0.2.0")]
[TestCase("Merge branch 'Release-v2.2'", true, "2.2.0")]
[TestCase("Merge remote-tracking branch 'origin/release/0.8.0' into develop/master", true, "0.8.0")]
public void AssertMergeMessage(string message, bool isMergeCommit, string expectedVersion)
{
var parents = GetParents(isMergeCommit);
Expand Down Expand Up @@ -121,6 +122,7 @@ static void AssertMergeMessage(string message, string expectedVersion, List<Comm
}
else
{
baseVersion.ShouldNotBeNull();
baseVersion.SemanticVersion.ToString().ShouldBe(expectedVersion);
}
}
Expand Down
39 changes: 26 additions & 13 deletions src/GitVersionCore/BranchConfigurationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BranchConfigurationCalculator
/// </summary>
public static BranchConfig GetBranchConfiguration(GitVersionContext context, Branch targetBranch, IList<Branch> excludedInheritBranches = null)
{
var matchingBranches = context.FullConfiguration.GetConfigForBranch(targetBranch.FriendlyName);
var matchingBranches = context.FullConfiguration.GetConfigForBranch(targetBranch.NameWithoutRemote());

if (matchingBranches == null)
{
Expand All @@ -27,11 +27,20 @@ public static BranchConfig GetBranchConfiguration(GitVersionContext context, Bra
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, matchingBranches, "", new List<string>());
}

return matchingBranches.Increment == IncrementStrategy.Inherit ?
InheritBranchConfiguration(context, targetBranch, matchingBranches, excludedInheritBranches) :
matchingBranches;
if (matchingBranches.Increment == IncrementStrategy.Inherit)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't happen now because we filter the conditions which would cause this code to be executed. But still, I think it's worth keeping in just in case

{
matchingBranches = InheritBranchConfiguration(context, targetBranch, matchingBranches, excludedInheritBranches);
if (matchingBranches.Name == FallbackConfigName && matchingBranches.Increment == IncrementStrategy.Inherit)
{
// We tried, and failed to inherit, just fall back to patch
matchingBranches.Increment = IncrementStrategy.Patch;
}
}

return matchingBranches;
}

// TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
static BranchConfig InheritBranchConfiguration(GitVersionContext context, Branch targetBranch, BranchConfig branchConfiguration, IList<Branch> excludedInheritBranches)
{
var repository = context.Repository;
Expand All @@ -50,17 +59,17 @@ static BranchConfig InheritBranchConfiguration(GitVersionContext context, Branch
{
excludedInheritBranches = repository.Branches.Where(b =>
{
var branchConfig = config.GetConfigForBranch(b.FriendlyName);
var branchConfig = config.GetConfigForBranch(b.NameWithoutRemote());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I think is the real fix. This will now exclude local and remote branches which do not have config, or the config is Inherit.

Previously we only excluded branches which had config and it was Inherit, but our infinite loops were caused by branches without config..


return branchConfig != null && branchConfig.Increment == IncrementStrategy.Inherit;
return branchConfig == null || branchConfig.Increment == IncrementStrategy.Inherit;
}).ToList();
}
// Add new excluded branches.
foreach (var excludedBranch in excludedBranches.ExcludingBranches(excludedInheritBranches))
{
excludedInheritBranches.Add(excludedBranch);
}
var branchesToEvaluate = repository.Branches.Except(excludedInheritBranches).ToList();
var branchesToEvaluate = repository.Branches.ExcludingBranches(excludedInheritBranches).ToList();

var branchPoint = context.RepositoryMetadataProvider
.FindCommitBranchWasBranchedFrom(targetBranch, excludedInheritBranches.ToArray());
Expand Down Expand Up @@ -94,13 +103,17 @@ static BranchConfig InheritBranchConfiguration(GitVersionContext context, Branch
if (possibleParents.Count == 1)
{
var branchConfig = GetBranchConfiguration(context, possibleParents[0], excludedInheritBranches);
return new BranchConfig(branchConfiguration)
// If we have resolved a fallback config we should not return that we have got config
if (branchConfig.Name != FallbackConfigName)
{
Increment = branchConfig.Increment,
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
// If we are inheriting from develop then we should behave like develop
TracksReleaseBranches = branchConfig.TracksReleaseBranches
};
return new BranchConfig(branchConfiguration)
{
Increment = branchConfig.Increment,
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
// If we are inheriting from develop then we should behave like develop
TracksReleaseBranches = branchConfig.TracksReleaseBranches
};
}
}

// 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
Expand Down
8 changes: 4 additions & 4 deletions src/GitVersionCore/GitRepoMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public BranchCommit FindCommitBranchWasBranchedFrom(Branch branch, params Branch
return BranchCommit.Empty;
}

var possibleBranches = GetMergeCommitsForBranch(branch)
.ExcludingBranches(excludedBranches)
var possibleBranches = GetMergeCommitsForBranch(branch, excludedBranches)
.Where(b => !branch.IsSameBranch(b.Branch))
.ToList();

Expand All @@ -216,7 +215,7 @@ public BranchCommit FindCommitBranchWasBranchedFrom(Branch branch, params Branch
}
}

List<BranchCommit> GetMergeCommitsForBranch(Branch branch)
List<BranchCommit> GetMergeCommitsForBranch(Branch branch, Branch[] excludedBranches)
{
if (mergeBaseCommitsCache.ContainsKey(branch))
{
Expand All @@ -226,11 +225,12 @@ List<BranchCommit> GetMergeCommitsForBranch(Branch branch)
return mergeBaseCommitsCache[branch];
}

var currentBranchConfig = configuration.GetConfigForBranch(branch.FriendlyName);
var currentBranchConfig = configuration.GetConfigForBranch(branch.NameWithoutRemote());
var regexesToCheck = currentBranchConfig == null
? new [] { ".*" } // Match anything if we can't find a branch config
: currentBranchConfig.SourceBranches.Select(sb => configuration.Branches[sb].Regex);
var branchMergeBases = Repository.Branches
.ExcludingBranches(excludedBranches)
.Where(b =>
{
if (b == branch) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static Regex CreateRegex(string pattern)

public static VersionField FindDefaultIncrementForBranch( GitVersionContext context, string branch = null )
{
var config = context.FullConfiguration.GetConfigForBranch(branch ?? context.CurrentBranch.FriendlyName);
var config = context.FullConfiguration.GetConfigForBranch(branch ?? context.CurrentBranch.NameWithoutRemote());
if ( config?.Increment != null && config.Increment != IncrementStrategy.Inherit )
{
return config.Increment.Value.ToVersionField();
Expand Down
18 changes: 12 additions & 6 deletions src/GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ public static DateTimeOffset When(this Commit commit)
/// <summary>
/// Checks if the two branch objects refer to the same branch (have the same friendly name).
/// </summary>
public static bool IsSameBranch(this Branch branch, Branch otherBranch)
public static string NameWithoutRemote(this Branch branch)
{
// For each branch, fixup the friendly name if the branch is remote.
var otherBranchFriendlyName = otherBranch.IsRemote ?
otherBranch.FriendlyName.Substring(otherBranch.FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1) :
otherBranch.FriendlyName;
var branchFriendlyName = branch.IsRemote ?
return branch.IsRemote ?
branch.FriendlyName.Substring(branch.FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1) :
branch.FriendlyName;
}

/// <summary>
/// Checks if the two branch objects refer to the same branch (have the same friendly name).
/// </summary>
public static bool IsSameBranch(this Branch branch, Branch otherBranch)
{
// For each branch, fixup the friendly name if the branch is remote.
var otherBranchFriendlyName = otherBranch.NameWithoutRemote();
var branchFriendlyName = branch.NameWithoutRemote();

return otherBranchFriendlyName == branchFriendlyName;
}
Expand Down
13 changes: 12 additions & 1 deletion src/GitVersionCore/MergeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class MergeMessage
static Regex smartGitMergeMessage = new Regex(
@"^Finish (?<Branch>.*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
static Regex parseRemoteTrackingMergeMessage = new Regex(
@"^Merge remote-tracking branch '(?<SourceBranch>.*)' into (?<TargetBranch>.*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

private string mergeMessage;
private Config config;

Expand Down Expand Up @@ -73,7 +77,14 @@ private string ParseBranch()
PullRequestNumber = pullNumber;
}
var from = match.Groups["Source"].Value;
// We could remove/separate the remote name at this point?
// TODO We could remove/separate the remote name at this point?
return from;
}

match = parseRemoteTrackingMergeMessage.Match(mergeMessage);
if (match.Success) {
var from = match.Groups["SourceBranch"].Value;
// TODO We could remove/separate the remote name at this point?
return from;
}

Expand Down