diff options
Diffstat (limited to 'tests/hawd/state.cpp')
-rw-r--r-- | tests/hawd/state.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/hawd/state.cpp b/tests/hawd/state.cpp index 3ee7775..4ea741d 100644 --- a/tests/hawd/state.cpp +++ b/tests/hawd/state.cpp | |||
@@ -26,6 +26,10 @@ | |||
26 | 26 | ||
27 | #include <iostream> | 27 | #include <iostream> |
28 | 28 | ||
29 | #ifdef HAVE_LIBGIT2 | ||
30 | #include <git2.h> | ||
31 | #endif | ||
32 | |||
29 | static const QString configFileName("hawd.conf"); | 33 | static const QString configFileName("hawd.conf"); |
30 | 34 | ||
31 | namespace HAWD | 35 | namespace HAWD |
@@ -34,6 +38,7 @@ namespace HAWD | |||
34 | State::State(const QString &_configPath) | 38 | State::State(const QString &_configPath) |
35 | : m_valid(true) | 39 | : m_valid(true) |
36 | { | 40 | { |
41 | m_commitHash[0] = '\0'; | ||
37 | QString configPath = _configPath; | 42 | QString configPath = _configPath; |
38 | if (configPath.isEmpty()) { | 43 | if (configPath.isEmpty()) { |
39 | QDir dir; | 44 | QDir dir; |
@@ -98,4 +103,36 @@ QVariant State::configValue(const QString &key) const | |||
98 | return m_configData.value(key).toVariant(); | 103 | return m_configData.value(key).toVariant(); |
99 | } | 104 | } |
100 | 105 | ||
106 | const char *State::commitHash() const | ||
107 | { | ||
108 | if (isValid() && m_commitHash[0] == '\0') { | ||
109 | const_cast<State *>(this)->findGitHash(); | ||
110 | } | ||
111 | |||
112 | return m_commitHash; | ||
113 | } | ||
114 | |||
115 | void State::findGitHash() | ||
116 | { | ||
117 | #ifdef HAVE_LIBGIT2 | ||
118 | git_buf root = {0}; | ||
119 | int error = git_repository_discover(&root, projectPath().toStdString().data(), 0, NULL); | ||
120 | if (!error) { | ||
121 | git_repository *repo = NULL; | ||
122 | int error = git_repository_open(&repo, root.ptr); | ||
123 | |||
124 | if (!error) { | ||
125 | git_oid oid; | ||
126 | error = git_reference_name_to_id(&oid, repo, "HEAD" ); | ||
127 | if (!error) { | ||
128 | git_oid_tostr(m_commitHash, sizeof(m_commitHash), &oid); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | git_repository_free(repo); | ||
133 | } | ||
134 | git_buf_free(&root); | ||
135 | #endif | ||
136 | } | ||
137 | |||
101 | } // namespace HAWD | 138 | } // namespace HAWD |