Skip to content

Commit ca002ed

Browse files
committed
Merge pull request #350 from JakeGinnivan/pr/dynamic
Pr/dynamic
2 parents 74a36ee + dd368aa commit ca002ed

File tree

13 files changed

+274
-33
lines changed

13 files changed

+274
-33
lines changed

GitVersion.sln.GhostDoc.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Protected>false</Protected>
2424
<Private>false</Private>
2525
<Inherited>true</Inherited>
26+
<InheritedFromReferences>true</InheritedFromReferences>
2627
<EnableTags>false</EnableTags>
2728
<TagList />
2829
</IncludeScopes>

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace GitVersion
77

88
public static class GitHelper
99
{
10-
const string MergeMessageRegexPattern = "refs/heads/pull(-requests)?/(?<issuenumber>[0-9]*)/merge";
10+
const string MergeMessageRegexPattern = "refs/heads/(pr|pull(-requests)?/(?<issuenumber>[0-9]*)/merge)";
1111

1212
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication)
1313
{
@@ -32,14 +32,14 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
3232
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha));
3333
return;
3434
}
35-
35+
3636
Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", headSha));
37-
37+
3838
// In order to decide whether a fake branch is required or not, first check to see if any local branches have the same commit SHA of the head SHA.
3939
// If they do, go ahead and checkout that branch
4040
// If no, go ahead and check out a new branch, using the known commit SHA as the pointer
4141
var localBranchesWhereCommitShaIsHead = repo.Branches.Where(b => !b.IsRemote && b.Tip.Sha == headSha).ToList();
42-
42+
4343
if (localBranchesWhereCommitShaIsHead.Count > 1)
4444
{
4545
var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName));
@@ -55,7 +55,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
5555
else
5656
{
5757
Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", localBranchesWhereCommitShaIsHead[0].Name));
58-
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
58+
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
5959
}
6060
}
6161
}
@@ -73,13 +73,15 @@ public static bool LooksLikeAValidPullRequestNumber(string issueNumber)
7373

7474
public static string ExtractIssueNumber(string mergeMessage)
7575
{
76+
// Dynamic: refs/heads/pr/5
7677
// Github Message: refs/heads/pull/5/merge
7778
// Stash Message: refs/heads/pull-requests/5/merge
78-
7979
var regex = new Regex(MergeMessageRegexPattern);
8080
var match = regex.Match(mergeMessage);
8181

82-
return match.Groups["issuenumber"].Value;
82+
var issueNumber = match.Groups["issuenumber"].Value;
83+
84+
return issueNumber;
8385
}
8486

8587
static void AddMissingRefSpecs(Repository repo, Remote remote)
@@ -101,7 +103,7 @@ static FetchOptions BuildFetchOptions(string username, string password)
101103

102104
if (!string.IsNullOrEmpty(username))
103105
{
104-
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
106+
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
105107
{
106108
Username = username,
107109
Password = password

GitVersionCore/ExtensionMethods.cs renamed to GitVersionCore/Extensions/ExtensionMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitVersion
66
using System.Text.RegularExpressions;
77
using JetBrains.Annotations;
88

9-
static class ExtensionMethods
9+
static partial class ExtensionMethods
1010
{
1111
public static bool IsOdd(this int number)
1212
{
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
5+
static partial class ExtensionMethods
6+
{
7+
public static string GetCanonicalBranchName(this string branchName)
8+
{
9+
if (branchName.IsPullRequest())
10+
{
11+
branchName = branchName.Replace("pull-requests", "pull");
12+
branchName = branchName.Replace("pr", "pull");
13+
14+
return string.Format("refs/{0}/head", branchName);
15+
}
16+
17+
return string.Format("refs/heads/{0}", branchName);
18+
}
19+
20+
public static bool IsHotfix(this string branchName)
21+
{
22+
return branchName.StartsWith("hotfix-") || branchName.StartsWith("hotfix/");
23+
}
24+
25+
public static string GetHotfixSuffix(this string branchName)
26+
{
27+
return branchName.TrimStart("hotfix-").TrimStart("hotfix/");
28+
}
29+
30+
public static bool IsRelease(this string branchName)
31+
{
32+
return branchName.StartsWith("release-") || branchName.StartsWith("release/");
33+
}
34+
35+
public static string GetReleaseSuffix(this string branchName)
36+
{
37+
return branchName.TrimStart("release-").TrimStart("release/");
38+
}
39+
40+
public static string GetUnknownBranchSuffix(this string branchName)
41+
{
42+
var unknownBranchSuffix = branchName.Split('-', '/');
43+
if (unknownBranchSuffix.Length == 1)
44+
return branchName;
45+
return unknownBranchSuffix[1];
46+
}
47+
48+
public static string GetSuffix(this string branchName, BranchType branchType)
49+
{
50+
switch (branchType)
51+
{
52+
case BranchType.Hotfix:
53+
return branchName.GetHotfixSuffix();
54+
55+
case BranchType.Release:
56+
return branchName.GetReleaseSuffix();
57+
58+
case BranchType.Unknown:
59+
return branchName.GetUnknownBranchSuffix();
60+
61+
default:
62+
throw new NotSupportedException(string.Format("Unexpected branch type {0}.", branchType));
63+
}
64+
}
65+
66+
public static bool IsDevelop(this string branchName)
67+
{
68+
return branchName == "develop";
69+
}
70+
71+
public static bool IsMaster(this string branchName)
72+
{
73+
return branchName == "master";
74+
}
75+
76+
public static bool IsPullRequest(this string branchName)
77+
{
78+
return branchName.Contains("pull/") || branchName.Contains("pull-requests/") || branchName.Contains("pr/");
79+
}
80+
81+
public static bool IsSupport(this string branchName)
82+
{
83+
return branchName.ToLower().StartsWith("support-") || branchName.ToLower().StartsWith("support/");
84+
}
85+
}
86+
}

GitVersionCore/GitFlow/BranchClassifier.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,54 @@ namespace GitVersion
44

55
static class BranchClassifier
66
{
7-
87
public static bool IsHotfix(this Branch branch)
98
{
10-
return branch.Name.StartsWith("hotfix-") || branch.Name.StartsWith("hotfix/");
9+
return branch.Name.IsHotfix();
10+
}
11+
12+
public static string GetHotfixSuffix(this Branch branch)
13+
{
14+
return branch.Name.GetHotfixSuffix();
1115
}
1216

1317
public static bool IsRelease(this Branch branch)
1418
{
15-
return branch.Name.StartsWith("release-") || branch.Name.StartsWith("release/");
19+
return branch.Name.IsRelease();
20+
}
21+
22+
public static string GetReleaseSuffix(this Branch branch)
23+
{
24+
return branch.Name.GetReleaseSuffix();
25+
}
26+
27+
public static string GetUnknownBranchSuffix(this Branch branch)
28+
{
29+
return branch.Name.GetUnknownBranchSuffix();
30+
}
31+
32+
public static string GetSuffix(this Branch branch, BranchType branchType)
33+
{
34+
return branch.CanonicalName.GetSuffix(branchType);
1635
}
1736

1837
public static bool IsDevelop(this Branch branch)
1938
{
20-
return branch.Name == "develop";
39+
return branch.Name.IsDevelop();
2140
}
2241

2342
public static bool IsMaster(this Branch branch)
2443
{
25-
return branch.Name == "master";
44+
return branch.Name.IsMaster();
2645
}
2746

2847
public static bool IsPullRequest(this Branch branch)
2948
{
30-
return branch.CanonicalName.Contains("/pull/") || branch.CanonicalName.Contains("/pull-requests/");
49+
return branch.CanonicalName.IsPullRequest();
3150
}
3251

3352
public static bool IsSupport(this Branch branch)
3453
{
35-
return branch.Name.ToLower().StartsWith("support-") || branch.Name.StartsWith("support/");
54+
return branch.Name.IsSupport();
3655
}
3756
}
3857
}

GitVersionCore/GitHubFlow/GitHubFlowVersionFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class GitHubFlowVersionFinder
44
{
55
public SemanticVersion FindVersion(GitVersionContext context)
66
{
7-
var repositoryDirectory = context.Repository.Info.WorkingDirectory;
7+
var repositoryDirectory = context.Repository.GetRepositoryDirectory();
88
var lastTaggedReleaseFinder = new LastTaggedReleaseFinder(context);
99
var nextVersionTxtFileFinder = new NextVersionTxtFileFinder(repositoryDirectory, context.Configuration);
1010
var nextSemverCalculator = new NextSemverCalculator(nextVersionTxtFileFinder, lastTaggedReleaseFinder, context);

GitVersionCore/GitVersionCore.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@
7373
<Compile Include="Configuration\Config.cs" />
7474
<Compile Include="Configuration\ConfigReader.cs" />
7575
<Compile Include="Configuration\ConfigurationProvider.cs" />
76-
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
7776
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
7877
<Compile Include="Helpers\FileSystem.cs" />
7978
<Compile Include="Helpers\IFileSystem.cs" />
8079
<Compile Include="LastMinorVersionFinder.cs" />
80+
<Compile Include="Extensions\ExtensionMethods.git.cs" />
81+
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
8182
<Compile Include="SemanticVersionExtensions.cs" />
8283
<Compile Include="WarningException.cs" />
83-
<Compile Include="ExtensionMethods.cs" />
84+
<Compile Include="Extensions\ExtensionMethods.cs" />
8485
<Compile Include="GitDirFinder.cs" />
8586
<Compile Include="GitFlow\BranchClassifier.cs" />
8687
<Compile Include="GitFlow\BranchFinders\DevelopBasedVersionFinderBase.cs" />

GitVersionCore/LibGitExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ public static bool IsDetachedHead(this Branch branch)
7878
return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
7979
}
8080

81-
public static string GetRepositoryDirectory(this IRepository repository)
81+
public static string GetRepositoryDirectory(this IRepository repository, bool omitGitPostFix = true)
8282
{
8383
var gitDirectory = repository.Info.Path;
8484

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

87-
if (gitDirectory.EndsWith(".git"))
87+
if (omitGitPostFix && gitDirectory.EndsWith(".git"))
8888
{
8989
gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length);
90+
gitDirectory = gitDirectory.TrimEnd('\\');
9091
}
9192

9293
return gitDirectory;

GitVersionCore/SemanticVersion.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace GitVersion
55

66
public class SemanticVersion : IFormattable, IComparable<SemanticVersion>
77
{
8+
public static SemanticVersion Empty = new SemanticVersion();
9+
810
static Regex ParseSemVer = new Regex(
911
@"(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?",
1012
RegexOptions.Compiled);
@@ -34,6 +36,11 @@ public bool Equals(SemanticVersion obj)
3436
BuildMetaData == obj.BuildMetaData;
3537
}
3638

39+
public bool IsEmpty()
40+
{
41+
return Equals(Empty);
42+
}
43+
3744
public override bool Equals(object obj)
3845
{
3946
if (ReferenceEquals(null, obj))
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace GitVersionExe.Tests
2+
{
3+
using System.IO;
4+
using GitVersion;
5+
using LibGit2Sharp;
6+
using NUnit.Framework;
7+
8+
[TestFixture]
9+
public class GitPreparerTests
10+
{
11+
public GitPreparerTests()
12+
{
13+
Logger.WriteInfo = s => { };
14+
Logger.WriteWarning = s => { };
15+
Logger.WriteError = s => { };
16+
}
17+
18+
const string DefaultBranchName = "master";
19+
const string SpecificBranchName = "feature/foo";
20+
21+
[Test]
22+
[TestCase(null, DefaultBranchName)]
23+
[TestCase(SpecificBranchName, SpecificBranchName)]
24+
public void WorksCorrectlyWithRemoteRepository(string branchName, string expectedBranchName)
25+
{
26+
var tempDir = Path.GetTempPath();
27+
28+
using (var fixture = new EmptyRepositoryFixture(new Config()))
29+
{
30+
fixture.Repository.MakeCommits(5);
31+
fixture.Repository.CreateBranch("feature/foo");
32+
var arguments = new Arguments
33+
{
34+
TargetPath = tempDir,
35+
TargetUrl = fixture.RepositoryPath
36+
};
37+
38+
if (!string.IsNullOrWhiteSpace(branchName))
39+
{
40+
arguments.TargetBranch = branchName;
41+
}
42+
43+
var gitPreparer = new GitPreparer(arguments);
44+
var dynamicRepositoryPath = gitPreparer.Prepare();
45+
46+
Assert.AreEqual(Path.Combine(tempDir, "_dynamicrepository", ".git"), dynamicRepositoryPath);
47+
Assert.IsTrue(gitPreparer.IsDynamicGitRepository);
48+
49+
using (var repository = new Repository(dynamicRepositoryPath))
50+
{
51+
var currentBranch = repository.Head.CanonicalName;
52+
53+
Assert.IsTrue(currentBranch.EndsWith(expectedBranchName));
54+
}
55+
}
56+
}
57+
58+
[Test]
59+
public void WorksCorrectlyWithLocalRepository()
60+
{
61+
var tempDir = Path.GetTempPath();
62+
63+
var arguments = new Arguments
64+
{
65+
TargetPath = tempDir
66+
};
67+
68+
var gitPreparer = new GitPreparer(arguments);
69+
var dynamicRepositoryPath = gitPreparer.Prepare();
70+
71+
Assert.AreEqual(null, dynamicRepositoryPath);
72+
Assert.IsFalse(gitPreparer.IsDynamicGitRepository);
73+
}
74+
}
75+
}

GitVersionExe.Tests/GitVersionExe.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="ArgumentParserTests.cs" />
8181
<Compile Include="ExecCmdLineArgumentTest.cs" />
8282
<Compile Include="ExecutionResults.cs" />
83+
<Compile Include="GitPreparerTests.cs" />
8384
<Compile Include="GitVersionHelper.cs" />
8485
<Compile Include="MsBuildProjectArgTest.cs" />
8586
<Compile Include="PullRequestInTeamCityTest.cs" />

0 commit comments

Comments
 (0)