| 1 |
include("${${PROJECT_NAME}_TRIBITS_DIR}/core/utils/MessageWrapper.cmake") |
| 2 |
include("${${PROJECT_NAME}_TRIBITS_DIR}/core/utils/TribitsStripQuotesFromStr.cmake") |
| 3 |
|
| 4 |
|
| 5 |
# @FUNCTION: tribits_get_raw_git_commit_utc_time() |
| 6 |
# |
| 7 |
# Get the git commit date of a repo at a given commit in UTC in the format |
| 8 |
# "2019-03-22 15:34:43 +0000" |
| 9 |
# |
| 10 |
# Usage:: |
| 11 |
# |
| 12 |
# tribits_get_raw_git_commit_utc_time(<repo_base_dir> <commit_Ref> |
| 13 |
# <git_commit_utc_time_out> ) |
| 14 |
# |
| 15 |
# This requires find_package(Git) to have been called before calling this |
| 16 |
# function and it requires a vesrion of git of 2.10.0 or greater (because |
| 17 |
# ``git log`` must support the argument ``--date=iso-local``). |
| 18 |
# |
| 19 |
function(tribits_get_raw_git_commit_utc_time repo_base_dir commit_ref |
| 20 |
git_commit_utc_time_out |
| 21 |
) |
| 22 |
if ("${GIT_EXECUTABLE}" STREQUAL "") |
| 23 |
message_wrapper(FATAL_ERROR "Error, GIT_EXECUTABLE not set!") |
| 24 |
endif() |
| 25 |
if ("${GIT_VERSION_STRING}" STREQUAL "") |
| 26 |
message_wrapper(FATAL_ERROR "Error, GIT_VERSION_STRING not set!") |
| 27 |
endif() |
| 28 |
if (GIT_VERSION_STRING VERSION_LESS "2.10.0") |
| 29 |
message_wrapper(FATAL_ERROR |
| 30 |
"Error, GIT_VERSION_STRING=${GIT_VERSION_STRING} < 2.10.0!") |
| 31 |
endif() |
| 32 |
if (NOT TRIBITS_GET_RAW_GIT_COMMIT_UTC_TIME_UNIT_TEST_MODE) |
| 33 |
set(OLD_ENV_TZ "$ENV{TZ}") |
| 34 |
set(ENV{TZ} GMT) |
| 35 |
execute_process( |
| 36 |
COMMAND "${GIT_EXECUTABLE}" log |
| 37 |
"--format=%cd" --date=iso-local -1 ${commit_ref} |
| 38 |
WORKING_DIRECTORY "${repo_base_dir}" |
| 39 |
OUTPUT_VARIABLE GIT_CMND_OUTPUT |
| 40 |
ERROR_VARIABLE GIT_CMND_OUTPUT |
| 41 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 42 |
RESULT_VARIABLE GIT_CMD_RTN |
| 43 |
TIMEOUT 10 #seconds |
| 44 |
) |
| 45 |
set(ENV{TZ} "${OLD_ENV_TZ}") |
| 46 |
#print_var(ENV{TZ}) |
| 47 |
if (NOT GIT_CMD_RTN STREQUAL "0") |
| 48 |
message(FATAL_ERROR |
| 49 |
"ERROR: GIT_CMD_RTN=${GIT_CMD_RTN} != 0!\n" |
| 50 |
"Error Message: ${GIT_CMND_OUTPUT}" ) |
| 51 |
endif() |
| 52 |
endif() |
| 53 |
# print_var(GIT_CMND_OUTPUT) |
| 54 |
tribits_strip_quotes_from_str("${GIT_CMND_OUTPUT}" git_commit_no_quotes) |
| 55 |
# ToDo: Assert that the date offset is "+0000" or error out! |
| 56 |
set(${git_commit_utc_time_out} "${git_commit_no_quotes}" PARENT_SCOPE) |
| 57 |
endfunction() |
| 58 |
|
| 59 |
|
| 60 |
# @FUNCTION: tribits_get_version_date_from_raw_git_commit_utc_time() |
| 61 |
# |
| 62 |
# Takes input of the form "YYYY-MM-DD hh:mm:ss +0000" from the git command:: |
| 63 |
# |
| 64 |
# git log --format="%cd" --date=iso-local -1 <ref> |
| 65 |
# |
| 66 |
# and returns the string integer YYYYMMDDhh. |
| 67 |
# |
| 68 |
# Usage:: |
| 69 |
# |
| 70 |
# tribits_get_version_date_from_raw_git_commit_utc_time( |
| 71 |
# ""YYYY-MM-DD hh:mm:ss +0000" <version_date_var> ) |
| 72 |
# |
| 73 |
# This returns a 10-digit integer ``YYYYMMDDhh`` that should fit in a 32-bit |
| 74 |
# integer with a max value of ``2^32 / 2 - 1`` = ``2147483647`` and therefore |
| 75 |
# should be good until the last hour of of the last day of the last month of |
| 76 |
# the year 2147 (i.e. `2147 12 31 23` = `2147123123`). |
| 77 |
# |
| 78 |
function(tribits_get_version_date_from_raw_git_commit_utc_time |
| 79 |
git_raw_commit_time_utc version_date_out |
| 80 |
) |
| 81 |
# Split by spaces first " " |
| 82 |
string(REPLACE " " ";" git_raw_commit_time_utc_space_array |
| 83 |
"${git_raw_commit_time_utc}") |
| 84 |
#print_var(git_raw_commit_time_utc_space_array) |
| 85 |
list(GET git_raw_commit_time_utc_space_array 0 YYYY_MM_DD) # YYYY-MM-DD |
| 86 |
list(GET git_raw_commit_time_utc_space_array 1 hh_mm_ss) # hh:mm:ss |
| 87 |
list(GET git_raw_commit_time_utc_space_array 2 utc_offset) # +0000 |
| 88 |
#print_var(YYYY_MM_DD) |
| 89 |
#print_var(hh_mm_ss) |
| 90 |
#print_var(utc_offset) |
| 91 |
if (NOT utc_offset STREQUAL "+0000") |
| 92 |
message_wrapper(FATAL_ERROR "ERROR, '${git_raw_commit_time_utc}' is NOT" |
| 93 |
" in UTC which would have offset '+0000'!") |
| 94 |
endif() |
| 95 |
# Split YYYY-MM-DD into its components |
| 96 |
string(REPLACE "-" ";" YYYY_MM_DD_array "${YYYY_MM_DD}") |
| 97 |
list(GET YYYY_MM_DD_array 0 YYYY) |
| 98 |
list(GET YYYY_MM_DD_array 1 MM) |
| 99 |
list(GET YYYY_MM_DD_array 2 DD) |
| 100 |
# Split hh:mm:ss into its components |
| 101 |
string(REPLACE ":" ";" hh_mm_ss_array "${hh_mm_ss}") |
| 102 |
list(GET hh_mm_ss_array 0 hh) |
| 103 |
# Form the full YYYYMMDDhhmm integer and return |
| 104 |
set(${version_date_out} "${YYYY}${MM}${DD}${hh}" PARENT_SCOPE) |
| 105 |
endfunction() |