Skip to content

Commit

Permalink
Implemented -DPROJECT_NAME_SHOW_GIT_COMMIT_PARENTS option (#597)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
achauphan committed Jan 17, 2024
1 parent f6b6442 commit e9e1a14
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 67 deletions.
1 change: 1 addition & 0 deletions test/core/ExamplesUnitTests/TribitsHelloWorld_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
135 changes: 71 additions & 64 deletions tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,80 +106,30 @@ 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(<gitRepoDir> <gitCommitSha1>
# 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.
#
# Usage:
#
# tribits_generate_single_repo_version_string(<gitRepoDir>
# <repoVersionStringOut>)
# <repoVersionStringOut> 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
Expand Down Expand Up @@ -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)
Expand All @@ -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")

Expand All @@ -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(<gitRepoDir> <gitCommitSha1>
# 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!"
Expand Down
11 changes: 8 additions & 3 deletions tribits/core/package_arch/TribitsGlobalMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Expand Down

0 comments on commit e9e1a14

Please sign in to comment.