1 /*
2  *             Copyright Andrej Mitrovic 2013.
3  *  Distributed under the Boost Software License, Version 1.0.
4  *     (See accompanying file LICENSE_1_0.txt or copy at
5  *           http://www.boost.org/LICENSE_1_0.txt)
6  */
7 module git.version_;
8 
9 import std.exception;
10 import std..string;
11 
12 import deimos.git2.common;
13 import deimos.git2.version_;
14 
15 import git.common;
16 
17 /**
18     Contains version information.
19 */
20 struct VersionInfo
21 {
22     ///
23     this(int major, int minor, int revision)
24     {
25         this.major = major;
26         this.minor = minor;
27         this.revision = revision;
28         this.text = format("%s.%s.%s", major, minor, revision);
29     }
30 
31     ///
32     this(int major, int minor, int revision, string text)
33     {
34         this.major = major;
35         this.minor = minor;
36         this.revision = revision;
37         this.text = text;
38     }
39 
40     /// Major version, e.g. 0.19.1 -> 0
41     int major;
42 
43     /// Minor version, e.g. 0.19.1 -> 19
44     int minor;
45 
46     /// Revision version, e.g. 0.19.1 -> 1
47     int revision;
48 
49     /// Text representation of version, e.g. "0.19.1"
50     string text;
51 
52     string toString()
53     {
54         return format("v%s", text);
55     }
56 
57     int opCmp(in VersionInfo other)
58     const {
59         if (this.major != other.major) return this.major - other.major;
60         if (this.minor != other.minor) return this.minor - other.minor;
61         if (this.revision != other.revision) return this.revision - other.revision;
62         return 0;
63     }
64 }
65 
66 /**
67     Target version this binding is based on.
68 */
69 enum targetLibGitVersion = VersionInfo(LIBGIT2_VER_MAJOR, LIBGIT2_VER_MINOR, LIBGIT2_VER_REVISION, LIBGIT2_VERSION);
70 
71 /**
72     The current version of dlibgit.
73 */
74 enum dlibgitVersion = VersionInfo(0, 50, 0);
75 
76 
77 static assert(targetLibGitVersion == VersionInfo(0, 19, 0) || targetLibGitVersion == VersionInfo(0, 20, 0));
78 
79 /**
80     Return the runtime version of the libgit2 library
81     that has been linked with.
82 */
83 VersionInfo getLibGitVersion()
84 {
85     int major;
86     int minor;
87     int revision;
88     git_libgit2_version(&major, &minor, &revision);
89 
90     return VersionInfo(major, minor, revision);
91 }
92 
93 shared static this()
94 {
95     verifyCompatibleLibgit();
96 }
97 
98 /**
99     Verify at runtime that the loaded version of libgit is the
100     one supported by this version of dlibgit, and that it
101     has features which are required by dlibgit.
102 */
103 void verifyCompatibleLibgit()
104 {
105     auto libgitVersion = getLibGitVersion();
106     enforce(libgitVersion == targetLibGitVersion,
107             format("Error: dlibgit (%s) requires libgit2 (%s).\nCurrently loaded libgit2 version is (%s).",
108                    dlibgitVersion, targetLibGitVersion, libgitVersion));
109 
110     auto features = getLibGitFeatures();
111     enforce(features.usesSSL, "Error: dlibgit requires libgit2 compiled with SSL support.");
112 }