Skip to content

Pr/dynamic #350

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 4 commits into from
Jan 17, 2015
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
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: 10 additions & 8 deletions GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GitVersion

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

public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication)
{
Expand All @@ -32,14 +32,14 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha));
return;
}

Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", headSha));

// 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.
// If they do, go ahead and checkout that branch
// If no, go ahead and check out a new branch, using the known commit SHA as the pointer
var localBranchesWhereCommitShaIsHead = repo.Branches.Where(b => !b.IsRemote && b.Tip.Sha == headSha).ToList();

if (localBranchesWhereCommitShaIsHead.Count > 1)
{
var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName));
Expand All @@ -55,7 +55,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
else
{
Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", localBranchesWhereCommitShaIsHead[0].Name));
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
}
}
}
Expand All @@ -73,13 +73,15 @@ 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);

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

return issueNumber;
}

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

if (!string.IsNullOrEmpty(username))
{
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
{
Username = username,
Password = password
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-") || 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-") || branch.Name.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, context.Configuration);
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 @@ -73,14 +73,15 @@
<Compile Include="Configuration\Config.cs" />
<Compile Include="Configuration\ConfigReader.cs" />
<Compile Include="Configuration\ConfigurationProvider.cs" />
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="LastMinorVersionFinder.cs" />
<Compile Include="Extensions\ExtensionMethods.git.cs" />
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.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
5 changes: 3 additions & 2 deletions GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,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(
@"(?<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 override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
Expand Down
75 changes: 75 additions & 0 deletions GitVersionExe.Tests/GitPreparerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 DefaultBranchName = "master";
const string SpecificBranchName = "feature/foo";

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

using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeCommits(5);
fixture.Repository.CreateBranch("feature/foo");
var arguments = new Arguments
{
TargetPath = tempDir,
TargetUrl = fixture.RepositoryPath
};

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 @@ -80,6 +80,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