1 # ============================================================================ 2 # Copyright (c) 2011-2012 University of Pennsylvania 3 # Copyright (c) 2013-2016 Andreas Schuh 6 # See COPYING file for license information or visit 7 # https://cmake-basis.github.io/download.html#license 8 # ============================================================================ 10 ############################################################################## 11 # @file RevisionTools.cmake 12 # @brief CMake functions and macros related to revision control systems. 15 ############################################################################## 24 # ============================================================================ 26 # ============================================================================ 32 ## @addtogroup CMakeUtilities 36 # ============================================================================ 38 # ============================================================================ 40 # ---------------------------------------------------------------------------- 41 ## @brief Get current revision of file or directory. 43 # @param [in] URL Absolute path to directory or file. May also be a URL to the 44 # directory or file in the repository. A leading "file://" is 45 # automatically removed such that the svn command treats it as a 47 # @param [out] REV The revision number of URL. If URL is not under revision 48 # control or Subversion_SVN_EXECUTABLE is invalid, "0" is returned. 50 # @returns Sets @p REV to the revision of the working copy/repository 54 if (Subversion_SVN_EXECUTABLE)
55 # remove "file://" from URL 56 string (REGEX REPLACE
"file://" "" TMP "${URL}")
59 COMMAND "${Subversion_SVN_EXECUTABLE}" info
"${TMP}" 62 OUTPUT_STRIP_TRAILING_WHITESPACE
65 message (
"** basis_svn_get_revision()")
66 message ("** svn info: ${OUT}
") 69 if (OUT MATCHES "^(.*\n)?Revision: ([^\n]+).*
" AND NOT CMAKE_MATCH_2 STREQUAL "") 70 set (OUT "${CMAKE_MATCH_2}
") 76 set ("${REV}
" "${OUT}
" PARENT_SCOPE) 79 # ---------------------------------------------------------------------------- 80 ## @brief Get revision number when directory or file was last changed. 82 # @param [in] URL Absolute path to directory or file. May also be a URL to the 83 # directory or file in the repository. A leading "file:
84 # automatically removed such that the svn command treats it as a 86 # @param [out] REV Revision number when URL was last modified. If URL is not 87 # under Subversion control or Subversion_SVN_EXECUTABLE is invalid, 90 # @returns Sets @p REV to revision number at which the working copy/repository 91 # specified by the URL @p URL was last modified. 94 if (Subversion_SVN_EXECUTABLE)
95 # remove "file://" from URL 96 string (REGEX REPLACE
"file://" "" TMP
"${URL}")
99 COMMAND "${Subversion_SVN_EXECUTABLE}" info
"${TMP}" 102 OUTPUT_STRIP_TRAILING_WHITESPACE
105 message (
"** basis_svn_get_revision()")
106 message (
"** svn info: ${OUT}")
108 # extract last changed revision 109 if (OUT MATCHES
"^(.*\n)?Last Changed Rev: ([^\n]+).*" AND NOT CMAKE_MATCH_2 STREQUAL
"")
110 set (OUT
"${CMAKE_MATCH_2}")
116 set (
"${REV}" "${OUT}" PARENT_SCOPE)
119 # ---------------------------------------------------------------------------- 120 ## @brief Get status of revision controlled file. 122 # @param [in] URL Absolute path to directory or file. May also be a URL to 123 # the directory or file in the repository. 124 # A leading "file://" will be removed such that the svn 125 # command treats it as a local path. 126 # @param [out] STATUS The status of URL as returned by 'svn status'. 127 # If the local directory or file is unmodified, an 128 # empty string is returned. An empty string is also 129 # returned when Subversion_SVN_EXECUTABLE is invalid. 131 # @returns Sets @p STATUS to the output of the <tt>svn info</tt> command. 133 if (Subversion_SVN_EXECUTABLE)
134 # remove "file://" from URL 135 string (REGEX REPLACE
"file://" "" TMP
"${URL}")
137 # retrieve SVN status of URL 139 COMMAND "${Subversion_SVN_EXECUTABLE}" status
"${TMP}" 145 set (
"${STATUS}" "${OUT}" PARENT_SCOPE)
147 set (
"${STATUS}" "" PARENT_SCOPE)
151 # ============================================================================ 153 # ============================================================================ 155 # ---------------------------------------------------------------------------- 156 # @brief Determine whether or not a given directory is a Git repository 158 if (GITCOMMAND AND NOT GIT_EXECUTABLE)
159 set (GIT_EXECUTABLE GITCOMMAND)
163 COMMAND "${GIT_EXECUTABLE}" rev-parse
164 WORKING_DIRECTORY
"${DIR}" 173 set (${FLAG}
"TRUE" PARENT_SCOPE)
175 set (${FLAG}
"FALSE" PARENT_SCOPE)
179 # ---------------------------------------------------------------------------- 180 ## @brief Get HEAD commit SHA of file or directory. 182 # @param [in] URL Absolute path to repository directory or single file. 183 # @param [out] REV The short commit SHA when URL was last modified. If URL 184 # is not under Git control or GIT_EXECUTABLE is invalid, 186 # @param [in] ARGN Length of commit SHA to return. 188 # @returns Sets @p REV either to the HEAD commit SHA of the repository at 189 # directory @p URL or the last commit which modified the file. 192 message (FATAL_ERROR
"basis_git_get_revision: Too many arguments")
195 if (GITCOMMAND AND NOT GIT_EXECUTABLE)
196 set (GIT_EXECUTABLE GITCOMMAND)
199 # remove "file://" from URL 200 string (REGEX REPLACE
"file://" "" DIR "${URL}")
201 # retrieve Git commit SHA of HEAD 202 if (IS_DIRECTORY
"${DIR}")
204 COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
205 WORKING_DIRECTORY
"${DIR}" 208 OUTPUT_STRIP_TRAILING_WHITESPACE
211 message (
"** basis_git_get_revision()")
212 message (
"** DIR: ${DIR}")
213 message (
"** OUT: ${OUT}")
215 # retrieve Git commit SHA when file was last modified 220 COMMAND "${GIT_EXECUTABLE}" log -n 1 --
"${OBJ}" 221 WORKING_DIRECTORY
"${DIR}" 224 OUTPUT_STRIP_TRAILING_WHITESPACE
227 message (
"** basis_git_get_revision()")
228 message (
"** DIR: ${DIR}")
229 message (
"** OBJ: ${OBJ}")
230 message (
"** OUT: ${OUT}")
233 if (OUT MATCHES
"commit ([0-9a-f]+).*" AND NOT CMAKE_MATCH_1 STREQUAL
"")
234 set (OUT
"${CMAKE_MATCH_1}")
241 string (SUBSTRING
"${OUT}" 0
"${ARGV2}" OUT)
244 set (
"${REV}" "${OUT}" PARENT_SCOPE)
248 # ============================================================================ 250 # ============================================================================ 252 # ---------------------------------------------------------------------------- 253 ## @brief Get revision of file or directory. 255 # @param [in] URL Absolute path to directory or single file. 256 # @param [out] REV Revision number when directory / repository or file was 257 # last modified, "0" is returned when no known revision 258 # control system is used or revision command not found. 260 # @returns Revision when directory of file was last modified. 262 #
remove "file://" from URL
263 string (REGEX REPLACE
"file://" "" DIR
"${URL}")
265 if (NOT IS_DIRECTORY
"${URL}")
270 # check if directory is part of a Git repository 272 if (IS_GIT_REPOSITORY)
278 set (
"${REV}" "${OUT}" PARENT_SCOPE)
283 # end of Doxygen group function basis_svn_get_last_changed_revision(in URL, out REV)
Get revision number when directory or file was last changed.
function basis_get_revision(in URL, out REV)
Get revision of file or directory.
function basis_is_git_repository(in FLAG, in DIR)
macro basis_get_filename_component()
Alias for the overwritten get_filename_component() function.
function basis_svn_get_revision(in URL, out REV)
Get current revision of file or directory.
option BASIS_DEBUG
Request debugging messages from BASIS functions.
function basis_svn_status(in URL, out STATUS)
Get status of revision controlled file.
function basis_git_get_revision(in URL, out REV, in ARGN)
Get HEAD commit SHA of file or directory.
macro find_package()
Overloaded find_package() command.