-
-
Notifications
You must be signed in to change notification settings - Fork 174
Add a managed Git implementation #521
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
Changes from 40 commits
1bb16df
9e537e9
9de6fb4
fe0ba90
69f2673
cfb093a
7bf2f56
aa8af44
b6f0d39
64f6c45
168ce42
eff6450
b202975
63b3b0f
0ef14c2
8ee8260
82f4317
fc71605
eae9d27
3b461e2
083a994
a9c7f0b
4e96bb0
35e67a4
9144c2e
8cd0bae
b3ede9a
310903e
e84725a
ed246fb
f69109c
b145612
7b41b48
1eb35ec
7c0d366
ff165f6
52446d5
6ee7080
89984ff
8e39558
0c34296
6948e89
236dd40
745d0d5
103bce7
8328e84
552155d
0608e45
29bc0f4
dd7e2ed
96a0a34
aae92be
f8257be
f83df9e
d3e27c1
006c997
8ed7f57
ad64eb8
2e0be45
9da12fe
297206e
f12d5ae
646e5fd
e3bd782
21522ea
4f31f47
c470b2c
02a70f1
23a3f83
2b3baa3
040aa4e
35edb99
ae28491
0c5a573
313b899
ec6482f
9e9d165
31fb870
469eb1d
300c2de
61e877a
20f4055
36f2496
180489b
f53841a
8e400c1
3b403df
5a9e10a
150611d
486103e
b32589b
c4126df
2192ff9
3608036
78f27aa
2ae0b8f
96ca53a
a462fb8
39d677f
051ba0f
8eaf3bb
aca448c
168e9d0
4c4a311
fb1a76b
1f1ca7e
052735a
18af50e
d70601e
9e488a5
d7ec20b
df90653
209e53e
366d2c2
38d5df3
7685c32
7c08676
74e617b
14822fd
912e129
1074e57
b5f1fc5
77fe64b
de13412
670cbe6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.IO; | ||
using BenchmarkDotNet.Attributes; | ||
using Nerdbank.GitVersioning; | ||
using NerdBank.GitVersioning.Managed; | ||
|
||
namespace NerdBank.GitVersioning.Benchmarks | ||
{ | ||
public class GetVersionBenchmarks | ||
{ | ||
[Params( | ||
"xunit;version.json", | ||
"Cuemon;version.json", | ||
"SuperSocket;version.json", | ||
"NerdBank.GitVersioning;version.json")] | ||
public string TestData; | ||
|
||
public string RepositoryName => this.TestData.Split(';')[0]; | ||
|
||
public string VersionPath => this.TestData.Split(';')[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This property doesn't appear to be used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, I've removed it. |
||
|
||
public string RepositoryPath => Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), | ||
@"Source\Repos", | ||
this.RepositoryName); | ||
|
||
public Version Version | ||
{ get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can we move these accessors so they share the line above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, done. |
||
|
||
[Benchmark(Baseline = true)] | ||
public void GetVersionLibGit2() | ||
{ | ||
var oracle = LibGit2VersionOracle.CreateLibGit2(this.RepositoryPath); | ||
this.Version = oracle.Version; | ||
} | ||
|
||
[Benchmark] | ||
public void GetVersionManaged() | ||
{ | ||
var oracle = ManagedVersionOracle.CreateManaged(this.RepositoryPath); | ||
this.Version = oracle.Version; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
AArnott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>portable</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<Optimize>true</Optimize> | ||
AArnott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Configuration>Release</Configuration> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" /> | ||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.0" /> | ||
<PackageReference Include="Nerdbank.GitVersioning.LKG" Version="3.1.93" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../NerdBank.GitVersioning/NerdBank.GitVersioning.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace NerdBank.GitVersioning.Benchmarks | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var summary = BenchmarkRunner.Run<GetVersionBenchmarks>(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
commit-* text eol=lf | ||
tree-* text eol=lf |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Collections.ObjectModel; | ||
using System.IO; | ||
using NerdBank.GitVersioning.Managed; | ||
using Xunit; | ||
|
||
namespace NerdBank.GitVersioning.Tests.Managed | ||
{ | ||
// Test case borrowed from https://stefan.saasen.me/articles/git-clone-in-haskell-from-the-bottom-up/#format-of-the-delta-representation | ||
public class DeltaStreamReaderTests | ||
{ | ||
[Fact] | ||
public void ReadCopyInstruction() | ||
{ | ||
using (Stream stream = new MemoryStream( | ||
new byte[] | ||
{ | ||
0b_10110000, | ||
0b_11010001, | ||
0b_00000001 | ||
})) | ||
{ | ||
var instruction = DeltaStreamReader.Read(stream).Value; | ||
|
||
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType); | ||
Assert.Equal(0, instruction.Offset); | ||
Assert.Equal(465, instruction.Size); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadInsertInstruction() | ||
{ | ||
using (Stream stream = new MemoryStream(new byte[] { 0b_00010111 })) | ||
{ | ||
var instruction = DeltaStreamReader.Read(stream).Value; | ||
|
||
Assert.Equal(DeltaInstructionType.Insert, instruction.InstructionType); | ||
Assert.Equal(0, instruction.Offset); | ||
Assert.Equal(23, instruction.Size); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ReadStreamTest() | ||
{ | ||
using (Stream stream = new MemoryStream( | ||
new byte[] | ||
{ | ||
0b_10110011, 0b_11001110, 0b_00000001, 0b_00100111, 0b_00000001, | ||
0b_10110011, 0b_01011111, 0b_00000011, 0b_01101100, 0b_00010000, 0b_10010011, | ||
0b_11110101, 0b_00000010, 0b_01101011, 0b_10110011, 0b_11001011, 0b_00010011, | ||
0b_01000110, 0b_00000011})) | ||
{ | ||
Collection<DeltaInstruction> instructions = new Collection<DeltaInstruction>(); | ||
|
||
DeltaInstruction? current; | ||
|
||
while ((current = DeltaStreamReader.Read(stream)) != null) | ||
{ | ||
instructions.Add(current.Value); | ||
} | ||
|
||
Assert.Collection( | ||
instructions, | ||
instruction => | ||
{ | ||
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType); | ||
Assert.Equal(462, instruction.Offset); | ||
Assert.Equal(295, instruction.Size); | ||
}, | ||
instruction => | ||
{ | ||
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType); | ||
Assert.Equal(863, instruction.Offset); | ||
Assert.Equal(4204, instruction.Size); | ||
}, | ||
instruction => | ||
{ | ||
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType); | ||
Assert.Equal(757, instruction.Offset); | ||
Assert.Equal(107, instruction.Size); | ||
}, | ||
instruction => | ||
{ | ||
Assert.Equal(DeltaInstructionType.Copy, instruction.InstructionType); | ||
Assert.Equal(5067, instruction.Offset); | ||
Assert.Equal(838, instruction.Size); | ||
}); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.IO; | ||
using NerdBank.GitVersioning.Managed; | ||
using Xunit; | ||
|
||
namespace NerdBank.GitVersioning.Tests.Managed | ||
{ | ||
public class GitCommitReaderTests | ||
{ | ||
[Fact] | ||
public void ReadTest() | ||
{ | ||
using (Stream stream = TestUtilities.GetEmbeddedResource(@"Managed\commit-d56dc3ed179053abef2097d1120b4507769bcf1a")) | ||
{ | ||
var commit = GitCommitReader.Read(stream, GitObjectId.Parse("d56dc3ed179053abef2097d1120b4507769bcf1a")); | ||
|
||
Assert.Equal("d56dc3ed179053abef2097d1120b4507769bcf1a", commit.Sha.ToString()); | ||
Assert.Equal("f914b48023c7c804a4f3be780d451f31aef74ac1", commit.Tree.ToString()); | ||
|
||
Assert.Collection( | ||
commit.Parents, | ||
c => Assert.Equal("4497b0eaaa89abf0e6d70961ad5f04fd3a49cbc6", c.ToString()), | ||
c => Assert.Equal("0989e8fe0cd0e0900173b26decdfb24bc0cc8232", c.ToString())); | ||
|
||
Assert.Equal("Andrew Arnott", commit.Author.Name); | ||
Assert.Equal(new DateTimeOffset(2020, 10, 6, 13, 40, 09, TimeSpan.FromHours(-6)), commit.Author.Date); | ||
Assert.Equal("[email protected]", commit.Author.Email); | ||
|
||
// Committer and commit message are not read | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using NerdBank.GitVersioning.Managed; | ||
using Xunit; | ||
|
||
namespace NerdBank.GitVersioning.Tests.Managed | ||
{ | ||
public class GitCommitTests | ||
{ | ||
private readonly byte[] shaAsByteArray = new byte[] { 0x4e, 0x91, 0x27, 0x36, 0xc2, 0x7e, 0x40, 0xb3, 0x89, 0x90, 0x4d, 0x04, 0x6d, 0xc6, 0x3d, 0xc9, 0xf5, 0x78, 0x11, 0x7f }; | ||
|
||
[Fact] | ||
public void EqualsObjectTest() | ||
{ | ||
var commit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
var commit2 = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
var emptyCommit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Empty, | ||
}; | ||
|
||
// Must be equal to itself | ||
Assert.True(commit.Equals((object)commit)); | ||
Assert.True(commit.Equals((object)commit2)); | ||
|
||
// Not equal to null | ||
Assert.False(commit.Equals(null)); | ||
|
||
// Not equal to other representations of the commit | ||
Assert.False(commit.Equals(this.shaAsByteArray)); | ||
Assert.False(commit.Equals(commit.Sha)); | ||
|
||
// Not equal to other object ids | ||
Assert.False(commit.Equals((object)emptyCommit)); | ||
} | ||
|
||
[Fact] | ||
public void EqualsCommitTest() | ||
{ | ||
var commit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
var commit2 = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
var emptyCommit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Empty, | ||
}; | ||
|
||
// Must be equal to itself | ||
Assert.True(commit.Equals(commit2)); | ||
Assert.True(commit.Equals(commit2)); | ||
|
||
// Not equal to other object ids | ||
Assert.False(commit.Equals(emptyCommit)); | ||
} | ||
|
||
[Fact] | ||
public void GetHashCodeTest() | ||
{ | ||
var commit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
var emptyCommit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Empty, | ||
}; | ||
|
||
// The hash code is the int32 representation of the first 4 bytes of the SHA hash | ||
Assert.Equal(0x3627914e, commit.GetHashCode()); | ||
Assert.Equal(0, emptyCommit.GetHashCode()); | ||
} | ||
|
||
[Fact] | ||
public void ToStringTest() | ||
{ | ||
var commit = new GitCommit() | ||
{ | ||
Sha = GitObjectId.Parse(this.shaAsByteArray), | ||
}; | ||
|
||
Assert.Equal("Git Commit: 4e912736c27e40b389904d046dc63dc9f578117f", commit.ToString()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do these repos actually come from? You're not checking them in, it seems. Are you thinking git submodules might be useful here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I searched GitHub for repos which use NerdBank.GitVersioning and have non-trivial version history. These came up. Submodules would probably work, I'll add a build leg for the perf test soon & let's see what works best.