Debian Codegraph @

cmake/tribits/core/utils/TribitsGitRepoVersionInfo.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 ################################################################################
11 #
12 # Module containing functions for getting Git repo information
13 #
14 ################################################################################
15
16
17 # @FUNCTION: tribits_git_repo_sha1()
18 #
19 # Get the Git repo version SHA1.
20 #
21 # Usage::
22 #
23 # tribits_git_repo_sha1(<gitRepoDir> <gitRepoSha1Out>
24 # FAILURE_MESSAGE_OUT <failureMsgOut>)
25 #
26 # If ``<failureMsgOut>`` is non-empty on input, then the variable
27 # ``<failureMsgOut>`` will be set to a non-empty value on output with the
28 # error message if an error occurs and the SHA1 for ``<gitRepoDir>`` can not
29 # be computed. If ``<failureMsgOut>`` is empty in input, and there is an
30 # error, a ``FATAL_ERROR`` will be raised.
31 #
32 # NOTE: To get the SHA1 for the Git repo ``<gitRepoDir>``, it must contain the
33 # ``.git/`` directory and the ``git`` executable var ``GIT_EXECUTABLE`` must
34 # be set. Otherwise, it is an error.
35 #
36 function(tribits_git_repo_sha1 gitRepoDir gitRepoSha1Out)
37
38 cmake_parse_arguments( PARSE_ARGV 2
39 PARSE "" "" # prefix, options, one_value_keywords
40 "FAILURE_MESSAGE_OUT" # multi_value_keywords
41 )
42 tribits_check_for_unparsed_arguments()
43 tribits_assert_parse_arg_zero_or_one_value(PARSE FAILURE_MESSAGE_OUT)
44
45 set(failureMsg "")
46
47 if (NOT GIT_EXECUTABLE)
48 set(failureMsg "ERROR: The program '${GIT_NAME}' could not be found!")
49 elseif (NOT IS_DIRECTORY "${gitRepoDir}/.git")
50 set(failureMsg "ERROR: The directory ${gitRepoDir}/.git does not exist!")
51 endif()
52
53 set(gitRepoSha1 "")
54 if (failureMsg STREQUAL "")
55 execute_process(
56 COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%H"
57 WORKING_DIRECTORY ${gitRepoDir}
58 RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput
59 OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
60 )
61
62 if (NOT gitCmndRtn STREQUAL 0)
63 set(failureMsg "ERROR: ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0 for repo ${gitRepoDir}!")
64 else()
65 set(gitRepoSha1 "${gitCmndOutput}")
66 endif()
67 endif()
68
69 if (NOT failureMsg STREQUAL "" AND PARSE_FAILURE_MESSAGE_OUT STREQUAL "")
70 message(FATAL_ERROR "${failureMsg}")
71 elseif (PARSE_FAILURE_MESSAGE_OUT)
72 set(${PARSE_FAILURE_MESSAGE_OUT} "${failureMsg}" PARENT_SCOPE)
73 endif()
74 set(${gitRepoSha1Out} "${gitRepoSha1}" PARENT_SCOPE)
75
76 endfunction()
77
78
79 # @FUNCTION: tribits_generate_single_repo_version_string()
80 #
81 # Get the formatted string containing the current git repo version.
82 #
83 # Usage:
84 #
85 # tribits_generate_single_repo_version_string(<gitRepoDir>
86 # <repoVersionStringOut> [INCLUDE_PARENT_COMMITS ON|OFF])
87 #
88 # If the optional argument ``INCLUDE_PARENT_COMMITS <val>`` is passed,
89 # then the head commit's parent(s) info will be be included in
90 # the repo version output string formatted.
91 #
92 function(tribits_generate_single_repo_version_string gitRepoDir
93 repoVersionStringOut
94 )
95
96 cmake_parse_arguments( PARSE_ARGV 2
97 PARSE "" # prefix, optional
98 "INCLUDE_COMMIT_PARENTS" "" # one_value_keywords, multi_value_keyword
99 )
100 tribits_check_for_unparsed_arguments()
101 tribits_assert_parse_arg_zero_or_one_value(PARSE INCLUDE_COMMIT_PARENTS)
102
103 tribits_assert_git_executable()
104
105 # A) Get HEAD commit's info
106
107 tribits_generate_commit_info_string(${gitRepoDir} HEAD commitInfoString)
108
109 set(outStringBuilder ${commitInfoString})
110
111 # B) Get all of HEAD commit's parents into a list
112
113 execute_process(
114 COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%p" HEAD
115 WORKING_DIRECTORY ${gitRepoDir}
116 RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput
117 OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
118 )
119
120 if (NOT gitCmndRtn STREQUAL 0)
121 message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
122 " with output '${gitCmndOutput}' for sha1 ${gitHeadSha1} of repo ${gitRepoDir}!")
123 set(headParentList "Error, could not get commit's parents!")
124 else()
125 string(REGEX REPLACE " +" ";" headParentList "${gitCmndOutput}")
126 endif()
127
128 list(LENGTH headParentList headNumParents)
129
130 # C) Get each parent's commit info and format the output
131
132 if (PARSE_INCLUDE_COMMIT_PARENTS)
133
134 set(parentIdx 1) # Parent commit indexes are 1-based by git
135
136 foreach(parentSha1 IN LISTS headParentList)
137
138 # C.1) Get parent commit info string
139
140 tribits_generate_commit_info_string(
141 ${gitRepoDir} ${parentSha1}
142 commitInfoString)
143
144 # C.2) Format parent string to be pretty in config output
145
146 string(APPEND outStringBuilder
147 "\n *** Parent ${parentIdx}:")
148 string(REPLACE "\n" "\n "
149 commitInfoString "${commitInfoString}")
150 string(APPEND outStringBuilder "\n ${commitInfoString}" )
151
152 math(EXPR parentIdx "${parentIdx}+1")
153
154 endforeach()
155
156 endif()
157
158 set(${repoVersionStringOut} "${outStringBuilder}" PARENT_SCOPE)
159
160 endfunction()
161
162
163 # @FUNCTION: tribits_generate_commit_info_string()
164 #
165 # Get the formatted commit info containing commit's SHA1,
166 # author, date, email, and 80 character summary.
167 #
168 # Usage:
169 # tribits_generate_commit_info_string(<gitRepoDir> <gitCommitSha1>
170 # commitInfoStringOut)
171 #
172 # NOTE: Below, it is fine if ${maxSummaryLen} > len(${gitCmndOutput}) as
173 # string(SUBSTRING ...) will just shorten this to the length of the string.
174 #
175 function(tribits_generate_commit_info_string gitRepoDir gitCommitSha1
176 commitInfoStringOut
177 )
178
179 # A) Get commit hash, author, date, and email
180
181 execute_process(
182 COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%h [%ad] <%ae>" ${gitCommitSha1}
183 WORKING_DIRECTORY ${gitRepoDir}
184 RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput
185 OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
186 )
187
188 if (NOT gitCmndRtn STREQUAL 0)
189 message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
190 " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!")
191 set(gitVersionLine "Error, could not get version info!")
192 else()
193 set(gitVersionLine "${gitCmndOutput}")
194 endif()
195
196 # B) Get the first 80 chars of the summary message for more info
197
198 execute_process(
199 COMMAND ${GIT_EXECUTABLE} log -1 "--pretty=format:%s" ${gitCommitSha1}
200 WORKING_DIRECTORY ${gitRepoDir}
201 RESULT_VARIABLE gitCmndRtn OUTPUT_VARIABLE gitCmndOutput
202 OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
203 )
204
205 if (NOT gitCmndRtn STREQUAL 0)
206 message(FATAL_ERROR "ERROR, ${GIT_EXECUTABLE} command returned ${gitCmndRtn}!=0"
207 " with output '${gitCmndOutput}' for sha1 ${gitCommitSha1} of repo ${gitRepoDir}!")
208 set(gitSummaryStr "Error, could not get version summary!")
209 else()
210 set(maxSummaryLen 80)
211 string(SUBSTRING "${gitCmndOutput}" 0 ${maxSummaryLen} gitSummaryStr)
212 endif()
213
214 set(${commitInfoStringOut}
215 "${gitVersionLine}\n${gitSummaryStr}" PARENT_SCOPE)
216
217 endfunction()
218
219
220 function(tribits_assert_git_executable)
221 if (NOT GIT_EXECUTABLE)
222 message(SEND_ERROR "ERROR, the program '${GIT_NAME}' could not be found!"
223 " We can not generate the repo version file!")
224 endif()
225 endfunction()