[cmake-developers] added get_git_revision and get_git_branch commands as follow-up to cmake at cmake.org

Daniel Wirtz daniel.wirtz at simtech.uni-stuttgart.de
Fri Sep 25 11:32:24 EDT 2015


Hello all,
here's my proposal for the git convenience functions, see 
http://public.kitware.com/pipermail/cmake/2015-September/061516.html

i've also created a pull request 
https://github.com/Kitware/CMake/pull/185 (before i read 
CONTRIBUTING.rst, sorry)

additionally, can someone hint me as to how to build the html-help 
locally? i want to be able to check the generated help output .. some 
quick search did not get me any how-to's.

cheers,
Daniel


Signed-off-by: Daniel Wirtz <daniel.wirtz at simtech.uni-stuttgart.de>
---
  Modules/FindGit.cmake | 58 
+++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 58 insertions(+)

diff --git a/Modules/FindGit.cmake b/Modules/FindGit.cmake
index b4f7b4b..18f8f59 100644
--- a/Modules/FindGit.cmake
+++ b/Modules/FindGit.cmake
@@ -20,6 +20,24 @@
  #    if(GIT_FOUND)
  #      message("git found: ${GIT_EXECUTABLE}")
  #    endif()
+#
+# For convenience, the package also provides the following functions:
+#
+# ::
+#
+#    git_get_revision(VARNAME [WORKING_DIRECTORY])
+#    git_get_branch(VARNAME [WORKING_DIRECTORY])
+#
+# Both functions return the current Git revision (full ID) and branch 
name, respectively.
+# If the functions are invoked but Git was not found, the configure 
step stops with an error.
+# The arguments are
+#
+# ``VARNAME``
+#   Name of the variable to contain the result.
+#
+# ``WORKING_DIRECTORY``
+#   The working directory at which to execute the git commands.
+#   If not specified, :variable:`CMAKE_CURRENT_SOURCE_DIR` is assumed.
 
#=============================================================================
  # Copyright 2010 Kitware, Inc.
@@ -77,3 +95,43 @@ 
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  find_package_handle_standard_args(Git
                                    REQUIRED_VARS GIT_EXECUTABLE
                                    VERSION_VAR GIT_VERSION_STRING)
+
+# Convenience Git repo & branch information functions
+# Added by Daniel Wirtz<daniel.wirtz at simtech.uni-stuttgart.de>
+function(git_get_revision VARNAME)
+    if (NOT GIT_FOUND)
+        message(FATAL_ERROR "Cannot use git_get_revision: Git was not 
found.")
+    endif()
+    set(WD ${ARGV1}) +    if("${WD}" STREQUAL "")
+        set(WD ${CMAKE_CURRENT_SOURCE_DIR})
+    endif()
+    execute_process(COMMAND ${GIT_EXECUTABLE} rev-list --max-count=1 HEAD
+        OUTPUT_VARIABLE RES
+        ERROR_VARIABLE ERR
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+        WORKING_DIRECTORY ${WD})
+    set(${VARNAME} ${RES} PARENT_SCOPE)
+    if (ERR)
+        message(WARNING "Issuing Git command '${GIT_EXECUTABLE} 
rev-list --max-count=1 HEAD' failed: ${ERR}")
+    endif()
+endfunction()
+
+function(git_get_branch VARNAME)
+    if (NOT GIT_FOUND)
+        message(FATAL_ERROR "Cannot use git_get_branch: Git was not 
found.")
+    endif()
+    set(WD ${ARGV1}) +    if("${WD}" STREQUAL "")
+        set(WD ${CMAKE_CURRENT_SOURCE_DIR})
+    endif()
+    execute_process(COMMAND ${GIT_EXECUTABLE} describe --all
+        OUTPUT_VARIABLE RES
+        ERROR_VARIABLE ERR
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+        WORKING_DIRECTORY ${WD})
+    if (ERR)
+        message(WARNING "Issuing Git command '${GIT_EXECUTABLE} 
describe --all' failed: ${ERR}")
+    endif()
+    set(${VARNAME} ${RES} PARENT_SCOPE)
+endfunction()                                  \ No newline at end of file
-- 
1.9.1



More information about the cmake-developers mailing list