From a66c5fd91ba04d9e8c52f35277196f219f1cdf4c Mon Sep 17 00:00:00 2001 From: Valentino Berta Date: Sat, 14 Sep 2024 06:13:56 -0300 Subject: [PATCH] fix: check if git top-level directory matches CMake project source (#3156) --- CMakeLists.txt | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index acb58b6b..63a00dbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,20 +8,41 @@ project(polybar CXX) # a version string is used, so the file supports comments file(STRINGS version.txt version_txt REGEX "^[0-9]+\\.[0-9]+\\.[0-9]+.*$" LIMIT_COUNT 1) -# If we are in a git repo we can get the version information from git describe -execute_process(COMMAND git describe --tags --dirty=-dev + +execute_process(COMMAND git rev-parse --show-toplevel WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - RESULT_VARIABLE git_result - OUTPUT_VARIABLE git_describe + RESULT_VARIABLE git_top_dir_result + OUTPUT_VARIABLE git_top_dir OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) -if(git_result EQUAL "0") - set(APP_VERSION "${git_describe}") -else() - message(STATUS "Could not detect version with git, falling back to built-in version information.") - set(APP_VERSION "${version_txt}") +# Set fallback first, override if more information (from git) is found +set(APP_VERSION "${version_txt}") + +# Prevent formatting errors & resolve symlinks (REALPATH) +get_filename_component(resolved_project_source_dir ${PROJECT_SOURCE_DIR} REALPATH) +if(NOT ("${git_top_dir}" STREQUAL "")) + get_filename_component(git_top_dir ${git_top_dir} REALPATH) endif() +if(git_top_dir_result EQUAL "0" AND git_top_dir STREQUAL resolved_project_source_dir) + # If we are in a git repo we can get the version information from git describe + execute_process(COMMAND git describe --tags --dirty=-dev + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + RESULT_VARIABLE git_describe_result + OUTPUT_VARIABLE git_describe + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) + + if(git_describe_result EQUAL "0") + set(APP_VERSION "${git_describe}") + else() + message(STATUS "Could not detect version with git, falling back to built-in version information.") + endif() + +else() + message(STATUS "CMake and git directory mismatch, falling back to built-in version information.") # Assuming that if git rev-parse doesn't return 0, git describe won't either +endif() + + # Set the default installation prefix to /usr # Otherwise the default value is /usr/local which causes the default config # file to be installed to /usr/local/etc, with /usr, cmake has special handling