Skip to content

Commit 1023b0a

Browse files
committed
Merge pull request #208 from GeertvanHorrik/master
Added support for NextVersion.txt in GitFlow and dynamic repositories
2 parents 6f9987a + 105eeeb commit 1023b0a

11 files changed

+125
-6
lines changed

GitVersionCore/GitFlow/BranchFinders/DevelopBasedVersionFinderBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ protected SemanticVersion FindVersion(
3737
context.CurrentBranch.Name, releaseDate)
3838
};
3939

40+
semanticVersion.OverrideVersionManuallyIfNeeded(context.Repository);
41+
4042
return semanticVersion;
4143
}
4244

GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public SemanticVersion FindVersion(GitVersionContext context)
3030
PreReleaseTag = "unstable" + numberOfCommitsSinceRelease,
3131
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceRelease, context.CurrentBranch.Name, releaseDate),
3232
};
33+
34+
semanticVersion.OverrideVersionManuallyIfNeeded(context.Repository);
35+
3336
return semanticVersion;
3437
}
3538
}

GitVersionCore/GitFlow/BranchFinders/MasterVersionFinder.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@ public SemanticVersion FindVersion(IRepository repository, Commit tip)
1717
}
1818
}
1919

20+
var semanticVersion = new SemanticVersion();
21+
2022
string versionString;
2123
if (MergeMessageParser.TryParse(tip, out versionString))
2224
{
2325
if (ShortVersionParser.TryParse(versionString, out major, out minor, out patch))
2426
{
25-
return BuildVersion(repository, tip, major, minor, patch);
27+
semanticVersion = BuildVersion(repository, tip, major, minor, patch);
2628
}
2729
}
2830

29-
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.");
31+
if (semanticVersion == null || semanticVersion.IsEmpty())
32+
{
33+
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.");
34+
}
35+
36+
return semanticVersion;
3037
}
3138

3239
SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int minor, int patch)

GitVersionCore/GitFlow/BranchFinders/SupportVersionFinder.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@ public SemanticVersion FindVersion(IRepository repository, Commit tip)
1717
}
1818
}
1919

20+
var semanticVersion = new SemanticVersion();
21+
2022
string versionString;
2123
if (MergeMessageParser.TryParse(tip, out versionString))
2224
{
2325
if (ShortVersionParser.TryParse(versionString, out major, out minor, out patch))
2426
{
25-
return BuildVersion(repository, tip, major, minor, patch);
27+
semanticVersion = BuildVersion(repository, tip, major, minor, patch);
2628
}
2729
}
2830

29-
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.");
31+
semanticVersion.OverrideVersionManuallyIfNeeded(repository);
32+
33+
if (semanticVersion == null || semanticVersion.IsEmpty())
34+
{
35+
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.");
36+
}
37+
38+
return semanticVersion;
3039
}
3140

3241
SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int minor, int patch)

GitVersionCore/GitHubFlow/NextVersionTxtFileFinder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ public SemanticVersion GetNextVersion()
1919
{
2020
return new SemanticVersion();
2121
}
22-
var version = File.ReadAllText(filePath);
2322

23+
var version = File.ReadAllText(filePath);
2424
if (string.IsNullOrEmpty(version))
25+
{
2526
return new SemanticVersion();
27+
}
2628

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

3135
return semanticVersion;
3236
}

GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ProjectGuid>{F9741A0D-B9D7-4557-9A1C-A7252C1071F5}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>GitVersionCore</RootNamespace>
10+
<RootNamespace>GitVersion</RootNamespace>
1111
<AssemblyName>GitVersionCore</AssemblyName>
1212
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
@@ -66,6 +66,7 @@
6666
<Compile Include="BuildServers\TeamCity.cs" />
6767
<Compile Include="CachedVersion.cs" />
6868
<Compile Include="DirectoryDateFinder.cs" />
69+
<Compile Include="SemanticVersionExtensions.cs" />
6970
<Compile Include="WarningException.cs" />
7071
<Compile Include="ExtensionMethods.cs" />
7172
<Compile Include="GitDirFinder.cs" />

GitVersionCore/LibGitExtensions.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using LibGit2Sharp;
78

@@ -73,5 +74,59 @@ public static bool IsDetachedHead(this Branch branch)
7374
{
7475
return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
7576
}
77+
78+
public static string GetRepositoryDirectory(this IRepository repository)
79+
{
80+
var gitDirectory = repository.Info.Path;
81+
82+
gitDirectory = gitDirectory.TrimEnd('\\');
83+
84+
if (gitDirectory.EndsWith(".git"))
85+
{
86+
gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length);
87+
}
88+
89+
return gitDirectory;
90+
}
91+
92+
public static void CheckoutFilesIfExist(this IRepository repository, params string[] fileNames)
93+
{
94+
if (fileNames == null || fileNames.Length == 0)
95+
{
96+
return;
97+
}
98+
99+
Logger.WriteInfo(string.Format("Checking out files that might be needed later in dynamic repository"));
100+
101+
foreach (var fileName in fileNames)
102+
{
103+
try
104+
{
105+
Logger.WriteInfo(string.Format(" Trying to check out '{0}'", fileName));
106+
107+
var headBranch = repository.Head;
108+
var tip = headBranch.Tip;
109+
110+
var treeEntry = tip[fileName];
111+
if (treeEntry == null)
112+
{
113+
continue;
114+
}
115+
116+
var fullPath = Path.Combine(repository.GetRepositoryDirectory(), fileName);
117+
using (var stream = ((Blob) treeEntry.Target).GetContentStream())
118+
{
119+
using (var streamReader = new BinaryReader(stream))
120+
{
121+
File.WriteAllBytes(fullPath, streamReader.ReadBytes((int)stream.Length));
122+
}
123+
}
124+
}
125+
catch (Exception ex)
126+
{
127+
Logger.WriteWarning(string.Format(" An error occurred while checking out '{0}': '{1}'", fileName, ex.Message));
128+
}
129+
}
130+
}
76131
}
77132
}

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
@"[vV]?(?<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 static bool operator ==(SemanticVersion v1, SemanticVersion v2)
3845
{
3946
if (ReferenceEquals(v1, null))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace GitVersion
2+
{
3+
using LibGit2Sharp;
4+
5+
public static class SemanticVersionExtensions
6+
{
7+
public static void OverrideVersionManuallyIfNeeded(this SemanticVersion version, IRepository repository)
8+
{
9+
var nextVersionTxtFileFinder = new NextVersionTxtFileFinder(repository.GetRepositoryDirectory());
10+
var manualNextVersion = nextVersionTxtFileFinder.GetNextVersion();
11+
if (!manualNextVersion.IsEmpty())
12+
{
13+
if (manualNextVersion > version)
14+
{
15+
version.Major = manualNextVersion.Major;
16+
version.Minor = manualNextVersion.Minor;
17+
version.Patch = manualNextVersion.Patch;
18+
}
19+
}
20+
}
21+
}
22+
}

GitVersionExe/GitPreparer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ string GetGitInfoFromUrl()
7373

7474
repository.Refs.UpdateTarget("HEAD", targetBranchName);
7575
}
76+
77+
repository.CheckoutFilesIfExist("NextVersion.txt");
7678
}
7779
}
7880

Tests/SemanticVersionTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,12 @@ public void LegacySemVerTest()
6161
new SemanticVersionPreReleaseTag("AReallyReallyReallyLongBranchName", 1).ToString("lp").ShouldBe("AReallyReallyRea0001");
6262
}
6363

64+
[Test]
65+
public void EmptyVersion()
66+
{
67+
Assert.IsTrue(SemanticVersion.Empty.IsEmpty());
6468

69+
var emptyVersion = new SemanticVersion();
70+
Assert.IsTrue(emptyVersion.IsEmpty());
71+
}
6572
}

0 commit comments

Comments
 (0)