Skip to content

Commit ddfe4b1

Browse files
committed
Introduced ConfigurationProvider and added config parameter into GitVersionContext
1 parent bbe60f5 commit ddfe4b1

24 files changed

+146
-87
lines changed

GitVersionCore.Tests/CommitCountingRepoFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
22
using System.IO;
3+
using GitVersion.Configuration;
34
using LibGit2Sharp;
45

56
public class CommitCountingRepoFixture : RepositoryFixtureBase
67
{
78
public CommitCountingRepoFixture() :
8-
base(CloneTestRepo)
9+
base(CloneTestRepo, new Config())
910
{
1011
}
1112

GitVersionCore.Tests/ConfigReaderTests.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using GitVersion;
33
using GitVersion.Configuration;
44
using NUnit.Framework;
5+
using Shouldly;
56

67
[TestFixture]
78
public class ConfigReaderTests
@@ -10,16 +11,24 @@ public class ConfigReaderTests
1011
[Test]
1112
public void CanReadDocument()
1213
{
13-
var text = "assemblyVersioningScheme: MajorMinor";
14+
const string text = @"
15+
assemblyVersioningScheme: MajorMinor
16+
develop-branch-tag: alpha
17+
release-branch-tag: rc
18+
";
1419
var config = ConfigReader.Read(new StringReader(text));
15-
Assert.AreEqual(AssemblyVersioningScheme.MajorMinor, config.AssemblyVersioningScheme);
20+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
21+
config.DevelopBranchTag.ShouldBe("alpha");
22+
config.ReleaseBranchTag.ShouldBe("rc");
1623
}
1724

1825
[Test]
1926
public void CanReadDefaultDocument()
2027
{
21-
var text = "";
28+
const string text = "";
2229
var config = ConfigReader.Read(new StringReader(text));
23-
Assert.AreEqual(AssemblyVersioningScheme.MajorMinorPatch, config.AssemblyVersioningScheme);
30+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
31+
config.DevelopBranchTag.ShouldBe("unstable");
32+
config.ReleaseBranchTag.ShouldBe("beta");
2433
}
2534
}

GitVersionCore.Tests/EmptyRepositoryFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using GitVersion.Configuration;
23
using LibGit2Sharp;
34

45
public class EmptyRepositoryFixture : RepositoryFixtureBase
56
{
67
public EmptyRepositoryFixture() :
7-
base(CreateNewRepository)
8+
base(CreateNewRepository, new Config())
89
{
910
}
1011

GitVersionCore.Tests/RepositoryFixtureBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using System;
22
using GitVersion;
3+
using GitVersion.Configuration;
34
using LibGit2Sharp;
45
using Shouldly;
56

67
public abstract class RepositoryFixtureBase : IDisposable
78
{
89
public string RepositoryPath;
910
public IRepository Repository;
11+
Config configuration;
1012

11-
protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder)
13+
protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config configuration)
1214
{
15+
this.configuration = configuration;
1316
RepositoryPath = PathHelper.GetTempPath();
1417
Repository = repoBuilder(RepositoryPath);
1518
Repository.Config.Set("user.name", "Test");
@@ -19,7 +22,7 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder)
1922
public SemanticVersion ExecuteGitVersion()
2023
{
2124
var vf = new GitVersionFinder();
22-
return vf.FindVersion(new GitVersionContext(Repository));
25+
return vf.FindVersion(new GitVersionContext(Repository, configuration));
2326
}
2427

2528
public void AssertFullSemver(string fullSemver)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
namespace GitVersion.Configuration
22
{
3+
using YamlDotNet.Serialization;
4+
35
public class Config
46
{
7+
public Config()
8+
{
9+
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch;
10+
DevelopBranchTag = "unstable";
11+
ReleaseBranchTag = "beta";
12+
}
13+
514
public AssemblyVersioningScheme AssemblyVersioningScheme { get; set; }
15+
16+
[YamlAlias("develop-branch-tag")]
17+
public string DevelopBranchTag { get; set; }
18+
19+
[YamlAlias("release-branch-tag")]
20+
public string ReleaseBranchTag { get; set; }
621
}
722
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace GitVersion.Configuration
2+
{
3+
using System.IO;
4+
5+
public class ConfigurationProvider
6+
{
7+
public static Config Provide(string gitDirectory)
8+
{
9+
var configFilePath = Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml");
10+
if (File.Exists(configFilePath))
11+
{
12+
using (var reader = File.OpenText(configFilePath))
13+
{
14+
{
15+
return ConfigReader.Read(reader);
16+
}
17+
}
18+
}
19+
20+
return new Config();
21+
}
22+
}
23+
}

GitVersionCore/GitVersionContext.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22
{
33
using System.Collections.Generic;
44
using System.Linq;
5+
using GitVersion.Configuration;
56
using LibGit2Sharp;
67

78
/// <summary>
89
/// Contextual information about where GitVersion is being run
910
/// </summary>
1011
public class GitVersionContext
1112
{
12-
public GitVersionContext(IRepository repository)
13-
: this(repository, repository.Head)
13+
public GitVersionContext(IRepository repository, Config configuration)
14+
: this(repository, repository.Head, configuration)
1415
{
16+
Configuration = configuration;
1517
}
1618

17-
public GitVersionContext(IRepository repository, Branch currentBranch)
19+
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration)
1820
{
1921
Repository = repository;
22+
Configuration = configuration;
2023

2124
if (currentBranch == null)
2225
return;
@@ -33,6 +36,7 @@ public GitVersionContext(IRepository repository, Branch currentBranch)
3336
}
3437
}
3538

39+
public Config Configuration { get; private set; }
3640
public IRepository Repository { get; private set; }
3741
public Branch CurrentBranch { get; private set; }
3842
public Commit CurrentCommit { get; private set; }

GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="BuildServers\TeamCity.cs" />
7171
<Compile Include="Configuration\Config.cs" />
7272
<Compile Include="Configuration\ConfigReader.cs" />
73+
<Compile Include="Configuration\ConfigurationProvider.cs" />
7374
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
7475
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
7576
<Compile Include="LastMinorVersionFinder.cs" />

GitVersionCore/GitVersionFinder.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class GitVersionFinder
77
{
88
public SemanticVersion FindVersion(GitVersionContext context)
99
{
10+
Logger.WriteInfo("Running against branch: " + context.CurrentBranch.Name);
1011
EnsureMainTopologyConstraints(context);
1112

1213
if (ShouldGitHubFlowVersioningSchemeApply(context.Repository))
@@ -19,14 +20,6 @@ public SemanticVersion FindVersion(GitVersionContext context)
1920
return new GitFlowVersionFinder().FindVersion(context);
2021
}
2122

22-
public static SemanticVersion GetSemanticVersion(Repository repository)
23-
{
24-
var versionForRepositoryFinder = new GitVersionFinder();
25-
var gitVersionContext = new GitVersionContext(repository);
26-
Logger.WriteInfo("Running against branch: " + gitVersionContext.CurrentBranch.Name);
27-
return versionForRepositoryFinder.FindVersion(gitVersionContext);
28-
}
29-
3023
static bool ShouldGitHubFlowVersioningSchemeApply(IRepository repo)
3124
{
3225
return repo.FindBranch("develop") == null;

GitVersionExe/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace GitVersion
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9+
using GitVersion.Configuration;
910

1011
class Program
1112
{
@@ -78,9 +79,11 @@ static int Run()
7879
buildServer.PerformPreProcessingSteps(gitDirectory);
7980
}
8081
SemanticVersion semanticVersion;
82+
var versionFinder = new GitVersionFinder();
8183
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
8284
{
83-
semanticVersion = GitVersionFinder.GetSemanticVersion(repo);
85+
var gitVersionContext = new GitVersionContext(repo, ConfigurationProvider.Provide(gitDirectory));
86+
semanticVersion = versionFinder.FindVersion(gitVersionContext);
8487
}
8588

8689
if (arguments.Output == OutputType.BuildServer)

GitVersionTask.Tests/BranchFinders/DevelopTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FluentDate;
22
using FluentDateTimeOffset;
33
using GitVersion;
4+
using GitVersion.Configuration;
45
using NUnit.Framework;
56
using ObjectApproval;
67

@@ -35,7 +36,7 @@ public void Commit_on_develop_and_previous_commit_on_master_is_a_hotfix()
3536
mockBranch
3637
},
3738
};
38-
var version = finder.FindVersion(new GitVersionContext(repository, mockBranch));
39+
var version = finder.FindVersion(new GitVersionContext(repository, mockBranch, new Config()));
3940
Assert.AreEqual(2, version.Minor, "Minor should be master.Minor+1");
4041
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
4142
}
@@ -76,7 +77,7 @@ public void Commit_on_develop_and_previous_commit_on_master_has_a_tag()
7677
}
7778
}
7879
};
79-
var context = new GitVersionContext(repository, develop);
80+
var context = new GitVersionContext(repository, develop, new Config());
8081

8182
var version = finder.FindVersion(context);
8283
Assert.AreEqual(2, version.Minor, "Minor should be master.Minor+1");

GitVersionTask.Tests/BranchFinders/FeatureBranchTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GitVersion;
2+
using GitVersion.Configuration;
23
using LibGit2Sharp;
34
using NUnit.Framework;
45
using ObjectApproval;
@@ -20,7 +21,7 @@ public void Feature_branch_with_no_commit()
2021

2122
var finder = new FeatureVersionFinder();
2223

23-
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
24+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch, new Config()));
2425

2526
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
2627

@@ -45,7 +46,7 @@ public void Feature_branch_with_1_commit()
4546

4647
var finder = new FeatureVersionFinder();
4748

48-
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
49+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch, new Config()));
4950

5051
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
5152

@@ -73,7 +74,7 @@ public void Feature_branch_with_2_commits()
7374

7475
var finder = new FeatureVersionFinder();
7576

76-
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
77+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch, new Config()));
7778

7879
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
7980

@@ -100,7 +101,7 @@ public void Feature_branch_with_2_commits_but_building_an_commit()
100101

101102
var finder = new FeatureVersionFinder();
102103

103-
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch));
104+
var version = finder.FindVersion(new GitVersionContext(repo, featureBranch, new Config()));
104105

105106
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
106107

GitVersionTask.Tests/BranchFinders/PullBranchTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GitVersion;
2+
using GitVersion.Configuration;
23
using LibGit2Sharp;
34
using NUnit.Framework;
45
using ObjectApproval;
@@ -35,7 +36,7 @@ void AssertInvalidPullBranchName(string invalidFakePullBranchName)
3536

3637
var finder = new PullVersionFinder();
3738

38-
Assert.Throws<WarningException>(() => finder.FindVersion(new GitVersionContext(repo, pullBranch)));
39+
Assert.Throws<WarningException>(() => finder.FindVersion(new GitVersionContext(repo, pullBranch, new Config())));
3940
}
4041
}
4142

@@ -54,7 +55,7 @@ public void Pull_branch_with_1_commit()
5455

5556
var finder = new PullVersionFinder();
5657

57-
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch));
58+
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch, new Config()));
5859

5960
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
6061

@@ -80,7 +81,7 @@ public void Pull_branch_with_2_commits()
8081

8182
var finder = new PullVersionFinder();
8283

83-
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch));
84+
var version = finder.FindVersion(new GitVersionContext(repo, pullBranch, new Config()));
8485

8586
var masterVersion = FindersHelper.RetrieveMasterVersion(repo);
8687

GitVersionTask.Tests/BranchFinders/ReleaseTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GitVersion;
3+
using GitVersion.Configuration;
34
using LibGit2Sharp;
45
using NUnit.Framework;
56
using ObjectApproval;
@@ -20,7 +21,7 @@ public void EnsureAReleaseBranchNameDoesNotExposeAPatchSegment()
2021

2122
var finder = new ReleaseVersionFinder();
2223

23-
Assert.Throws<WarningException>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
24+
Assert.Throws<WarningException>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch, new Config())));
2425
}
2526
}
2627

@@ -37,7 +38,7 @@ public void EnsureAReleaseBranchNameDoesNotExposeAStability()
3738

3839
var finder = new ReleaseVersionFinder();
3940

40-
Assert.Throws<Exception>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
41+
Assert.Throws<Exception>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch, new Config())));
4142
}
4243
}
4344

@@ -55,7 +56,7 @@ public void Tag_on_commit_should_be_exact_tag()
5556

5657
var finder = new ReleaseVersionFinder();
5758

58-
var version = finder.FindVersion(new GitVersionContext(repo, repo.Head));
59+
var version = finder.FindVersion(new GitVersionContext(repo, repo.Head, new Config()));
5960
ObjectApprover.VerifyWithJson(version, Scrubbers.GuidAndDateScrubber);
6061
}
6162
}

0 commit comments

Comments
 (0)