using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// Gets the current LibGit2Sharp version. /// public class Version { private readonly Assembly assembly = typeof(Repository).Assembly; /// /// Needed for mocking purposes. /// protected Version() { } internal static Version Build() { return new Version(); } /// /// Returns version of the LibGit2Sharp library. /// public virtual string InformationalVersion { get { var attribute = (AssemblyInformationalVersionAttribute)assembly .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false) .Single(); return attribute.InformationalVersion; } } /// /// Returns all the optional features that were compiled into /// libgit2. /// /// A enumeration. public virtual BuiltInFeatures Features { get { return Proxy.git_libgit2_features(); } } /// /// Returns the SHA hash for the libgit2 library. /// public virtual string LibGit2CommitSha { get { return ReadContentFromResource(assembly, "libgit2_hash.txt").Substring(0, 7); } } /// /// Returns the SHA hash for the LibGit2Sharp library. /// public virtual string LibGit2SharpCommitSha { get { return ReadContentFromResource(assembly, "libgit2sharp_hash.txt").Substring(0, 7); } } /// /// Returns a string representing the LibGit2Sharp version. /// /// /// The format of the version number is as follows: /// Major.Minor.Patch-LibGit2Sharp_abbrev_hash-libgit2_abbrev_hash (x86|amd64 - features) /// /// public override string ToString() { return RetrieveVersion(); } private string RetrieveVersion() { string features = Features.ToString(); return string.Format( CultureInfo.InvariantCulture, "{0}-{1}-{2} ({3} - {4})", InformationalVersion, LibGit2SharpCommitSha, LibGit2CommitSha, Platform.ProcessorArchitecture, features); } private string ReadContentFromResource(Assembly assembly, string partialResourceName) { string name = string.Format(CultureInfo.InvariantCulture, "LibGit2Sharp.{0}", partialResourceName); using (var sr = new StreamReader(assembly.GetManifestResourceStream(name))) { return sr.ReadLine(); } } } }