Skip to content

Commit 4d679ff

Browse files
committed
Merge pull request #388 from GeertvanHorrik/pr/specificcommit
Fixes #387
2 parents d282d79 + 9123449 commit 4d679ff

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

GitVersionCore.Tests/Fixtures/RepositoryFixtureBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config co
2121

2222
public bool IsForTrackedBranchOnly { private get; set; }
2323

24-
public void AssertFullSemver(string fullSemver, IRepository repository = null)
24+
public void AssertFullSemver(string fullSemver, IRepository repository = null, string commitId = null)
2525
{
26-
var gitVersionContext = new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly);
26+
var gitVersionContext = new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly, commitId);
2727
var executeGitVersion = ExecuteGitVersion(gitVersionContext);
2828
var variables = VariableProvider.GetVariablesFor(executeGitVersion,
2929
gitVersionContext.Configuration.AssemblyVersioningScheme,

GitVersionCore.Tests/IntegrationTests/DevelopScenarios.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@
55
[TestFixture]
66
public class DevelopScenarios
77
{
8+
[Test]
9+
public void WhenDevelopHasMultipleCommits_SpecifyExistingCommitId()
10+
{
11+
using (var fixture = new EmptyRepositoryFixture(new Config()))
12+
{
13+
fixture.Repository.MakeATaggedCommit("1.0.0");
14+
fixture.Repository.CreateBranch("develop").Checkout();
15+
16+
fixture.Repository.MakeACommit();
17+
fixture.Repository.MakeACommit();
18+
var thirdCommit = fixture.Repository.MakeACommit();
19+
fixture.Repository.MakeACommit();
20+
fixture.Repository.MakeACommit();
21+
22+
fixture.AssertFullSemver("1.1.0-unstable.3", commitId: thirdCommit.Sha);
23+
}
24+
}
25+
26+
[Test]
27+
public void WhenDevelopHasMultipleCommits_SpecifyNonExistingCommitId()
28+
{
29+
using (var fixture = new EmptyRepositoryFixture(new Config()))
30+
{
31+
fixture.Repository.MakeATaggedCommit("1.0.0");
32+
fixture.Repository.CreateBranch("develop").Checkout();
33+
34+
fixture.Repository.MakeACommit();
35+
fixture.Repository.MakeACommit();
36+
fixture.Repository.MakeACommit();
37+
fixture.Repository.MakeACommit();
38+
fixture.Repository.MakeACommit();
39+
40+
fixture.AssertFullSemver("1.1.0-unstable.5", commitId: "nonexistingcommitid");
41+
}
42+
}
43+
844
[Test]
945
public void WhenDevelopBranchedFromTaggedCommitOnMasterVersionDoesNotChange()
1046
{

GitVersionCore/GitVersionContext.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class GitVersionContext
1111
{
1212
readonly Config configuration;
1313

14-
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
15-
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
14+
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true, string commitId = null)
15+
: this(repository, repository.Head, configuration, isForTrackingBranchOnly, commitId)
1616
{
1717
}
1818

19-
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool onlyEvaluateTrackedBranches = true)
19+
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
2020
{
2121
Repository = repository;
2222
this.configuration = configuration;
@@ -25,7 +25,24 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
2525
if (currentBranch == null)
2626
throw new InvalidOperationException("Need a branch to operate on");
2727

28-
CurrentCommit = currentBranch.Tip;
28+
if (!string.IsNullOrWhiteSpace(commitId))
29+
{
30+
Logger.WriteInfo(string.Format("Searching for specific commit '{0}'", commitId));
31+
32+
var commit = repository.Commits.FirstOrDefault(c => string.Equals(c.Sha, commitId, StringComparison.OrdinalIgnoreCase));
33+
if (commit != null)
34+
{
35+
CurrentCommit = commit;
36+
}
37+
}
38+
39+
if (CurrentCommit == null)
40+
{
41+
Logger.WriteWarning("No specific commit specified or found, falling back to latest commit on specified branch");
42+
43+
CurrentCommit = currentBranch.Tip;
44+
}
45+
2946
if (currentBranch.IsDetachedHead())
3047
{
3148
CurrentBranch = CurrentCommit.GetBranchesContainingCommit(repository, OnlyEvaluateTrackedBranches).OnlyOrDefault() ?? currentBranch;

GitVersionExe.Tests/HelpWriterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void AllArgsAreInHelp()
2424
typeof(Arguments).GetFields()
2525
.Select(p => p.Name)
2626
.Where(p => IsNotInHelp(lookup, p, helpText))
27-
.Except(new[] { "Authentication" })
27+
.Except(new[] { "Authentication", "CommitId" })
2828
.ShouldBeEmpty();
2929
}
3030

GitVersionExe/ArgumentParser.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
108108
continue;
109109
}
110110

111+
if (IsSwitch("c", name))
112+
{
113+
arguments.CommitId = value;
114+
continue;
115+
}
116+
111117
if (IsSwitch("exec", name))
112118
{
113119
arguments.Exec = value;
@@ -198,6 +204,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
198204

199205
throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
200206
}
207+
201208
return arguments;
202209
}
203210

GitVersionExe/Arguments.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public Arguments()
1414

1515
public string TargetUrl;
1616
public string TargetBranch;
17+
public string CommitId;
1718

1819
public bool Init;
1920

GitVersionExe/HelpWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Specify name of AssemblyInfo file. Can also /updateAssemblyInfo GlobalAssemblyIn
3737
/b Name of the branch to use on the remote repository, must be used in combination with /url.
3838
/u Username in case authentication is required.
3939
/p Password in case authentication is required.
40+
/c The commit id to check. If not specified, the latest available commit on the specified branch will be used.
4041
4142
# Execute build args
4243
/exec Executes target executable making GitVersion variables available as environmental variables

GitVersionExe/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int Run()
9696

9797
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
9898
{
99-
var gitVersionContext = new GitVersionContext(repo, configuration);
99+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId);
100100
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
101101
var config = gitVersionContext.Configuration;
102102
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);

0 commit comments

Comments
 (0)