Skip to content

Improved support for dynamic repositories #269

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions GitVersion.sln.GhostDoc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Protected>false</Protected>
<Private>false</Private>
<Inherited>true</Inherited>
<InheritedFromReferences>true</InheritedFromReferences>
<EnableTags>false</EnableTags>
<TagList />
</IncludeScopes>
Expand Down
18 changes: 15 additions & 3 deletions GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,25 @@ public static bool LooksLikeAValidPullRequestNumber(string issueNumber)

public static string ExtractIssueNumber(string mergeMessage)
{
// Dynamic: refs/heads/pr/5
// Github Message: refs/heads/pull/5/merge
// Stash Message: refs/heads/pull-requests/5/merge

var regex = new Regex(MergeMessageRegexPattern);
var match = regex.Match(mergeMessage);
// Note by @GeertvanHorrik: sorry, I suck at regex so did a quick hack, feel free to replace by regex
Copy link
Member

Choose a reason for hiding this comment

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

For the MergeMessageRegexPattern you should be able to use something like this

if (mergeMessage.Contains("refs/heads/pr/"))
{
var issueNumber = mergeMessage.Replace("refs/heads/pr/", string.Empty);
return issueNumber;
}
else
{
var regex = new Regex(MergeMessageRegexPattern);
var match = regex.Match(mergeMessage);

return match.Groups["issuenumber"].Value;
var issueNumber = match.Groups["issuenumber"].Value;

return issueNumber;
}
}

static void AddMissingRefSpecs(Repository repo, Remote remote)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GitVersion
using System.Text.RegularExpressions;
using JetBrains.Annotations;

static class ExtensionMethods
static partial class ExtensionMethods
{
public static bool IsOdd(this int number)
{
Expand Down
86 changes: 86 additions & 0 deletions GitVersionCore/Extensions/ExtensionMethods.git.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace GitVersion
{
using System;

static partial class ExtensionMethods
{
public static string GetCanonicalBranchName(this string branchName)
{
if (branchName.IsPullRequest())
{
branchName = branchName.Replace("pull-requests", "pull");
branchName = branchName.Replace("pr", "pull");

return string.Format("refs/{0}/head", branchName);
}

return string.Format("refs/heads/{0}", branchName);
}

public static bool IsHotfix(this string branchName)
{
return branchName.StartsWith("hotfix-") || branchName.StartsWith("hotfix/");
}

public static string GetHotfixSuffix(this string branchName)
{
return branchName.TrimStart("hotfix-").TrimStart("hotfix/");
}

public static bool IsRelease(this string branchName)
{
return branchName.StartsWith("release-") || branchName.StartsWith("release/");
}

public static string GetReleaseSuffix(this string branchName)
{
return branchName.TrimStart("release-").TrimStart("release/");
}

public static string GetUnknownBranchSuffix(this string branchName)
{
var unknownBranchSuffix = branchName.Split('-', '/');
if (unknownBranchSuffix.Length == 1)
return branchName;
return unknownBranchSuffix[1];
}

public static string GetSuffix(this string branchName, BranchType branchType)
{
switch (branchType)
{
case BranchType.Hotfix:
return branchName.GetHotfixSuffix();

case BranchType.Release:
return branchName.GetReleaseSuffix();

case BranchType.Unknown:
return branchName.GetUnknownBranchSuffix();

default:
throw new NotSupportedException(string.Format("Unexpected branch type {0}.", branchType));
}
}

public static bool IsDevelop(this string branchName)
{
return branchName == "develop";
}

public static bool IsMaster(this string branchName)
{
return branchName == "master";
}

public static bool IsPullRequest(this string branchName)
{
return branchName.Contains("pull/") || branchName.Contains("pull-requests/") || branchName.Contains("pr/");
}

public static bool IsSupport(this string branchName)
{
return branchName.ToLower().StartsWith("support-");
}
}
}
33 changes: 26 additions & 7 deletions GitVersionCore/GitFlow/BranchClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,54 @@ namespace GitVersion

static class BranchClassifier
{

public static bool IsHotfix(this Branch branch)
{
return branch.Name.StartsWith("hotfix-") || branch.Name.StartsWith("hotfix/");
return branch.Name.IsHotfix();
}

public static string GetHotfixSuffix(this Branch branch)
{
return branch.Name.GetHotfixSuffix();
}

public static bool IsRelease(this Branch branch)
{
return branch.Name.StartsWith("release-") || branch.Name.StartsWith("release/");
return branch.Name.IsRelease();
}

public static string GetReleaseSuffix(this Branch branch)
{
return branch.Name.GetReleaseSuffix();
}

public static string GetUnknownBranchSuffix(this Branch branch)
{
return branch.Name.GetUnknownBranchSuffix();
}

public static string GetSuffix(this Branch branch, BranchType branchType)
{
return branch.CanonicalName.GetSuffix(branchType);
}

public static bool IsDevelop(this Branch branch)
{
return branch.Name == "develop";
return branch.Name.IsDevelop();
}

public static bool IsMaster(this Branch branch)
{
return branch.Name == "master";
return branch.Name.IsMaster();
}

public static bool IsPullRequest(this Branch branch)
{
return branch.CanonicalName.Contains("/pull/") || branch.CanonicalName.Contains("/pull-requests/");
return branch.CanonicalName.IsPullRequest();
}

public static bool IsSupport(this Branch branch)
{
return branch.Name.ToLower().StartsWith("support-");
return branch.Name.IsSupport();
}
}
}
2 changes: 1 addition & 1 deletion GitVersionCore/GitHubFlow/GitHubFlowVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class GitHubFlowVersionFinder
{
public SemanticVersion FindVersion(GitVersionContext context)
{
var repositoryDirectory = context.Repository.Info.WorkingDirectory;
var repositoryDirectory = context.Repository.GetRepositoryDirectory();
var lastTaggedReleaseFinder = new LastTaggedReleaseFinder(context);
var nextVersionTxtFileFinder = new NextVersionTxtFileFinder(repositoryDirectory);
var nextSemverCalculator = new NextSemverCalculator(nextVersionTxtFileFinder, lastTaggedReleaseFinder, context);
Expand Down
5 changes: 3 additions & 2 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@
<Compile Include="BuildServers\GitHelper.cs" />
<Compile Include="BuildServers\IBuildServer.cs" />
<Compile Include="BuildServers\TeamCity.cs" />
<Compile Include="Extensions\ExtensionMethods.git.cs" />
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
<Compile Include="GitFlow\BranchFinders\VersionOnMasterFinder.cs" />
<Compile Include="LastVersionOnMasterFinder.cs" />
<Compile Include="ShortVersion.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="WarningException.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="Extensions\ExtensionMethods.cs" />
<Compile Include="GitDirFinder.cs" />
<Compile Include="GitFlow\BranchClassifier.cs" />
<Compile Include="GitFlow\BranchFinders\DevelopBasedVersionFinderBase.cs" />
Expand Down Expand Up @@ -110,7 +112,6 @@
<Compile Include="SemanticVersionBuildMetaData.cs" />
<Compile Include="SemanticVersionPreReleaseTag.cs" />
<Compile Include="ShortVersionParser.cs" />
<Compile Include="GitFlow\BranchFinders\VersionOnMasterFinder.cs" />
<Compile Include="VersionPoint.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ public static bool IsDetachedHead(this Branch branch)
return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
}

public static string GetRepositoryDirectory(this IRepository repository)
public static string GetRepositoryDirectory(this IRepository repository, bool omitGitPostFix = true)
{
var gitDirectory = repository.Info.Path;

gitDirectory = gitDirectory.TrimEnd('\\');

if (gitDirectory.EndsWith(".git"))
if (omitGitPostFix && gitDirectory.EndsWith(".git"))
{
gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length);
gitDirectory = gitDirectory.TrimEnd('\\');
}

return gitDirectory;
Expand Down
7 changes: 7 additions & 0 deletions GitVersionCore/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace GitVersion

public class SemanticVersion : IFormattable, IComparable<SemanticVersion>
{
public static SemanticVersion Empty = new SemanticVersion();

static Regex ParseSemVer = new Regex(
@"[vV]?(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?",
RegexOptions.Compiled);
Expand Down Expand Up @@ -34,6 +36,11 @@ public bool Equals(SemanticVersion obj)
BuildMetaData == obj.BuildMetaData;
}

public bool IsEmpty()
{
return Equals(Empty);
}

public static bool operator ==(SemanticVersion v1, SemanticVersion v2)
{
if (ReferenceEquals(v1, null))
Expand Down
71 changes: 71 additions & 0 deletions GitVersionExe.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace GitVersionExe.Tests
{
using System.IO;
using GitVersion;
using LibGit2Sharp;
using NUnit.Framework;

[TestFixture]
public class GitPreparerTests
{
public GitPreparerTests()
{
Logger.WriteInfo = s => { };
Logger.WriteWarning = s => { };
Logger.WriteError = s => { };
}

const string RemoteRepositoryUrl = "https://github.com/ParticularLabs/GitVersion.git";
const string DefaultBranchName = "master";
const string SpecificBranchName = "gh-pages";

[Explicit]
[TestCase(null, DefaultBranchName)]
[TestCase(SpecificBranchName, SpecificBranchName)]
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
{
var tempDir = Path.GetTempPath();

var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = RemoteRepositoryUrl
};

if (!string.IsNullOrWhiteSpace(branchName))
{
arguments.TargetBranch = branchName;
}

var gitPreparer = new GitPreparer(arguments);
var dynamicRepositoryPath = gitPreparer.Prepare();

Assert.AreEqual(Path.Combine(tempDir, "_dynamicrepository", ".git"), dynamicRepositoryPath);
Assert.IsTrue(gitPreparer.IsDynamicGitRepository);

using (var repository = new Repository(dynamicRepositoryPath))
{
var currentBranch = repository.Head.CanonicalName;

Assert.IsTrue(currentBranch.EndsWith(expectedBranchName));
}
}

[Test]
public void WorksCorrectlyWithLocalRepository()
{
var tempDir = Path.GetTempPath();

var arguments = new Arguments
{
TargetPath = tempDir
};

var gitPreparer = new GitPreparer(arguments);
var dynamicRepositoryPath = gitPreparer.Prepare();

Assert.AreEqual(null, dynamicRepositoryPath);
Assert.IsFalse(gitPreparer.IsDynamicGitRepository);
}
}
}
1 change: 1 addition & 0 deletions GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="ArgumentParserTests.cs" />
<Compile Include="ExecCmdLineArgumentTest.cs" />
<Compile Include="ExecutionResults.cs" />
<Compile Include="GitPreparerTests.cs" />
<Compile Include="GitVersionHelper.cs" />
<Compile Include="MsBuildProjectArgTest.cs" />
<Compile Include="PullRequestInTeamCityTest.cs" />
Expand Down
Loading