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

green: RepoWithHistory, EmptyRepo #4

Merged
merged 1 commit into from
Oct 20, 2018
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs/
bin/
obj/
40 changes: 40 additions & 0 deletions MinVer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2003
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVerTests", "MinVerTests\MinVerTests.csproj", "{57307B80-9750-4D4F-91DC-EE28EC89EDDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MinVer", "MinVer\MinVer.csproj", "{EE44EA11-CEBE-49C7-9F23-EE8AE6FDFCFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "targets", "targets\Targets.csproj", "{0F266E8B-E818-41A8-B93C-9E80E9446AC6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{57307B80-9750-4D4F-91DC-EE28EC89EDDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57307B80-9750-4D4F-91DC-EE28EC89EDDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57307B80-9750-4D4F-91DC-EE28EC89EDDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57307B80-9750-4D4F-91DC-EE28EC89EDDD}.Release|Any CPU.Build.0 = Release|Any CPU
{EE44EA11-CEBE-49C7-9F23-EE8AE6FDFCFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE44EA11-CEBE-49C7-9F23-EE8AE6FDFCFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE44EA11-CEBE-49C7-9F23-EE8AE6FDFCFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE44EA11-CEBE-49C7-9F23-EE8AE6FDFCFB}.Release|Any CPU.Build.0 = Release|Any CPU
{0F266E8B-E818-41A8-B93C-9E80E9446AC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F266E8B-E818-41A8-B93C-9E80E9446AC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F266E8B-E818-41A8-B93C-9E80E9446AC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F266E8B-E818-41A8-B93C-9E80E9446AC6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A50D1D0-1F93-4845-829D-C33C590C82F4}
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions MinVer/MinVer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>latest</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.25.3" />
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="2.6.2" PrivateAssets="All" />
</ItemGroup>

</Project>
121 changes: 121 additions & 0 deletions MinVer/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
namespace MinVer
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using static System.Math;

#pragma warning disable CA1036 // Override methods on comparable types
public class Version : IComparable<Version>
#pragma warning restore CA1036 // Override methods on comparable types
{
private readonly int major;
private readonly int minor;
private readonly int patch;
private readonly List<string> preReleaseIdentifiers;

public Version() => this.preReleaseIdentifiers = new List<string> { "alpha", "0" };

public Version(int major, int minor, int patch, IEnumerable<string> preReleaseIdentifiers)
{
this.major = major;
this.minor = minor;
this.patch = patch;
this.preReleaseIdentifiers = preReleaseIdentifiers?.ToList() ?? new List<string>();
}

public override string ToString() =>
$"{this.major}.{this.minor}.{this.patch}{(this.preReleaseIdentifiers.Count == 0 ? "" : $"-{string.Join(".", this.preReleaseIdentifiers)}")}";

public int CompareTo(Version other)
{
var major = this.major.CompareTo(other.major);
if (major != 0)
{
return major;
}

var minor = this.minor.CompareTo(other.minor);
if (minor != 0)
{
return minor;
}

var patch = this.patch.CompareTo(other.patch);
if (patch != 0)
{
return patch;
}

if (this.preReleaseIdentifiers.Count > 0 && other.preReleaseIdentifiers.Count == 0)
{
return -1;
}

if (this.preReleaseIdentifiers.Count == 0 && other.preReleaseIdentifiers.Count > 0)
{
return 1;
}

var maxCount = Max(this.preReleaseIdentifiers.Count, other.preReleaseIdentifiers.Count);
for (var index = 0; index < maxCount; ++index)
{
if (this.preReleaseIdentifiers.Count == index && other.preReleaseIdentifiers.Count > index)
{
return -1;
}

if (this.preReleaseIdentifiers.Count > index && other.preReleaseIdentifiers.Count == index)
{
return 1;
}

if (int.TryParse(this.preReleaseIdentifiers[index], out var thisNumber) && int.TryParse(other.preReleaseIdentifiers[index], out var otherNumber))
{
var number = thisNumber.CompareTo(otherNumber);
if (number != 0)
{
return number;
}
}
else
{
var text = string.CompareOrdinal(this.preReleaseIdentifiers[index], other.preReleaseIdentifiers[index]);
if (text != 0)
{
return text;
}
}
}

return 0;
}

public Version AddHeight(int height) =>
height == 0
? new Version(this.major, this.minor, this.patch, this.preReleaseIdentifiers)
: this.preReleaseIdentifiers.Count == 0
? new Version(this.major, this.minor + 1, 0, new[] { "alpha", "0", height.ToString(CultureInfo.InvariantCulture) })
: new Version(this.major, this.minor, this.patch, this.preReleaseIdentifiers.Concat(new[] { height.ToString(CultureInfo.InvariantCulture) }));

public static Version ParseOrDefault(string text)
{
if (text == default)
{
return null;
}

var numbersAndPreRelease = text.Split(new[] { '-' }, 2);
var numbers = numbersAndPreRelease[0].Split('.');

return
numbers.Length == 3 &&
int.TryParse(numbers[0], out var major) &&
int.TryParse(numbers[1], out var minor) &&
int.TryParse(numbers[2], out var patch)
? new Version(major, minor, patch, numbersAndPreRelease.Length == 2 ? numbersAndPreRelease[1].Split('.') : null)
: null;
}
}
}
45 changes: 45 additions & 0 deletions MinVer/Versioner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace MinVer
{
using System.Linq;
using LibGit2Sharp;

public static class Versioner
{
public static Version GetVersion(string path)
{
using (var repo = new Repository(path))
{
return GetVersion(repo.Commits.FirstOrDefault(), 0, repo.Tags);
}
}

private static Version GetVersion(Commit commit, int height, TagCollection tags)
{
if (commit == default)
{
return new Version();
}

var version = GetVersionOrDefault(tags, commit);

if (version != default)
{
return version.AddHeight(height);
}

return commit.Parents
.Select(parent => GetVersion(parent, height + 1, tags))
.Where(_ => _ != default)
.OrderByDescending(_ => _)
.FirstOrDefault() ??
new Version().AddHeight(height);
}

private static Version GetVersionOrDefault(TagCollection tags, Commit commit) => tags
.Where(tag => tag.Target.Sha == commit.Sha)
.Select(tag => Version.ParseOrDefault(tag.FriendlyName))
.Where(_ => _ != default)
.OrderByDescending(_ => _)
.FirstOrDefault();
}
}
23 changes: 23 additions & 0 deletions MinVerTests/Infra/AssertFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MinVerTests.Infra
{
using System;
using System.IO;
using System.Threading.Tasks;

public static class AssertFile
{
public static async Task Contains(string expectedPath, string actual)
{
if (actual != await File.ReadAllTextAsync(expectedPath))
{
var actualPath = Path.Combine(
Path.GetDirectoryName(expectedPath),
Path.GetFileNameWithoutExtension(expectedPath) + "-actual" + Path.GetExtension(expectedPath));

await File.WriteAllTextAsync(actualPath, actual);

throw new Exception($"{actualPath} does not contain the contents of {expectedPath}.");
}
}
}
}
20 changes: 20 additions & 0 deletions MinVerTests/MinVerTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>latest</LangVersion>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp" Version="0.25.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="SimpleExec" Version="3.0.0" />
<PackageReference Include="Xbehave" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MinVer\MinVer.csproj" />
</ItemGroup>

</Project>
Loading