From ee37e41723114e7e157f027c60b25371a6122ba3 Mon Sep 17 00:00:00 2001 From: Maximilian Seidler <78690852+PaideiaDilemma@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:41:06 +0000 Subject: [PATCH] misc: add git commit hash to --version when not on tag v${VERSION} (#590) --- CMakeLists.txt | 21 ++++++++++++++++++++- src/main.cpp | 8 +++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0da7db9..704d079 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,9 +28,28 @@ set(CMAKE_CXX_STANDARD 23) add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-unused-value -Wno-missing-field-initializers -Wno-narrowing) +# get git commit +execute_process( + OUTPUT_VARIABLE GIT_SHORT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND git rev-parse --short HEAD +) + +# get git commit of v$VERSION +execute_process( + OUTPUT_VARIABLE GIT_TAG_SHORT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND git rev-parse --short v${VERSION} +) + +add_compile_definitions(HYPRLOCK_VERSION="${VERSION}") +add_compile_definitions(HYPRLOCK_COMMIT="${GIT_SHORT_HASH}") +add_compile_definitions(HYPRLOCK_VERSION_COMMIT="${GIT_TAG_SHORT_HASH}") + # position independent build: __FILE__ add_compile_options(-fmacro-prefix-map=${CMAKE_SOURCE_DIR}/=) -add_compile_definitions(HYPRLOCK_VERSION="${VERSION}") # dependencies message(STATUS "Checking deps...") diff --git a/src/main.cpp b/src/main.cpp index d797503..a4572ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "src/helpers/Log.hpp" #include #include +#include void help() { std::cout << "Usage: hyprlock [options]\n\n" @@ -46,7 +47,12 @@ int main(int argc, char** argv, char** envp) { } if (arg == "--version" || arg == "-V") { - std::cout << "Hyprlock version " << HYPRLOCK_VERSION << "\n"; + constexpr bool ISTAGGEDRELEASE = std::string_view(HYPRLOCK_COMMIT) == HYPRLOCK_VERSION_COMMIT; + + std::cout << "Hyprlock version v" << HYPRLOCK_VERSION; + if (!ISTAGGEDRELEASE) + std::cout << " (commit " << HYPRLOCK_COMMIT << ")"; + std::cout << std::endl; return 0; }