Skip to content

Commit 2ca1913

Browse files
committed
Add Repository.Version static property
This property returns a string containing the LibGit2Sharp version number, the commit hash it's been built against (if available), the libgit2 commit hash and the processor architecture.
1 parent 412010c commit 2ca1913

8 files changed

Lines changed: 74 additions & 5 deletions

File tree

CI-build.msbuild

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
<UsingTask AssemblyFile="$(MSBuildProjectDirectory)/Lib/xUnit/xunit.runner.msbuild.dll"
1010
TaskName="Xunit.Runner.MSBuild.xunit" />
1111
<Target Name="Clean">
12-
<!-- Workaround for xbuild -->
12+
<Message Text="Commit SHA = $(CommitSha)" />
13+
14+
<WriteLinesToFile Condition="'$(CommitSha)' != ''"
15+
File="$(RootDir)\LibGit2Sharp\libgit2sharp_hash.txt"
16+
Lines="$(CommitSha)"
17+
Overwrite="true" />
18+
19+
<!-- Workaround for xbuild -->
1320
<Exec Condition=" ('$(OS)' != 'Windows_NT') " Command=" rm -r -f $(DeployFolder) " />
1421
<Exec Condition=" ('$(OS)' != 'Windows_NT') " Command=" rm -r -f $(TestBuildDir) " />
1522

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ static NativeMethods()
1818
{
1919
string originalAssemblypath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
2020

21-
//TODO: When amd64 version of libgit2.dll is available, value this depending of the size of an IntPtr
22-
const string currentArchSubPath = "NativeBinaries/x86";
21+
string currentArchSubPath = "NativeBinaries/" + ProcessorArchitecture;
2322

2423
string path = Path.Combine(Path.GetDirectoryName(originalAssemblypath), currentArchSubPath);
2524

@@ -37,6 +36,19 @@ private static void ThreadsShutdown(object sender, EventArgs e)
3736
git_threads_shutdown();
3837
}
3938

39+
public static string ProcessorArchitecture
40+
{
41+
get
42+
{
43+
//TODO: When amd64 version of libgit2.dll is available, uncomment the following lines
44+
//if (IntPtr.Size == 8)
45+
//{
46+
// return "amd64";
47+
//}
48+
49+
return "x86";
50+
}
51+
}
4052
private static bool IsRunningOnLinux()
4153
{
4254
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@
132132
<ItemGroup>
133133
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
134134
</ItemGroup>
135+
<ItemGroup>
136+
<EmbeddedResource Include="libgit2_hash.txt" />
137+
</ItemGroup>
138+
<ItemGroup>
139+
<EmbeddedResource Include="libgit2sharp_hash.txt" />
140+
</ItemGroup>
135141
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
136142
<PropertyGroup>
137143
<PreBuildEvent>

LibGit2Sharp/Repository.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
35
using LibGit2Sharp.Core;
46
using LibGit2Sharp.Core.Compat;
57
using LibGit2Sharp.Core.Handles;
@@ -22,6 +24,7 @@ public class Repository : IDisposable
2224
private readonly Lazy<RepositoryInformation> info;
2325
private readonly bool isBare;
2426
private readonly List<SafeHandleBase> handlesToCleanup = new List<SafeHandleBase>();
27+
private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion);
2528

2629
/// <summary>
2730
/// Initializes a new instance of the <see cref = "Repository" /> class.
@@ -356,5 +359,42 @@ internal void RegisterForCleanup(SafeHandleBase handleToCleanup)
356359
{
357360
handlesToCleanup.Add(handleToCleanup);
358361
}
362+
363+
/// <summary>
364+
/// Gets the current LibGit2Sharp version.
365+
/// <para>
366+
/// The format of the version number is as follows:
367+
/// <para>Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64)</para>
368+
/// </para>
369+
/// </summary>
370+
public static string Version
371+
{
372+
get { return versionRetriever.Value; }
373+
}
374+
375+
private static string RetrieveVersion()
376+
{
377+
Assembly assembly = typeof(Repository).Assembly;
378+
379+
Version version = assembly.GetName().Version;
380+
381+
string libgit2Hash = ReadContentFromResource(assembly, "libgit2_hash.txt");
382+
string libgit2sharpHash = ReadContentFromResource(assembly, "libgit2sharp_hash.txt");
383+
384+
return string.Format("{0}-{1}-{2} ({3})",
385+
version.ToString(3),
386+
libgit2sharpHash.Substring(0, 7),
387+
libgit2Hash.Substring(0,7),
388+
NativeMethods.ProcessorArchitecture
389+
);
390+
}
391+
392+
private static string ReadContentFromResource(Assembly assembly, string partialResourceName)
393+
{
394+
using (var sr = new StreamReader(assembly.GetManifestResourceStream(string.Format("LibGit2Sharp.{0}", partialResourceName))))
395+
{
396+
return sr.ReadLine();
397+
}
398+
}
359399
}
360400
}

LibGit2Sharp/libgit2_hash.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a17e882fab4e190d5a052cbc9cf6a238a9166a7c

LibGit2Sharp/libgit2sharp_hash.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unknown

build.libgit2sharp.cmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ SETLOCAL
33
SET BASEDIR=%~dp0
44
SET FrameworkVersion=v4.0.30319
55
SET FrameworkDir=%SystemRoot%\Microsoft.NET\Framework
6+
SET CommitSha=%~1
67

7-
"%FrameworkDir%\%FrameworkVersion%\msbuild.exe" "%BASEDIR%CI-build.msbuild"
8+
"%FrameworkDir%\%FrameworkVersion%\msbuild.exe" "%BASEDIR%CI-build.msbuild" /property:CommitSha=%CommitSha%
89

910
ENDLOCAL
1011

nuget.package/build.nuget.package.cmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SETLOCAL
22
SET BASEDIR=%~dp0
33
SET SRCDIR=%BASEDIR%..\LibGit2Sharp\
4+
SET CommitSha=%~1
45

56
REM the nuspec file needs to be next to the csproj, so copy it there during the pack operation
67
COPY "%BASEDIR%LibGit2Sharp.nuspec" "%SRCDIR%"
@@ -9,7 +10,7 @@ PUSHD "%BASEDIR%"
910

1011
DEL *.nupkg
1112

12-
CMD /c "..\build.libgit2sharp.cmd"
13+
CMD /c "..\build.libgit2sharp.cmd %CommitSha%"
1314

1415
IF %ERRORLEVEL% NEQ 0 GOTO EXIT
1516

0 commit comments

Comments
 (0)