8000 Deprecate repo.Version in favor of GlobalSettings.Version · coding2233/libgit2sharp4unity3d@91c04d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91c04d8

Browse files
JPaulDuncannulltoken
authored andcommitted
Deprecate repo.Version in favor of GlobalSettings.Version
Fix libgit2#726
1 parent b8225a7 commit 91c04d8

File tree

7 files changed

+167
-73
lines changed

7 files changed

+167
-73
lines changed
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using LibGit2Sharp;
1+
using System.Text.RegularExpressions;
32
using LibGit2Sharp.Tests.TestHelpers;
43
using Xunit;
54

@@ -10,10 +9,44 @@ public class GlobalSettingsFixture : BaseFixture
109
[Fact]
1110
public void CanGetMinimumCompiledInFeatures()
1211
{
13-
BuiltInFeatures features = GlobalSettings.Features();
12+
BuiltInFeatures features = GlobalSettings.Version.Features;
1413

1514
Assert.True(features.HasFlag(BuiltInFeatures.Threads));
1615
Assert.True(features.HasFlag(BuiltInFeatures.Https));
1716
}
17+
18+
[Fact]
19+
public void CanRetrieveValidVersionString()
20+
8000 {
21+
// Version string format is:
22+
// Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)
23+
// Example output:
24+
// "0.17.0-unknown-06d772d (x86 - Threads, Https)"
25+
26+
string versionInfo = GlobalSettings.Version.ToString();
27+
28+
// The GlobalSettings.Version returned string should contain :
29+
// version:'0.17.0' LibGit2Sharp version number.
30+
// git2SharpHash:'unknown' ( when compiled from source ) else LibGit2Sharp library hash.
31+
// git2hash: '06d772d' LibGit2 library hash.
32+
// arch: 'x86' or 'amd64' LibGit2 target.
33+
// git2Features: 'Threads, Ssh' LibGit2 features compiled with.
34+
string regex = @"^(?<version>\d{1,}\.\d{1,2}\.\d{1,3})-(?<git2SharpHash>\w+)-(?<git2Hash>\w+) \((?<arch>\w+) - (?<git2Features>(?:\w*(?:, )*\w+)*)\)$";
35+
36+
Assert.NotNull(versionInfo);
37+
38+
Match regexResult = Regex.Match(versionInfo, regex);
39+
40+
Assert.True(regexResult.Success, "The following version string format is enforced:" +
41+
"Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)");
42+
43+
GroupCollection matchGroups = regexResult.Groups;
44+
45+
// Check that all groups are valid
46+
foreach (Group group in matchGroups)
47+
{
48+
Assert.True(group.Success);
49+
}
50+
}
1851
}
1952
}

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7-
using System.Text.RegularExpressions;
87

98
namespace LibGit2Sharp.Tests
109
{
@@ -86,40 +85,6 @@ public void CanCreateStandardRepo()
8685
}
8786
}
8887

89-
[Fact]
90-
public void CanRetrieveValidVersionString()
91-
{
92-
// Version string format is:
93-
// Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)
94-
// Example output:
95-
// "0.17.0-unknown-06d772d (x86 - Threads, Https)"
96-
97-
string versionInfo = Repository.Version;
98-
99-
// The Repository.Version returned string should contain :
100-
// version:'0.17.0' LibGit2Sharp version number.
101-
// git2SharpHash:'unknown' ( when compiled from source ) else LibGit2Sharp library hash.
102-
// git2hash: '06d772d' LibGit2 library hash.
103-
// arch: 'x86' or 'amd64' LibGit2 target.
104-
// git2Features: 'Threads, Ssh' LibGit2 features compiled with.
105-
string regex = @"^(?<version>\d{1,}\.\d{1,2}\.\d{1,3})-(?<git2SharpHash>\w+)-(?<git2Hash>\w+) \((?<arch>\w+) - (?<git2Features>(?:\w*(?:, )*\w+)*)\)$";
106-
107-
Assert.NotNull(versionInfo);
108-
109-
Match regexResult = Regex.Match(versionInfo, regex);
110-
111-
Assert.True(regexResult.Success, "The following version string format is enforced:" +
112-
"Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)");
113-
114-
GroupCollection matchGroups = regexResult.Groups;
115-
116-
// Check that all groups are valid
117-
foreach(Group group in matchGroups)
118-
{
119-
Assert.True(group.Success);
120-
}
121-
}
122-
12388
[Fact]
12489
public void CanCreateStandardRepoAndSpecifyAFolderWhichWillContainTheNewlyCreatedGitDirectory()
12590
{

LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected static void InconclusiveIf(Func<bool> predicate, string message)
210210
throw new SkipException(message);
211211
}
212212

213-
protected void RequiresDotNetOrMonoGreaterThanOrEqualTo(Version minimumVersion)
213+
protected void RequiresDotNetOrMonoGreaterThanOrEqualTo(System.Version minimumVersion)
214214
{
215215
Type type = Type.GetType("Mono.Runtime");
216216

@@ -229,11 +229,11 @@ protected void RequiresDotNetOrMonoGreaterThanOrEqualTo(Version minimumVersion)
229229

230230
var version = (string) displayName.Invoke(null, null);
231231

232-
Version current;
232+
System.Version current;
233233

234234
try
235235
{
236-
current = new Version(version.Split(' ')[0]);
236+
current = new System.Version(version.Split(' ')[0]);
237237
}
238238
catch (Exception e)
239239
{

LibGit2Sharp/GlobalSettings.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,29 @@ namespace LibGit2Sharp
88
/// </summary>
99
public static class GlobalSettings
1010
{
11+
private static readonly Lazy<Version> version = new Lazy<Version>(Version.Build);
12+
1113
/// <summary>
1214
/// Returns all the optional features that were compiled into
1315
/// libgit2.
1416
/// </summary>
1517
/// <returns>A <see cref="BuiltInFeatures"/> enumeration.</returns>
18+
[Obsolete("This method will be removed in the next release. Use Version.Features instead.")]
1619
public static BuiltInFeatures Features()
1720
{
18-
return Proxy.git_libgit2_features();
21+
return Version.Features;
22+
}
23+
24+
/// <summary>
25+
/// Returns information related to the current LibGit2Sharp
26+
/// library.
27+
/// </summary>
28+
public static Version Version
29+
{
30+
get
31+
{
32+
return version.Value;
33+
}
1934
}
2035

2136
/// <summary>

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
<Compile Include="TreeEntryDefinition.cs" />
334334
<Compile Include="UserCanceledException.cs" />
335335
<Compile Include="UsernamePasswordCredentials.cs" />
336+
<Compile Include="Version.cs" />
336337
<Compile Include="VoidReference.cs" />
337338
<Compile Include="Core\RawContentStream.cs" />
338339
<Compile Include="Core\Handles\OdbStreamSafeHandle.cs" />

LibGit2Sharp/Repository.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public sealed class Repository : IRepository
3535
private readonly Stack<IDisposable> toCleanup = new Stack<IDisposable>();
3636
private readonly Ignore ignore;
3737
private readonly SubmoduleCollection submodules;
38-
private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion);
3938
private readonly Lazy<PathCase> pathCase;
4039

4140
/// <summary>
@@ -951,38 +950,10 @@ internal T RegisterForCleanup<T>(T disposable) where T : IDisposable
951950
/// <para>Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)</para>
952951
/// </para>
953952
/// </summary>
953+
[Obsolete("This property will be removed in the next release. Use GlobalSettings.Version instead.")]
954954
public static string Version
955955
{
956-
get { return versionRetriever.Value; }
957-
}
958-
959-
private static string RetrieveVersion()
960-
{
961-
Assembly assembly = typeof(Repository).Assembly;
962-
963-
Version version = assembly.GetName().Version;
964-
965-
string libgit2Hash = ReadContentFromResource(assembly, "libgit2_hash.txt");
966-
string libgit2sharpHash = ReadContentFromResource(assembly, "libgit2sharp_hash.txt");
967-
string features = GlobalSettings.Features().ToString();
968-
969-
return string.Format(
970-
CultureInfo.InvariantCulture,
971-
"{0}-{1}-{2} ({3} - {4})",
972-
version.ToString(3),
973-
libgit2sharpHash.Substring(0, 7),
974-
libgit2Hash.Substring(0, 7),
975-
NativeMethods.ProcessorArchitecture,
976-
features);
977-
}
978-
979-
private static string ReadContentFromResource(Assembly assembly, string partialResourceName)
980-
{
981-
string name = string.Format(CultureInfo.InvariantCulture, "LibGit2Sharp.{0}", partialResourceName);
982-
using (var sr = new StreamReader(assembly.GetManifestResourceStream(name)))
983-
{
984-
return sr.ReadLine();
985-
}
956+
get { return GlobalSettings.Version.ToString(); }
986957
}
987958

988959
/// <summary>

LibGit2Sharp/Version.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.Globalization;
2+
using System.IO;
3+
using System.Reflection;
4+
using LibGit2Sharp.Core;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// Gets the current LibGit2Sharp version.
10+
/// </summary>
11+
public class Version
12+
{
13+
private readonly Assembly assembly = typeof(Repository).Assembly;
14+
15+
/// <summary>
16+
/// Needed for mocking purposes.
17+
/// </summary>
18+
protected Version()
19+
{ }
20+
21+
internal static Version Build()
22+
{
23+
return new Version();
24+
}
25+
26+
/// <summary>
27+
/// Returns the <see cref="System.Version" /> of the
28+
/// the LibGit2Sharp library.
29+
/// </summary>
30+
public virtual System.Version MajorMinorPatch
31+
{
32+
get
33+
{
34+
return assembly.GetName().Version;
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Returns all the optional features that were compiled into
40+
/// libgit2.
41+
/// </summary>
42+
/// <returns>A <see cref="BuiltInFeatures"/> enumeration.</returns>
43+
public virtual BuiltInFeatures Features
44+
{
45+
get
46+
{
47+
return Proxy.git_libgit2_features();
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Returns the SHA hash for the libgit2 library.
53+
/// </summary>
54+
public virtual string LibGit2CommitSha
55+
{
56+
get
57+
{
58+
return ReadContentFromResource(assembly, "libgit2_hash.txt").Substring(0, 7);
59+
}
60+
}
61+
62+
/// <summary>
63+
/// Returns the SHA hash for the LibGit2Sharp library.
64+
/// </summary>
65+
public virtual string LibGit2SharpCommitSha
66+
{
67+
get
68+
{
69+
return ReadContentFromResource(assembly, "libgit2sharp_hash.txt").Substring(0, 7);
70+
}
71+
}
72+
73+
/// <summary>
74+
/// Returns a string representing the LibGit2Sharp version.
75+
/// </summary>
76+
/// <para>
77+
/// The format of the version number is as follows:
78+
/// <para>Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features)</para>
79+
/// </para>
80+
/// <returns></returns>
81+
public override string ToString()
82+
{
83+
return RetrieveVersion();
84+
}
85+
86+
private string RetrieveVersion()
87+
{
88+
string features = Features.ToString();
89+
90+
return string.Format(
91+
CultureInfo.InvariantCulture,
92+
"{0}-{1}-{2} ({3} - {4})",
93+
MajorMinorPatch.ToString(3),
94+
LibGit2SharpCommitSha,
95+
LibGit2CommitSha,
96+
NativeMethods.ProcessorArchitecture,
97+
features);
98+
}
99+
100+
private string ReadContentFromResource(Assembly assembly, string partialResourceName)
101+
{
102+
string name = string.Format(CultureInfo.InvariantCulture, "LibGit2Sharp.{0}", partialResourceName);
103+
using (var sr = new StreamReader(assembly.GetManifestResourceStream(name)))
104+
{
105+
return sr.ReadLine();
106+
}
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)
0