From e9e1a14765322a4eeac53b7a3dcba35b39560659 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 17 Jan 2024 08:23:27 -0700 Subject: [PATCH] Implemented -DPROJECT_NAME_SHOW_GIT_COMMIT_PARENTS option (#597) Implemented usage of previously added global cmake variable, `PROJECT_NAME_SHOW_GIT_COMMIT_PARENTS` in `tribits_generate_single_repo_version_string`. This is done with an optional argument named INCLUDE_COMMIT_PARENTS whenever calling `tribits_generate_single_repo_version_string` --- .../TribitsHelloWorld_Tests.cmake | 1 + .../TribitsGitRepoVersionInfo.cmake | 135 +++++++++--------- .../package_arch/TribitsGlobalMacros.cmake | 11 +- 3 files changed, 80 insertions(+), 67 deletions(-) diff --git a/test/core/ExamplesUnitTests/TribitsHelloWorld_Tests.cmake b/test/core/ExamplesUnitTests/TribitsHelloWorld_Tests.cmake index c9fe109e5..0d674452b 100644 --- a/test/core/ExamplesUnitTests/TribitsHelloWorld_Tests.cmake +++ b/test/core/ExamplesUnitTests/TribitsHelloWorld_Tests.cmake @@ -123,6 +123,7 @@ tribits_add_advanced_test( TribitsHelloWorld_config_git_version_single_repo_two_ -DTribitsHelloWorld_TRIBITS_DIR=${${PROJECT_NAME}_TRIBITS_DIR} -DTribitsHelloWorld_ENABLE_TESTS=ON -DTribitsHelloWorld_GENERATE_REPO_VERSION_FILE=ON + -DTribitsHelloWorld_SHOW_GIT_COMMIT_PARENTS=ON -DGIT_EXECUTABLE=${${PROJECT_NAME}_TRIBITS_DIR}/python_utils/mockprogram.py ../TribitsHelloWorld PASS_REGULAR_EXPRESSION_ALL diff --git a/tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake b/tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake index 9c026d4af..588ee720b 100644 --- a/tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake +++ b/tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake @@ -106,63 +106,6 @@ function(tribits_git_repo_sha1 gitRepoDir gitRepoSha1Out) endfunction() -# @FUNCTION: tribits_generate_commit_info_string() -# -# Get the formatted commit info containing commit's SHA1, -# author, date, email, and 80 character summary. -# -# Usage: -# tribits_generate_commit_info_string( -# commitInfoStringOut) -# -# NOTE: Below, it is fine if ${maxSummaryLen} > len(${gitCmndOutput}) as -# string(SUBSTRING ...) will just shorten this to the length of the string. -# -function(tribits_generate_commit_info_string gitRepoDir gitCommitSha1 - commitInfoStringOut - ) - - # A) Get commit hash, author, date, and email - - execute_process( - COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%h [%ad] <%ae>" ${gitCommitSha1} - WORKING_DIRECTORY ${gitRepoDir} - RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput - OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE - ) - - if (NOT gitCmndRtn STREQUAL 0) - message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0" - " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!") - set(gitVersionLine "Error, could not get version info!") - else() - set(gitVersionLine "${gitCmndOutput}") - endif() - - # B) Get the first 80 chars of the summary message for more info - - execute_process( - COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%s" ${gitCommitSha1} - WORKING_DIRECTORY ${gitRepoDir} - RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput - OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE - ) - - if (NOT gitCmndRtn STREQUAL 0) - message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0" - " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!") - set(gitSummaryStr "Error, could not get version summary!") - else() - set(maxSummaryLen 80) - string(SUBSTRING "${gitCmndOutput}" 0 ${maxSummaryLen} gitSummaryStr) - endif() - - set(${commitInfoStringOut} - "${gitVersionLine}\n${gitSummaryStr}" PARENT_SCOPE) - -endfunction() - - # @FUNCTION: tribits_generate_single_repo_version_string # # Get the formatted string containing the current git repo version. @@ -170,16 +113,23 @@ endfunction() # Usage: # # tribits_generate_single_repo_version_string( -# ) +# INCLUDE_PARENT_COMMITS [ON|OFF]) # -# If the latest commit contains more than one parent, this function -# will also include formatted output of the commit info of those -# parents. +# If the optional argument ``INCLUDE_PARENT_COMMITS`` is passed, +# then the head commit's parent(s) info will be be included in +# the repo version output string formatted. # function(tribits_generate_single_repo_version_string gitRepoDir repoVersionStringOut ) + cmake_parse_arguments( PARSE_ARGV 2 + PARSE "INCLUDE_COMMIT_PARENTS" # prefix, optional + "" "" # one_value_keywords, multi_value_keyword + ) + tribits_check_for_unparsed_arguments() + tribits_assert_parse_arg_zero_or_one_value(PARSE INCLUDE_COMMIT_PARENTS) + tribits_assert_git_executable() # A) Get HEAD commit's info @@ -209,13 +159,14 @@ function(tribits_generate_single_repo_version_string gitRepoDir # C) Get each parent's commit info and format the output - if (headNumParents GREATER 1) + if (PARSE_INCLUDE_COMMIT_PARENTS) set(parentIdx 1) # Parent commit indexes are 1-based by git foreach(parentSha1 IN LISTS headParentList) # C.1) Get parent commit info string + tribits_generate_commit_info_string( ${gitRepoDir} ${parentSha1} commitInfoString) @@ -226,8 +177,7 @@ function(tribits_generate_single_repo_version_string gitRepoDir "\n *** Parent ${parentIdx}:") string(REPLACE "\n" "\n " commitInfoString "${commitInfoString}") - string(CONCAT outStringBuilder - "${outStringBuilder}" "\n ${commitInfoString}" ) + string(APPEND outStringBuilder "\n ${commitInfoString}" ) math(EXPR parentIdx "${parentIdx}+1") @@ -240,6 +190,63 @@ function(tribits_generate_single_repo_version_string gitRepoDir endfunction() +# @FUNCTION: tribits_generate_commit_info_string() +# +# Get the formatted commit info containing commit's SHA1, +# author, date, email, and 80 character summary. +# +# Usage: +# tribits_generate_commit_info_string( +# commitInfoStringOut) +# +# NOTE: Below, it is fine if ${maxSummaryLen} > len(${gitCmndOutput}) as +# string(SUBSTRING ...) will just shorten this to the length of the string. +# +function(tribits_generate_commit_info_string gitRepoDir gitCommitSha1 + commitInfoStringOut + ) + + # A) Get commit hash, author, date, and email + + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%h [%ad] <%ae>" ${gitCommitSha1} + WORKING_DIRECTORY ${gitRepoDir} + RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE + ) + + if (NOT gitCmndRtn STREQUAL 0) + message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0" + " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!") + set(gitVersionLine "Error, could not get version info!") + else() + set(gitVersionLine "${gitCmndOutput}") + endif() + + # B) Get the first 80 chars of the summary message for more info + + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%s" ${gitCommitSha1} + WORKING_DIRECTORY ${gitRepoDir} + RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE + ) + + if (NOT gitCmndRtn STREQUAL 0) + message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0" + " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!") + set(gitSummaryStr "Error, could not get version summary!") + else() + set(maxSummaryLen 80) + string(SUBSTRING "${gitCmndOutput}" 0 ${maxSummaryLen} gitSummaryStr) + endif() + + set(${commitInfoStringOut} + "${gitVersionLine}\n${gitSummaryStr}" PARENT_SCOPE) + +endfunction() + + function(tribits_assert_git_executable) if (NOT GIT_EXECUTABLE) message(SEND_ERROR "ERROR, the program '${GIT_NAME}' could not be found!" diff --git a/tribits/core/package_arch/TribitsGlobalMacros.cmake b/tribits/core/package_arch/TribitsGlobalMacros.cmake index e5c4d28a2..739d1a53d 100644 --- a/tribits/core/package_arch/TribitsGlobalMacros.cmake +++ b/tribits/core/package_arch/TribitsGlobalMacros.cmake @@ -1219,9 +1219,14 @@ function(tribits_generate_repo_version_file_string PROJECT_REPO_VERSION_FILE_ST set(REPO_VERSION_FILE_STR "") - tribits_generate_single_repo_version_string( - ${CMAKE_CURRENT_SOURCE_DIR} - SINGLE_REPO_VERSION) + if (${${PROJECT_NAME}_SHOW_GIT_COMMIT_PARENTS}) + tribits_generate_single_repo_version_string( + ${CMAKE_CURRENT_SOURCE_DIR} + SINGLE_REPO_VERSION INCLUDE_COMMIT_PARENTS) + else() + tribits_generate_single_repo_version_string( + ${CMAKE_CURRENT_SOURCE_DIR} SINGLE_REPO_VERSION) + endif() string(APPEND REPO_VERSION_FILE_STR "*** Base Git Repo: ${PROJECT_NAME}\n" "${SINGLE_REPO_VERSION}\n" )