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