From aaecabf4d67afcd913f21f775512ff7445b19127 Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 27 Dec 2022 00:34:20 -0800 Subject: [PATCH] cmake: refactor GitUtilities.cmake Remote branch name can now be resolved from detached HEAD. --- cmake/Modules/GitUtilities.cmake | 70 ++++++++++++++++---------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/cmake/Modules/GitUtilities.cmake b/cmake/Modules/GitUtilities.cmake index 4a94b5b5c..bddc552a6 100644 --- a/cmake/Modules/GitUtilities.cmake +++ b/cmake/Modules/GitUtilities.cmake @@ -6,54 +6,54 @@ endif() set(__GitUtilities ON) -function(git_describe variable path) - execute_process(COMMAND "${GIT_EXECUTABLE}" "describe" - WORKING_DIRECTORY "${path}" - RESULT_VARIABLE result +macro(_git_command) + execute_process( + COMMAND "${GIT_EXECUTABLE}" ${ARGN} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE output ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE ) +endmacro() + +macro(_git_easy_command) + _git_command(${ARGN}) + set(${variable} "${output}" PARENT_SCOPE) +endmacro() + +function(git_current_branch variable) + _git_command(symbolic-ref -q --short HEAD) + + # If a detached head, a ref could still be resolved. + if("${output}" STREQUAL "") + _git_command(describe --all --exact-match) + + # Get the ref, in the form heads/master or + # remotes/origin/master so isolate the final part. + string(REGEX REPLACE ".*/" "" output "${output}") + endif() set(${variable} "${output}" PARENT_SCOPE) endfunction() -function(git_current_branch variable path) - execute_process(COMMAND ${GIT_EXECUTABLE} "symbolic-ref" "--short" "HEAD" - WORKING_DIRECTORY "${path}" - RESULT_VARIABLE result - OUTPUT_VARIABLE output - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - set(${variable} "${output}" PARENT_SCOPE) +function(git_latest_commit variable) + _git_easy_command(rev-parse --short HEAD) endfunction() -function(git_latest_commit variable path) - execute_process(COMMAND ${GIT_EXECUTABLE} "rev-parse" "--short" "HEAD" - WORKING_DIRECTORY "${path}" - RESULT_VARIABLE result - OUTPUT_VARIABLE output - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - set(${variable} "${output}" PARENT_SCOPE) -endfunction() - -function(git_working_tree_dirty variable path) - execute_process(COMMAND ${GIT_EXECUTABLE} "status" "--porcelain" "-uno" - WORKING_DIRECTORY "${path}" - RESULT_VARIABLE result - OUTPUT_VARIABLE output - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE - ) +function(git_working_tree_dirty variable) + _git_command(status --porcelain -uno) if(output STREQUAL "") set(${variable} FALSE PARENT_SCOPE) else() set(${variable} TRUE PARENT_SCOPE) endif() -endfunction() \ No newline at end of file +endfunction() + +function(git_summary variable) + _git_easy_command(log -1 "--format=%h %s") +endfunction() + +function(get_git_dir variable) + _git_easy_command(rev-parse --git-dir) +endfunction()