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
4 changes: 2 additions & 2 deletions GitVersionCore.Tests/Fixtures/RepositoryFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder, Config co

public bool IsForTrackedBranchOnly { private get; set; }

public void AssertFullSemver(string fullSemver, IRepository repository = null)
public void AssertFullSemver(string fullSemver, IRepository repository = null, string commitId = null)
{
var gitVersionContext = new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly);
var gitVersionContext = new GitVersionContext(repository ?? Repository, configuration, IsForTrackedBranchOnly, commitId);
var executeGitVersion = ExecuteGitVersion(gitVersionContext);
var variables = VariableProvider.GetVariablesFor(executeGitVersion,
gitVersionContext.Configuration.AssemblyVersioningScheme,
Expand Down
36 changes: 36 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/DevelopScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@
[TestFixture]
public class DevelopScenarios
{
[Test]
public void WhenDevelopHasMultipleCommits_SpecifyExistingCommitId()
{
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeATaggedCommit("1.0.0");
fixture.Repository.CreateBranch("develop").Checkout();

fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();
var thirdCommit = fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();

fixture.AssertFullSemver("1.1.0-unstable.3", commitId: thirdCommit.Sha);
}
}

[Test]
public void WhenDevelopHasMultipleCommits_SpecifyNonExistingCommitId()
{
using (var fixture = new EmptyRepositoryFixture(new Config()))
{
fixture.Repository.MakeATaggedCommit("1.0.0");
fixture.Repository.CreateBranch("develop").Checkout();

fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();
fixture.Repository.MakeACommit();

fixture.AssertFullSemver("1.1.0-unstable.5", commitId: "nonexistingcommitid");
}
}

[Test]
public void WhenDevelopBranchedFromTaggedCommitOnMasterVersionDoesNotChange()
{
Expand Down
25 changes: 21 additions & 4 deletions GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class GitVersionContext
{
readonly Config configuration;

public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true)
: this(repository, repository.Head, configuration, isForTrackingBranchOnly)
public GitVersionContext(IRepository repository, Config configuration, bool isForTrackingBranchOnly = true, string commitId = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be on the config with the branch and such? Otherwise looks good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me, Config is something you read from the yaml file. This is not something you want to read from the yaml file (because it's too dynamic). I can easily move it to the config class though, just let me know what you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are right actually. Was thinking config included other arguments and such.

: this(repository, repository.Head, configuration, isForTrackingBranchOnly, commitId)
{
}

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

CurrentCommit = currentBranch.Tip;
if (!string.IsNullOrWhiteSpace(commitId))
{
Logger.WriteInfo(string.Format("Searching for specific commit '{0}'", commitId));

var commit = repository.Commits.FirstOrDefault(c => string.Equals(c.Sha, commitId, StringComparison.OrdinalIgnoreCase));
if (commit != null)
{
CurrentCommit = commit;
}
}

if (CurrentCommit == null)
{
Logger.WriteWarning("No specific commit specified or found, falling back to latest commit on specified branch");

CurrentCommit = currentBranch.Tip;
}

if (currentBranch.IsDetachedHead())
{
CurrentBranch = CurrentCommit.GetBranchesContainingCommit(repository, OnlyEvaluateTrackedBranches).OnlyOrDefault() ?? currentBranch;
Expand Down
2 changes: 1 addition & 1 deletion GitVersionExe.Tests/HelpWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void AllArgsAreInHelp()
typeof(Arguments).GetFields()
.Select(p => p.Name)
.Where(p => IsNotInHelp(lookup, p, helpText))
.Except(new[] { "Authentication" })
.Except(new[] { "Authentication", "CommitId" })
.ShouldBeEmpty();
}

Expand Down
7 changes: 7 additions & 0 deletions GitVersionExe/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
continue;
}

if (IsSwitch("c", name))
{
arguments.CommitId = value;
continue;
}

if (IsSwitch("exec", name))
{
arguments.Exec = value;
Expand Down Expand Up @@ -198,6 +204,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)

throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
}

return arguments;
}

Expand Down
1 change: 1 addition & 0 deletions GitVersionExe/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public Arguments()

public string TargetUrl;
public string TargetBranch;
public string CommitId;

public bool Init;

Expand Down
1 change: 1 addition & 0 deletions GitVersionExe/HelpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Specify name of AssemblyInfo file. Can also /updateAssemblyInfo GlobalAssemblyIn
/b Name of the branch to use on the remote repository, must be used in combination with /url.
/u Username in case authentication is required.
/p Password in case authentication is required.
/c The commit id to check. If not specified, the latest available commit on the specified branch will be used.

# Execute build args
/exec Executes target executable making GitVersion variables available as environmental variables
Expand Down
2 changes: 1 addition & 1 deletion GitVersionExe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int Run()

using (var repo = RepositoryLoader.GetRepo(gitDirectory))
{
var gitVersionContext = new GitVersionContext(repo, configuration);
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: arguments.CommitId);
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
var config = gitVersionContext.Configuration;
variables = VariableProvider.GetVariablesFor(semanticVersion, config.AssemblyVersioningScheme, config.VersioningMode, config.ContinuousDeploymentFallbackTag, gitVersionContext.IsCurrentCommitTagged);
Expand Down