Skip to content

Commit 6cc0890

Browse files
committed
Fix commit counting scheme
Fix #79
1 parent 943df23 commit 6cc0890

File tree

4 files changed

+248
-11
lines changed

4 files changed

+248
-11
lines changed

GitVersion/GitFlow/BranchFinders/DevelopVersionFinder.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GitVersion
22
{
33
using System.Linq;
4+
using LibGit2Sharp;
45

56
class DevelopVersionFinder
67
{
@@ -22,22 +23,26 @@ public VersionAndBranch FindVersion(GitVersionContext context)
2223

2324
SemanticVersion GetSemanticVersion(GitVersionContext context)
2425
{
26+
var tip = context.CurrentBranch.Tip;
27+
2528
var versionOnMasterFinder = new VersionOnMasterFinder();
26-
var versionFromMaster = versionOnMasterFinder.Execute(context, context.CurrentBranch.Tip.When());
29+
var versionFromMaster = versionOnMasterFinder.Execute(context, tip.When());
30+
31+
var f = new CommitFilter
32+
{
33+
Since = tip,
34+
Until = context.Repository.FindBranch("master").Tip
35+
};
36+
37+
var c = context.Repository.Commits.QueryBy(f);
38+
var preReleasePartOne = c.Count();
2739

28-
var developBranch = context.Repository.FindBranch("develop");
29-
var preReleasePartOne = developBranch.Commits
30-
.SkipWhile(x => x != context.CurrentBranch.Tip)
31-
.TakeWhile(x => x.When() >= versionFromMaster.Timestamp)
32-
.Count();
3340
return new SemanticVersion
3441
{
3542
Major = versionFromMaster.Major,
3643
Minor = versionFromMaster.Minor,
3744
Tag = Stability.Unstable.ToString().ToLower() + preReleasePartOne
3845
};
3946
}
40-
41-
4247
}
43-
}
48+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
using System;
2+
using GitVersion;
3+
using LibGit2Sharp;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
public class VersionByCommitFixture : Lg2sHelperBase
8+
{
9+
/*
10+
* hotfix-1.2.1 -----------C--
11+
* / \
12+
* master A----------------F-----H-------N
13+
* \ / \ /
14+
* hotfix-1.3.1 \ / ----L
15+
* \ / \
16+
* release-1.3.0 \ -D----G--- \
17+
* \ / \ \
18+
* develop -----B----E-------I-----M--O--P
19+
* \ /
20+
* feature -------J-K-
21+
*
22+
*
23+
* - A is tagged `1.2.0`
24+
* - F is tagged `1.2.1`
25+
* - H is tagged `1.3.0`
26+
* - N is tagged `1.3.1`
27+
*/
28+
29+
[Test]
30+
public void CanCorrectlyDetectCommitCountsWhenThatApplies()
31+
{
32+
var repoPath = Clone(CCTestRepoWorkingDirPath);
33+
34+
using (var repo = new Repository(repoPath))
35+
{
36+
ResetToP(repo);
37+
Assert.AreEqual("7", CommitCountFor(repo, "develop"));
38+
39+
ResetToO(repo);
40+
Assert.AreEqual("6", CommitCountFor(repo, "develop"));
41+
42+
ResetToN(repo);
43+
Assert.IsNull(CommitCountFor(repo, "master"));
44+
45+
ResetToM(repo);
46+
Assert.AreEqual("5", CommitCountFor(repo, "develop"));
47+
48+
ResetToL(repo);
49+
Assert.AreEqual("1", CommitCountFor(repo, "hotfix-1.3.1"));
50+
51+
ResetToK(repo);
52+
Assert.IsNull(CommitCountFor(repo, "feature"));
53+
54+
ResetToJ(repo);
55+
Assert.IsNull(CommitCountFor(repo, "feature"));
56+
57+
ResetToI(repo);
58+
Assert.AreEqual("2", CommitCountFor(repo, "develop"));
59+
60+
ResetToH(repo);
61+
Assert.IsNull(CommitCountFor(repo, "master"));
62+
63+
ResetToG(repo);
64+
Assert.AreEqual("2", CommitCountFor(repo, "release-1.3.0"));
65+
66+
ResetToF(repo);
67+
Assert.IsNull(CommitCountFor(repo, "master"));
68+
69+
ResetToE(repo);
70+
Assert.AreEqual("2", CommitCountFor(repo, "develop"));
71+
72+
ResetToD(repo);
73+
Assert.AreEqual("1", CommitCountFor(repo, "release-1.3.0"));
74+
75+
ResetToC(repo);
76+
Assert.AreEqual("1", CommitCountFor(repo, "hotfix-1.2.1"));
77+
78+
ResetToB(repo);
79+
Assert.AreEqual("1", CommitCountFor(repo, "develop"));
80+
}
81+
}
82+
83+
static string CommitCountFor(Repository repo, string branchName)
84+
{
85+
var gvf = new GitVersionFinder();
86+
var context = new GitVersionContext
87+
{
88+
CurrentBranch = repo.Branches[branchName],
89+
Repository = repo
90+
};
91+
92+
var vab = gvf.FindVersion(context);
93+
var v = vab.ToKeyValue();
94+
95+
string number = null;
96+
97+
switch (vab.BranchType)
98+
{
99+
case BranchType.Develop:
100+
number = v["PreReleasePartOne"];
101+
break;
102+
case BranchType.Release:
103+
case BranchType.Hotfix:
104+
number = v["PreReleasePartTwo"];
105+
break;
106+
case BranchType.Master:
107+
case BranchType.Feature:
108+
// We don't care about commit count on those kind of branches
109+
break;
110+
111+
default:
112+
throw new InvalidOperationException(string.Format("Unexpected branch type '{0}'.", vab.BranchType));
113+
}
114+
115+
return number;
116+
}
117+
118+
void DropTags(Repository repo, params string[] names)
119+
{
120+
foreach (var name in names)
121+
{
122+
if (repo.Tags[name] == null)
123+
{
124+
continue;
125+
}
126+
127+
repo.Tags.Remove(name);
128+
}
129+
}
130+
131+
void DropBranches(Repository repo, params string[] names)
132+
{
133+
foreach (var name in names)
134+
{
135+
if (repo.Branches[name] == null)
136+
{
137+
continue;
138+
}
139+
140+
repo.Branches.Remove(name);
141+
}
142+
}
143+
144+
void ResetBranch(Repository repo, string name, string committish)
145+
{
146+
var b = repo.Branches[name];
147+
Assert.IsNotNull(b);
148+
repo.Refs.UpdateTarget(b.CanonicalName, committish);
149+
}
150+
151+
void ResetToP(Repository repo)
152+
{
153+
ResetBranch(repo, "develop", "4d65c519f88773854f9345eaf5dbb30cb49f6a74");
154+
}
155+
156+
void ResetToO(Repository repo)
157+
{
158+
ResetBranch(repo, "develop", "7655537837096d925a4f974232f78ec589d86ebd");
159+
}
160+
161+
void ResetToN(Repository repo)
162+
{
163+
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
164+
}
165+
166+
void ResetToM(Repository repo)
167+
{
168+
ResetBranch(repo, "develop", "0b7a2482ab7d167cefa4ecfc106db001dc5c17ff");
169+
ResetBranch(repo, "master", "5b84136c848fd48f1f8b3fa4e1b767a1f6101279");
170+
DropTags(repo, "1.3.1");
171+
}
172+
173+
void ResetToL(Repository repo)
174+
{
175+
ResetBranch(repo, "develop", "243f56dcdb543688fd0a99bd3e0e72dd9a786603");
176+
}
177+
178+
void ResetToK(Repository repo)
179+
{
180+
DropBranches(repo, "hotfix-1.3.1");
181+
}
182+
183+
void ResetToJ(Repository repo)
184+
{
185+
ResetBranch(repo, "feature", "0491c5dac30d706f4e54c5cb26d082baad8228d1");
186+
}
187+
188+
void ResetToI(Repository repo)
189+
{
190+
DropBranches(repo, "feature");
191+
}
192+
193+
void ResetToH(Repository repo)
194+
{
195+
ResetBranch(repo, "develop", "320f4b6820cf4b0853dc08ac153f04fbd4958200");
196+
}
197+
198+
void ResetToG(Repository repo)
199+
{
200+
ResetBranch(repo, "master", "576a28e321cd6dc764b52c5fface672fa076f37f");
201+
DropTags(repo, "1.3.0");
202+
}
203+
204+
void ResetToF(Repository repo)
205+
{
206+
ResetBranch(repo, "release-1.3.0", "b53054c614d36edc9d1bee8c35cd2ed575a43607");
207+
}
208+
209+
void ResetToE(Repository repo)
210+
{
211+
ResetBranch(repo, "master", "8c890487ed143d5a72d151e64be1c5ddb314c908");
212+
DropTags(repo, "1.2.1");
213+
}
214+
215+
void ResetToD(Repository repo)
216+
{
217+
ResetBranch(repo, "develop", "fab69e28ee35dd912c0c95d5993dd84e4f2bcd92");
218+
}
219+
220+
void ResetToC(Repository repo)
221+
{
222+
DropBranches(repo, "release-1.3.0");
223+
}
224+
225+
void ResetToB(Repository repo)
226+
{
227+
DropBranches(repo, "hotfix-1.2.1");
228+
}
229+
}

Tests/Helpers/Lg2sHelperBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ static Lg2sHelperBase()
3636
}
3737
}
3838

39-
public static string ASBMTestRepoWorkingDirPath;
40-
public static DirectoryInfo ResourcesDirectory;
39+
protected static string ASBMTestRepoWorkingDirPath { private set; get; }
40+
protected static string CCTestRepoWorkingDirPath { private set; get; }
41+
static DirectoryInfo ResourcesDirectory;
4142

4243
static void SetUpTestEnvironment()
4344
{
@@ -54,6 +55,7 @@ static void SetUpTestEnvironment()
5455

5556
// Setup standard paths to our test repositories
5657
ASBMTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "asbm_wd");
58+
CCTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "commit_counting_wd");
5759
}
5860

5961
protected SelfCleaningDirectory BuildSelfCleaningDirectory()

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="BranchFinders\DevelopTests.cs" />
101101
<Compile Include="BuildServers\TeamCityTests.cs" />
102102
<Compile Include="BuildServers\ContinuaCiTests.cs" />
103+
<Compile Include="GitFlow\VersionByCommitFixture.cs" />
103104
<Compile Include="GitFlow\GitFlowVersionFinderTests.cs" />
104105
<Compile Include="GitHelperTests.cs" />
105106
<Compile Include="Helpers\Constants.cs" />

0 commit comments

Comments
 (0)