Skip to content

Commit

Permalink
Factor out functions into TribitsGitRepoVersionInfo.cmake (TriBITSPub…
Browse files Browse the repository at this point in the history
…#483)

We need to be breaking up TribitsGlobalMacros.cmake because it is too long.

I also added a new function to get just the 40-char SHA1.
  • Loading branch information
bartlettroscoe committed Jun 2, 2022
1 parent b676236 commit a57e75a
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 69 deletions.
158 changes: 158 additions & 0 deletions tribits/core/package_arch/TribitsGitRepoVersionInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# @HEADER
# ************************************************************************
#
# TriBITS: Tribal Build, Integrate, and Test System
# Copyright 2013 Sandia Corporation
#
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the Corporation nor the names of the
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ************************************************************************
# @HEADER

################################################################################
#
# Module containing functions for getting Git repo information
#
################################################################################


# @FUNCTION: tribits_git_repo_sha1()
#
# Get the Git repo version SHA1.
#
# Usage::
#
# tribits_git_repo_sha1(<gitRepoDir> <gitRepoSha1Out>)
#
function(tribits_git_repo_sha1 gitRepoDir gitRepoSha1Out)

tribits_assert_git_executable()

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:"%H"
WORKING_DIRECTORY ${gitRepoDir}
RESULT_VARIABLE gitCmndRtn
OUTPUT_VARIABLE gitCmndOutput
)
# NOTE: Above we have to add quotes '"' or CMake will not accept the
# command. However, git will put those quotes in the output so we have to
# strip them out below!

if (NOT gitCmndRtn STREQUAL 0)
message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
" for repo ${gitRepoDir}!")
set(gitRepoSha1 "Error, could not get version info!")
else()
# Strip the quotes off :-(
string(LENGTH "${gitCmndOutput}" gitCmndOutputLen)
math(EXPR outputNumCharsToKeep "${gitCmndOutputLen}-2")
string(SUBSTRING "${gitCmndOutput}" 1 ${outputNumCharsToKeep}
gitRepoSha1)
endif()

set(${gitRepoSha1Out} ${gitRepoDir})

endfunction()


# Run the git log command to get the version info for a git repo
#
function(tribits_generate_single_repo_version_string gitRepoDir
repoVersionStringOut
)

tribits_assert_git_executable()

# A) Get the basic version info.

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:"%h [%ad] <%ae>"
WORKING_DIRECTORY ${gitRepoDir}
RESULT_VARIABLE gitCmndRtn
OUTPUT_VARIABLE gitCmndOutput
)
# NOTE: Above we have to add quotes '"' or CMake will not accept the
# command. However, git will put those quotes in the output so we have to
# strip them out later :-(

if (NOT gitCmndRtn STREQUAL 0)
message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
" for repo ${gitRepoDir}!")
set(gitVersionLine "Error, could not get version info!")
else()
# Strip the quotes off :-(
string(LENGTH "${gitCmndOutput}" gitCmndOutputLen)
math(EXPR outputNumCharsToKeep "${gitCmndOutputLen}-2")
string(SUBSTRING "${gitCmndOutput}" 1 ${outputNumCharsToKeep}
gitVersionLine)
endif()

# B) Get the first 80 chars of the summary message for more info

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:"%s"
WORKING_DIRECTORY ${gitRepoDir}
RESULT_VARIABLE gitCmndRtn
OUTPUT_VARIABLE gitCmndOutput
)

if (NOT gitCmndRtn STREQUAL 0)
message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
" for extra repo ${gitRepoDir}!")
set(gitSummaryStr "Error, could not get version summary!")
else()
# Strip ouf quotes and quote the 80 char string
set(maxSummaryLen 80)
math(EXPR maxSummaryLen_PLUS_2 "${maxSummaryLen}+2")
string(LENGTH "${gitCmndOutput}" gitCmndOutputLen)
math(EXPR outputNumCharsToKeep "${gitCmndOutputLen}-2")
string(SUBSTRING "${gitCmndOutput}" 1 ${outputNumCharsToKeep}
gitCmndOutputStripped)
if (gitCmndOutputLen GREATER ${maxSummaryLen_PLUS_2})
string(SUBSTRING "${gitCmndOutputStripped}" 0 ${maxSummaryLen}
gitSummaryStr)
else()
set(gitSummaryStr "${gitCmndOutputStripped}")
endif()
endif()

set(${repoVersionStringOut}
"${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!"
" We can not generate the repo version file!")
endif()
endfunction()
70 changes: 1 addition & 69 deletions tribits/core/package_arch/TribitsGlobalMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ include(TribitsGetVersionDate)
include(TribitsReportInvalidTribitsUsage)
include(TribitsReadAllProjectDepsFilesCreateDepsGraph)
include(TribitsAdjustPackageEnables)
include(TribitsGitRepoVersionInfo)

# Standard TriBITS utilities includes
include(TribitsAddOptionAndDefine)
Expand Down Expand Up @@ -1251,75 +1252,6 @@ macro(tribits_copy_installer_resource _varname _source _destination)
COPYONLY)
endmacro()

# Run the git log command to get the version info for a git repo
#
function(tribits_generate_single_repo_version_string GIT_REPO_DIR
SINGLE_REPO_VERSION_STRING_OUT
)

if (NOT GIT_EXECUTABLE)
message(SEND_ERROR "ERROR, the program '${GIT_NAME}' could not be found!"
" We can not generate the repo version file!")
endif()

# A) Get the basic version info.

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:"%h [%ad] <%ae>"
WORKING_DIRECTORY ${GIT_REPO_DIR}
RESULT_VARIABLE GIT_RETURN
OUTPUT_VARIABLE GIT_OUTPUT
)
# NOTE: Above we have to add quotes '"' or CMake will not accept the
# command. However, git will put those quotes in the output so we have to
# strip them out later :-(

if (NOT GIT_RETURN STREQUAL 0)
message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${GIT_RETURN}!=0"
" for extra repo ${GIT_REPO_DIR}!")
set(GIT_VERSION_INFO "Error, could not get version info!")
else()
# Strip the quotes off :-(
string(LENGTH "${GIT_OUTPUT}" GIT_OUTPUT_LEN)
math(EXPR OUTPUT_NUM_CHARS_TO_KEEP "${GIT_OUTPUT_LEN}-2")
string(SUBSTRING "${GIT_OUTPUT}" 1 ${OUTPUT_NUM_CHARS_TO_KEEP}
GIT_VERSION_INFO)
endif()

# B) Get the first 80 chars of the summary message for more info

execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:"%s"
WORKING_DIRECTORY ${GIT_REPO_DIR}
RESULT_VARIABLE GIT_RETURN
OUTPUT_VARIABLE GIT_OUTPUT
)

if (NOT GIT_RETURN STREQUAL 0)
message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${GIT_RETURN}!=0"
" for extra repo ${GIT_REPO_DIR}!")
set(GIT_VERSION_SUMMARY "Error, could not get version summary!")
else()
# Strip ouf quotes and quote the 80 char string
set(MAX_SUMMARY_LEN 80)
math(EXPR MAX_SUMMARY_LEN_PLUS_2 "${MAX_SUMMARY_LEN}+2")
string(LENGTH "${GIT_OUTPUT}" GIT_OUTPUT_LEN)
math(EXPR OUTPUT_NUM_CHARS_TO_KEEP "${GIT_OUTPUT_LEN}-2")
string(SUBSTRING "${GIT_OUTPUT}" 1 ${OUTPUT_NUM_CHARS_TO_KEEP}
GIT_OUTPUT_STRIPPED)
if (GIT_OUTPUT_LEN GREATER ${MAX_SUMMARY_LEN_PLUS_2})
string(SUBSTRING "${GIT_OUTPUT_STRIPPED}" 0 ${MAX_SUMMARY_LEN}
GIT_SUMMARY_STR)
else()
set(GIT_SUMMARY_STR "${GIT_OUTPUT_STRIPPED}")
endif()
endif()

set(${SINGLE_REPO_VERSION_STRING_OUT}
"${GIT_VERSION_INFO}\n${GIT_SUMMARY_STR}" PARENT_SCOPE)

endfunction()


# Get the versions of all the git repos
#
Expand Down

0 comments on commit a57e75a

Please sign in to comment.