11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Reflection ;
35using LibGit2Sharp . Core ;
46using LibGit2Sharp . Core . Compat ;
57using 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}
0 commit comments