Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the Dirty flag #2423

Merged
merged 9 commits into from
Oct 23, 2020
Merged
3 changes: 2 additions & 1 deletion docs/input/docs/more-info/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Running `GitVersion.exe` in your repo will show you what is available. For the
"VersionSourceSha":"950d2f830f5a2af12a6779a48d20dcbb02351f25",
"CommitsSinceVersionSource":1,
"CommitsSinceVersionSourcePadded":"0001",
"CommitDate":"2014-03-06"
"CommitDate":"2014-03-06",
"UncommittedChanges": 0
}
```

Expand Down
4 changes: 4 additions & 0 deletions src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public void CacheFileExistsOnDisk()
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
UncommittedChanges: 0
";

var stringBuilder = new StringBuilder();
Expand Down Expand Up @@ -208,6 +209,7 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
UncommittedChanges: 0
";

using var fixture = new EmptyRepositoryFixture();
Expand Down Expand Up @@ -294,6 +296,7 @@ public void ConfigChangeInvalidatesCache()
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
UncommittedChanges: 0
";

using var fixture = new EmptyRepositoryFixture();
Expand Down Expand Up @@ -357,6 +360,7 @@ public void NoCacheBypassesCache()
CommitsSinceVersionSource: 19
CommitsSinceVersionSourcePadded: 0019
CommitDate: 2015-11-10
UncommittedChanges: 0
";

using var fixture = new EmptyRepositoryFixture();
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/Helpers/TestableVersionVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public TestableVersionVariables(
string weightedPreReleaseNumber = "", string informationalVersion = "", string commitDate = "",
string nugetVersion = "", string nugetVersionV2 = "", string nugetPreReleaseTag = "",
string nugetPreReleaseTagV2 = "", string versionSourceSha = "", string commitsSinceVersionSource = "",
string commitsSinceVersionSourcePadded = "") : base(
string commitsSinceVersionSourcePadded = "", string uncommittedChanges = "") : base(
major, minor, patch, buildMetaData, buildMetaDataPadded, fullBuildMetaData, branchName, escapedBranchName,
sha, shortSha, majorMinorPatch, semVer, legacySemVer, legacySemVerPadded, fullSemVer,
assemblySemVer, assemblySemFileVer, preReleaseTag, weightedPreReleaseNumber, preReleaseTagWithDash,
preReleaseLabel, preReleaseNumber, informationalVersion, commitDate, nugetVersion, nugetVersionV2,
nugetPreReleaseTag, nugetPreReleaseTagV2, versionSourceSha, commitsSinceVersionSource, commitsSinceVersionSourcePadded)
nugetPreReleaseTag, nugetPreReleaseTagV2, versionSourceSha, commitsSinceVersionSource, commitsSinceVersionSourcePadded, uncommittedChanges)
{
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Globalization;
using System.IO;
using System.Linq;
using GitTools.Testing;
using GitVersionCore.Tests.Helpers;
using LibGit2Sharp;
using NUnit.Framework;
using Shouldly;

namespace GitVersionCore.Tests.IntegrationTests
{
Expand Down Expand Up @@ -94,5 +97,40 @@ public void DoNotBlowUpWhenDevelopAndFeatureBranchPointAtSameCommit()
fixture.InitializeRepo();
fixture.AssertFullSemver("1.1.0-alpha.1");
}

[TestCase(true, 1)]
[TestCase(false, 1)]
[TestCase(true, 5)]
[TestCase(false, 5)]
public void HasDirtyFlagWhenUncommittedChangesAreInRepo(bool stageFile, int numberOfFiles)
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();

for (int i = 0; i < numberOfFiles; i++)
{
var tempFile = Path.GetTempFileName();
var repoFile = Path.Combine(fixture.RepositoryPath, Path.GetFileNameWithoutExtension(tempFile) + ".txt");
File.Move(tempFile, repoFile);
File.WriteAllText(repoFile, $"Hello world / testfile {i}");

if (stageFile)
Commands.Stage(fixture.Repository, repoFile);
}

var version = fixture.GetVersion();
version.UncommittedChanges.ShouldBe(numberOfFiles.ToString(CultureInfo.InvariantCulture));
}

[Test]
public void NoDirtyFlagInCleanRepository()
{
using var fixture = new EmptyRepositoryFixture();
fixture.Repository.MakeACommit();

var version = fixture.GetVersion();
var zero = 0;
version.UncommittedChanges.ShouldBe(zero.ToString(CultureInfo.InvariantCulture));
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/Model/CommitDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void CommitDateFormatTest(string format, string expectedOutcome)
var formatValues = new SemanticVersionFormatValues(
new SemanticVersion
{
BuildMetaData = new SemanticVersionBuildMetaData("950d2f830f5a2af12a6779a48d20dcbb02351f25", 0, "master", "3139d4eeb044f46057693473eacc2655b3b27e7d", "3139d4eeb", new DateTimeOffset(date, TimeSpan.Zero)), // assume time zone is UTC
BuildMetaData = new SemanticVersionBuildMetaData("950d2f830f5a2af12a6779a48d20dcbb02351f25", 0, "master", "3139d4eeb044f46057693473eacc2655b3b27e7d", "3139d4eeb", new DateTimeOffset(date, TimeSpan.Zero), 0), // assume time zone is UTC

},
new EffectiveConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"VersionSourceSha":"versionSourceSha",
"CommitsSinceVersionSource":5,
"CommitsSinceVersionSourcePadded":"0005",
"UncommittedChanges":0,
"CommitDate":"2014-03-06"
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Json()
Minor = 2,
Patch = 0,
PreReleaseTag = "unstable4",
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceSha", 5, "feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z"))
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceSha", 5, "feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z"), 0)
};

var config = new TestEffectiveConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class NextVersionCalculatorTests : TestBase
[Test]
public void ShouldIncrementVersionBasedOnConfig()
{
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 1, "master", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now);
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 1, "master", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now, 0);

var contextBuilder = new GitVersionContextBuilder();

Expand All @@ -43,7 +43,7 @@ public void ShouldIncrementVersionBasedOnConfig()
[Test]
public void DoesNotIncrementWhenBaseVersionSaysNotTo()
{
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 1, "master", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now);
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 1, "master", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now, 0);

var contextBuilder = new GitVersionContextBuilder();

Expand All @@ -68,7 +68,7 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo()
[Test]
public void AppliesBranchPreReleaseTag()
{
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 2, "develop", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now);
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData("ef7d0d7e1e700f1c7c9fa01ea6791bb778a5c37c", 2, "develop", "b1a34edbd80e141f7cc046c074f109be7d022074", "b1a34e", DateTimeOffset.Now, 0);
var contextBuilder = new GitVersionContextBuilder();

contextBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<?define SemVer="1.2.3"?>
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
<?define UncommittedChanges="0"?>
<?define VersionSourceSha="versionSourceSha"?>
<?define WeightedPreReleaseNumber="0"?>
</Include>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<?define SemVer="1.2.3"?>
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
<?define UncommittedChanges="0"?>
<?define VersionSourceSha="versionSourceSha"?>
<?define WeightedPreReleaseNumber="0"?>
</Include>
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ static class GitVersionInformation
public static string VersionSourceSha = "versionSourceSha";
public static string CommitsSinceVersionSource = "5";
public static string CommitsSinceVersionSourcePadded = "0005";
public static string UncommittedChanges = "0";
public static string CommitDate = "2014-03-06";
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ type GitVersionInformation =
static member VersionSourceSha = "versionSourceSha"
static member CommitsSinceVersionSource = "5"
static member CommitsSinceVersionSourcePadded = "0005"
static member UncommittedChanges = "0"
static member CommitDate = "2014-03-06"
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Namespace Global
Public Shared VersionSourceSha As String = "versionSourceSha"
Public Shared CommitsSinceVersionSource As String = "5"
Public Shared CommitsSinceVersionSourcePadded As String = "0005"
Public Shared UncommittedChanges As String = "0"
Public Shared CommitDate As String = "2014-03-06"
End Class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private void VerifyAssemblyInfoFile(
fileSystem = Substitute.For<IFileSystem>();
var version = new SemanticVersion
{
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now),
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now, 0),
Major = 2,
Minor = 3,
Patch = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void ShouldCreateFile(string fileExtension)
Patch = 3,
PreReleaseTag = "unstable4",
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceSha", 5,
"feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z"))
"feature1", "commitSha", "commitShortSha", DateTimeOffset.Parse("2014-03-06 23:59:59Z"), 0)
};

var sp = ConfigureServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ValidateInformationalVersionBuilder(string branchName, string sha, s
Minor = minor,
Patch = patch,
PreReleaseTag = tag,
BuildMetaData = new SemanticVersionBuildMetaData(versionSourceSha, commitsSinceTag, branchName, sha, shortSha, DateTimeOffset.MinValue),
BuildMetaData = new SemanticVersionBuildMetaData(versionSourceSha, commitsSinceTag, branchName, sha, shortSha, DateTimeOffset.MinValue, 0),
};
var informationalVersion = semanticVersion.ToString("i");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private void VerifyAssemblyInfoFile(
fileSystem = Substitute.For<IFileSystem>();
var version = new SemanticVersion
{
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now),
BuildMetaData = new SemanticVersionBuildMetaData("versionSourceHash", 3, "foo", "hash", "shortHash", DateTimeOffset.Now, 0),
Major = 2,
Minor = 3,
Patch = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public interface IRepositoryMetadataProvider
bool GetMatchingCommitBranch(Commit baseVersionSource, Branch branch, Commit firstMatchingCommit);
string ShortenObjectId(GitObject commit);
VersionField? DetermineIncrementedField(BaseVersion baseVersion, GitVersionContext context);

int GetNumberOfUncommittedChanges();
}
}
3 changes: 2 additions & 1 deletion src/GitVersionCore/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ private GitVersionContext Init(Branch currentBranch, string commitId = null, boo
var currentBranchConfig = branchConfigurationCalculator.GetBranchConfiguration(currentBranch, currentCommit, configuration);
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
var currentCommitTaggedVersion = repositoryMetadataProvider.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
var numberOfUncommittedChanges = repositoryMetadataProvider.GetNumberOfUncommittedChanges();

return new GitVersionContext(currentBranch, currentCommit, configuration, effectiveConfiguration, currentCommitTaggedVersion);
return new GitVersionContext(currentBranch, currentCommit, configuration, effectiveConfiguration, currentCommitTaggedVersion, numberOfUncommittedChanges);
}
}
}
31 changes: 31 additions & 0 deletions src/GitVersionCore/Core/RepositoryMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,37 @@ private IEnumerable<BranchCommit> GetMergeCommitsForBranch(Branch branch, Config
return branchMergeBases;
}

public int GetNumberOfUncommittedChanges()
{
// check if we have a branch tip at all to behave properly with empty repos
// => return that we have actually uncomitted changes because we are apparently
// running GitVersion on something which lives inside this brand new repo _/\Ö/\_
if (repository.Head?.Tip == null || repository.Diff == null)
{
// this is a somewhat cumbersome way of figuring out the number of changes in the repo
// which is more expensive than to use the Diff as it gathers more info, but
// we can't use the other method when we are dealing with a new/empty repo
try
{
var status = repository.RetrieveStatus();
return status.Untracked.Count() + status.Staged.Count();

}
catch (Exception)
{
return Int32.MaxValue; // this should be somewhat puzzling to see,
// so we may have reached our goal to show that
// that repo is really "Dirty"...
}
}

// gets all changes of the last commit vs Staging area and WT
var changes = repository.Diff.Compare<TreeChanges>(repository.Head.Tip.Tree,
DiffTargets.Index | DiffTargets.WorkingDirectory);

return changes.Count;
}

private class MergeBaseData
{
public Commit MergeBase { get; }
Expand Down
6 changes: 5 additions & 1 deletion src/GitVersionCore/Model/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public class GitVersionContext
public Commit CurrentCommit { get; }
public bool IsCurrentCommitTagged => CurrentCommitTaggedVersion != null;

public int NumberOfUncommittedChanges { get; }

public GitVersionContext()
{
}

public GitVersionContext(Branch currentBranch, Commit currentCommit,
Config configuration, EffectiveConfiguration effectiveConfiguration, SemanticVersion currentCommitTaggedVersion)
Config configuration, EffectiveConfiguration effectiveConfiguration, SemanticVersion currentCommitTaggedVersion, int numberOfUncommittedChanges)
{
CurrentCommit = currentCommit;
CurrentBranch = currentBranch;
Expand All @@ -33,6 +35,8 @@ public GitVersionContext(Branch currentBranch, Commit currentCommit,
Configuration = effectiveConfiguration;

CurrentCommitTaggedVersion = currentCommitTaggedVersion;

NumberOfUncommittedChanges = numberOfUncommittedChanges;
}
}
}
Loading