cmake: refactor GitUtilities.cmake

Remote branch name can now be resolved from detached HEAD.
This commit is contained in:
James R 2022-12-27 00:34:20 -08:00
parent 2533ebba23
commit aaecabf4d6

View file

@ -6,54 +6,54 @@ endif()
set(__GitUtilities ON) set(__GitUtilities ON)
function(git_describe variable path) macro(_git_command)
execute_process(COMMAND "${GIT_EXECUTABLE}" "describe" execute_process(
WORKING_DIRECTORY "${path}" COMMAND "${GIT_EXECUTABLE}" ${ARGN}
RESULT_VARIABLE result WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE output OUTPUT_VARIABLE output
ERROR_QUIET ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE 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) set(${variable} "${output}" PARENT_SCOPE)
endfunction() endfunction()
function(git_current_branch variable path) function(git_latest_commit variable)
execute_process(COMMAND ${GIT_EXECUTABLE} "symbolic-ref" "--short" "HEAD" _git_easy_command(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() endfunction()
function(git_latest_commit variable path) function(git_working_tree_dirty variable)
execute_process(COMMAND ${GIT_EXECUTABLE} "rev-parse" "--short" "HEAD" _git_command(status --porcelain -uno)
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
)
if(output STREQUAL "") if(output STREQUAL "")
set(${variable} FALSE PARENT_SCOPE) set(${variable} FALSE PARENT_SCOPE)
else() else()
set(${variable} TRUE PARENT_SCOPE) set(${variable} TRUE PARENT_SCOPE)
endif() endif()
endfunction() 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()