|
| 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