From 997637b3b466e1f1c95405a3d43a78d78d4ba259 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 10 Dec 2014 10:08:42 +0100 Subject: commit hashes! --- cmake/modules/FindLibgit2.cmake | 33 +++++++++++++++++++++++++++++++++ tests/hawd/CMakeLists.txt | 12 +++++++++--- tests/hawd/dataset.cpp | 40 ++++++++++++++++++++++++++++++---------- tests/hawd/dataset.h | 5 +++-- tests/hawd/state.cpp | 37 +++++++++++++++++++++++++++++++++++++ tests/hawd/state.h | 4 ++++ tests/storagebenchmark.cpp | 2 ++ 7 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 cmake/modules/FindLibgit2.cmake diff --git a/cmake/modules/FindLibgit2.cmake b/cmake/modules/FindLibgit2.cmake new file mode 100644 index 0000000..410eab0 --- /dev/null +++ b/cmake/modules/FindLibgit2.cmake @@ -0,0 +1,33 @@ +# - Try to find the libgit2 library +# Once done this will define +# +# LIBGIT2_FOUND - System has libgit2 +# LIBGIT2_INCLUDE_DIR - The libgit2 include directory +# LIBGIT2_LIBRARIES - The libraries needed to use libgit2 +# LIBGIT2_DEFINITIONS - Compiler switches required for using libgit2 + + +# use pkg-config to get the directories and then use these values +# in the FIND_PATH() and FIND_LIBRARY() calls +#FIND_PACKAGE(PkgConfig) +#PKG_SEARCH_MODULE(PC_LIBGIT2 libgit2) + +SET(LIBGIT2_DEFINITIONS ${PC_LIBGIT2_CFLAGS_OTHER}) + +FIND_PATH(LIBGIT2_INCLUDE_DIR NAMES git2.h + HINTS + ${PC_LIBGIT2_INCLUDEDIR} + ${PC_LIBGIT2_INCLUDE_DIRS} + ) + +FIND_LIBRARY(LIBGIT2_LIBRARIES NAMES git2 + HINTS + ${PC_LIBGIT2_LIBDIR} + ${PC_LIBGIT2_LIBRARY_DIRS} + ) + + +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(libgit2 DEFAULT_MSG LIBGIT2_LIBRARIES LIBGIT2_INCLUDE_DIR) + +MARK_AS_ADVANCED(LIBGIT2_INCLUDE_DIR LIBGIT2_LIBRARIES) diff --git a/tests/hawd/CMakeLists.txt b/tests/hawd/CMakeLists.txt index 413c86b..6400baf 100644 --- a/tests/hawd/CMakeLists.txt +++ b/tests/hawd/CMakeLists.txt @@ -1,6 +1,12 @@ project(hawd) -include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +find_package(Libgit2) + +if (LIBGIT2_FOUND) + add_definitions(-DHAVE_LIBGIT2) +endif (LIBGIT2_FOUND) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${LIBGIT2_INCLUDE_DIR}) set(lib_SRCS dataset.cpp @@ -18,11 +24,11 @@ set(SRCS add_library(libhawd ${lib_SRCS}) qt5_use_modules(libhawd Core) -target_link_libraries(libhawd akonadi2common) +target_link_libraries(libhawd akonadi2common ${LIBGIT2_LIBRARIES}) install(TARGETS libhawd DESTINATION lib) add_executable(${PROJECT_NAME} ${SRCS}) qt5_use_modules(${PROJECT_NAME} Core) -target_link_libraries(${PROJECT_NAME} libhawd) +target_link_libraries(${PROJECT_NAME} libhawd ${LIBGIT2_LIBRARIES}) install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/tests/hawd/dataset.cpp b/tests/hawd/dataset.cpp index 6fc85ce..592612e 100644 --- a/tests/hawd/dataset.cpp +++ b/tests/hawd/dataset.cpp @@ -34,6 +34,8 @@ Dataset::Row::Row(const Row &other) : m_key(other.m_key), m_columns(other.m_columns), m_data(other.m_data), + m_annotation(other.m_annotation), + m_commitHash(other.m_commitHash), m_dataset(other.m_dataset) { } @@ -57,6 +59,8 @@ Dataset::Row &Dataset::Row::operator=(const Row &rhs) m_columns = rhs.m_columns; m_data = rhs.m_data; m_dataset = rhs.m_dataset; + m_annotation = rhs.m_annotation; + m_commitHash = rhs.m_commitHash; return *this; } @@ -74,6 +78,11 @@ void Dataset::Row::annotate(const QString ¬e) m_annotation = note; } +void Dataset::Row::setCommitHash(const QString &hash) +{ + m_commitHash = hash; +} + qint64 Dataset::Row::key() const { if (m_key < 1) { @@ -90,11 +99,16 @@ void Dataset::Row::fromBinary(QByteArray &data) QDataStream stream(&data, QIODevice::ReadOnly); while (!stream.atEnd()) { - stream >> key >> value; + stream >> key; + if (stream.atEnd()) { + break; + } + + stream >> value; if (key == s_annotationKey) { m_annotation = value.toString(); } else if (key == s_hashKey) { - m_hash = value.toString(); + m_commitHash = value.toString(); } else { setValue(key, value); } @@ -105,19 +119,23 @@ QByteArray Dataset::Row::toBinary() const { QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); + QHashIterator it(m_data); while (it.hasNext()) { it.next(); - stream << it.key() << it.value(); + if (it.value().isValid()) { + stream << it.key() << it.value(); + } } - if (!m_hash.isEmpty()) { - stream << s_hashKey << m_hash; + if (!m_commitHash.isEmpty()) { + stream << s_hashKey << QVariant(m_commitHash); } if (!m_annotation.isEmpty()) { - stream << s_annotationKey << m_annotation; + stream << s_annotationKey << QVariant(m_annotation); } + return data; } @@ -169,7 +187,7 @@ QString Dataset::Row::toString(const QStringList &cols, int standardCols, const } if (standardCols & CommitHash) { - strings << m_hash; + strings << m_commitHash; } QHashIterator it(m_data); @@ -189,9 +207,9 @@ QString Dataset::Row::toString(const QStringList &cols, int standardCols, const Dataset::Dataset(const QString &name, const State &state) : m_definition(state.datasetDefinition(name)), - m_storage(state.resultsPath(), name, Storage::ReadWrite) + m_storage(state.resultsPath(), name, Storage::ReadWrite), + m_commitHash(state.commitHash()) { - //TODO: it should use a different file name if the data columns have changed m_storage.startTransaction(); } @@ -252,7 +270,9 @@ void Dataset::eachRow(const std::function &resultHandler) Dataset::Row Dataset::row(qint64 key) { if (key < 1) { - return Row(*this); + Row row(*this); + row.setCommitHash(m_commitHash); + return row; } Row row(*this, key); diff --git a/tests/hawd/dataset.h b/tests/hawd/dataset.h index f23d67c..b91bc24 100644 --- a/tests/hawd/dataset.h +++ b/tests/hawd/dataset.h @@ -47,6 +47,7 @@ public: void setValue(const QString &column, const QVariant &value); QVariant value(const QString &column); void annotate(const QString ¬e); + void setCommitHash(const QString &hash); qint64 key() const; QByteArray toBinary() const; QString toString(const QStringList &cols = QStringList(), int standardCols = All, const QString &seperator = "\t") const; @@ -60,12 +61,11 @@ public: QHash m_columns; QHash m_data; QString m_annotation; - QString m_hash; + QString m_commitHash; const Dataset *m_dataset; friend class Dataset; }; - Dataset(const DatasetDefinition &definition); Dataset(const QString &name, const State &state); ~Dataset(); @@ -83,6 +83,7 @@ public: private: DatasetDefinition m_definition; Storage m_storage; + QString m_commitHash; }; } // namespace HAWD 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 @@ #include +#ifdef HAVE_LIBGIT2 +#include +#endif + static const QString configFileName("hawd.conf"); namespace HAWD @@ -34,6 +38,7 @@ namespace HAWD State::State(const QString &_configPath) : m_valid(true) { + m_commitHash[0] = '\0'; QString configPath = _configPath; if (configPath.isEmpty()) { QDir dir; @@ -98,4 +103,36 @@ QVariant State::configValue(const QString &key) const return m_configData.value(key).toVariant(); } +const char *State::commitHash() const +{ + if (isValid() && m_commitHash[0] == '\0') { + const_cast(this)->findGitHash(); + } + + return m_commitHash; +} + +void State::findGitHash() +{ +#ifdef HAVE_LIBGIT2 + git_buf root = {0}; + int error = git_repository_discover(&root, projectPath().toStdString().data(), 0, NULL); + if (!error) { + git_repository *repo = NULL; + int error = git_repository_open(&repo, root.ptr); + + if (!error) { + git_oid oid; + error = git_reference_name_to_id(&oid, repo, "HEAD" ); + if (!error) { + git_oid_tostr(m_commitHash, sizeof(m_commitHash), &oid); + } + } + + git_repository_free(repo); + } + git_buf_free(&root); +#endif +} + } // namespace HAWD diff --git a/tests/hawd/state.h b/tests/hawd/state.h index 72ac528..fa76724 100644 --- a/tests/hawd/state.h +++ b/tests/hawd/state.h @@ -37,10 +37,14 @@ public: QString resultsPath() const; QString projectPath() const; DatasetDefinition datasetDefinition(const QString &name) const; + const char *commitHash() const; private: + void findGitHash(); + bool m_valid; QJsonObject m_configData; + char m_commitHash[10]; }; } // namespace HAWD diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp index ba14990..31cef06 100644 --- a/tests/storagebenchmark.cpp +++ b/tests/storagebenchmark.cpp @@ -66,6 +66,7 @@ private Q_SLOTS: store.removeFromDisk(); } +public: void testWriteRead_data() { QTest::addColumn("useDb"); @@ -178,6 +179,7 @@ private Q_SLOTS: } } +private Q_SLOTS: void testBufferCreation() { HAWD::Dataset dataset("buffer_creation", m_hawdState); -- cgit v1.2.3