Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected SemanticVersion FindVersion(
context.CurrentBranch.Name, releaseDate)
};

semanticVersion.OverrideVersionManuallyIfNeeded(context.Repository);

return semanticVersion;
}

Expand Down
3 changes: 3 additions & 0 deletions GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public SemanticVersion FindVersion(GitVersionContext context)
PreReleaseTag = "unstable" + numberOfCommitsSinceRelease,
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceRelease, context.CurrentBranch.Name, releaseDate),
};

semanticVersion.OverrideVersionManuallyIfNeeded(context.Repository);

return semanticVersion;
}
}
Expand Down
11 changes: 9 additions & 2 deletions GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ public SemanticVersion FindVersion(IRepository repository, Commit tip)
}
}

var semanticVersion = new SemanticVersion();

string versionString;
if (MergeMessageParser.TryParse(tip, out versionString))
{
if (ShortVersionParser.TryParse(versionString, out major, out minor, out patch))
{
return BuildVersion(repository, tip, major, minor, patch);
semanticVersion = BuildVersion(repository, tip, major, minor, patch);
}
}

throw new WarningException("The head of master should always be a merge commit if you follow gitflow. Please create one or work around this by tagging the commit with SemVer compatible Id.");
if (semanticVersion == null || semanticVersion.IsEmpty())
{
throw new WarningException("The head of master should always be a merge commit if you follow gitflow. Please create one or work around this by tagging the commit with SemVer compatible Id.");
}

return semanticVersion;
}

SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int minor, int patch)
Expand Down
13 changes: 11 additions & 2 deletions GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ public SemanticVersion FindVersion(IRepository repository, Commit tip)
}
}

var semanticVersion = new SemanticVersion();

string versionString;
if (MergeMessageParser.TryParse(tip, out versionString))
{
if (ShortVersionParser.TryParse(versionString, out major, out minor, out patch))
{
return BuildVersion(repository, tip, major, minor, patch);
semanticVersion = BuildVersion(repository, tip, major, minor, patch);
}
}

throw new WarningException("The head of a support branch should always be a merge commit if you follow gitflow. Please create one or work around this by tagging the commit with SemVer compatible Id.");
semanticVersion.OverrideVersionManuallyIfNeeded(repository);

if (semanticVersion == null || semanticVersion.IsEmpty())
{
throw new WarningException("The head of a support branch should always be a merge commit if you follow gitflow. Please create one or work around this by tagging the commit with SemVer compatible Id.");
}

return semanticVersion;
}

SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int minor, int patch)
Expand Down
6 changes: 5 additions & 1 deletion GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public SemanticVersion GetNextVersion()
{
return new SemanticVersion();
}
var version = File.ReadAllText(filePath);

var version = File.ReadAllText(filePath);
if (string.IsNullOrEmpty(version))
{
return new SemanticVersion();
}

SemanticVersion semanticVersion;
if (!SemanticVersion.TryParse(version, out semanticVersion))
{
throw new ArgumentException("Make sure you have a valid semantic version in NextVersion.txt");
}

return semanticVersion;
}
Expand Down
3 changes: 2 additions & 1 deletion GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ProjectGuid>{F9741A0D-B9D7-4557-9A1C-A7252C1071F5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GitVersionCore</RootNamespace>
<RootNamespace>GitVersion</RootNamespace>
<AssemblyName>GitVersionCore</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
Expand Down Expand Up @@ -66,6 +66,7 @@
<Compile Include="BuildServers\TeamCity.cs" />
<Compile Include="CachedVersion.cs" />
<Compile Include="DirectoryDateFinder.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="WarningException.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="GitDirFinder.cs" />
Expand Down
55 changes: 55 additions & 0 deletions GitVersionCore/LibGitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace GitVersion
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp;

Expand Down Expand Up @@ -73,5 +74,59 @@ public static bool IsDetachedHead(this Branch branch)
{
return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
}

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

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

if (gitDirectory.EndsWith(".git"))
{
gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length);
}

return gitDirectory;
}

public static void CheckoutFilesIfExist(this IRepository repository, params string[] fileNames)
{
if (fileNames == null || fileNames.Length == 0)
{
return;
}

Logger.WriteInfo(string.Format("Checking out files that might be needed later in dynamic repository"));

foreach (var fileName in fileNames)
{
try
{
Logger.WriteInfo(string.Format(" Trying to check out '{0}'", fileName));

var headBranch = repository.Head;
var tip = headBranch.Tip;

var treeEntry = tip[fileName];
if (treeEntry == null)
{
continue;
}

var fullPath = Path.Combine(repository.GetRepositoryDirectory(), fileName);
using (var stream = ((Blob) treeEntry.Target).GetContentStream())
{
using (var streamReader = new BinaryReader(stream))
{
File.WriteAllBytes(fullPath, streamReader.ReadBytes((int)stream.Length));
}
}
}
catch (Exception ex)
{
Logger.WriteWarning(string.Format(" An error occurred while checking out '{0}': '{1}'", fileName, ex.Message));
}
}
}
}
}
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()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice addition

{
return Equals(Empty);
}

public static bool operator ==(SemanticVersion v1, SemanticVersion v2)
{
if (ReferenceEquals(v1, null))
Expand Down
22 changes: 22 additions & 0 deletions GitVersionCore/SemanticVersionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace GitVersion
{
using LibGit2Sharp;

public static class SemanticVersionExtensions
{
public static void OverrideVersionManuallyIfNeeded(this SemanticVersion version, IRepository repository)
{
var nextVersionTxtFileFinder = new NextVersionTxtFileFinder(repository.GetRepositoryDirectory());
var manualNextVersion = nextVersionTxtFileFinder.GetNextVersion();
Copy link
Contributor

Choose a reason for hiding this comment

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

This should only override if it is bigger. Otherwise when convention gets higher (release branch merged in for instance) this could cause a version drop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix now.

if (!manualNextVersion.IsEmpty())
{
if (manualNextVersion > version)
{
version.Major = manualNextVersion.Major;
version.Minor = manualNextVersion.Minor;
version.Patch = manualNextVersion.Patch;
}
}
}
}
}
2 changes: 2 additions & 0 deletions GitVersionExe/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ string GetGitInfoFromUrl()

repository.Refs.UpdateTarget("HEAD", targetBranchName);
}

repository.CheckoutFilesIfExist("NextVersion.txt");
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/SemanticVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,12 @@ public void LegacySemVerTest()
new SemanticVersionPreReleaseTag("AReallyReallyReallyLongBranchName", 1).ToString("lp").ShouldBe("AReallyReallyRea0001");
}

[Test]
public void EmptyVersion()
{
Assert.IsTrue(SemanticVersion.Empty.IsEmpty());

var emptyVersion = new SemanticVersion();
Assert.IsTrue(emptyVersion.IsEmpty());
}
}