FindGit.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ################################################################################
  2. #
  3. # Program: 3D Slicer
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # See COPYRIGHT.txt
  8. # or http://www.slicer.org/copyright/copyright.txt for details.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
  17. # and was partially funded by NIH grant 3P41RR013218-12S1
  18. #
  19. # AG 2013-02-18: I got it from here
  20. # https://github.com/Slicer/Slicer/blob/master/CMake/FindGit.cmake
  21. # license is BSD
  22. #
  23. ################################################################################
  24. #
  25. # The module defines the following variables:
  26. # GIT_EXECUTABLE - path to git command line client
  27. # GIT_FOUND - true if the command line client was found
  28. #
  29. # If the command line client executable is found the macro
  30. # GIT_WC_INFO(<dir> <var-prefix>)
  31. # is defined to extract information of a git working copy at
  32. # a given location.
  33. #
  34. # The macro defines the following variables:
  35. # <var-prefix>_WC_REVISION_HASH - Current SHA1 hash
  36. # <var-prefix>_WC_REVISION - Current SHA1 hash
  37. # <var-prefix>_WC_REVISION_NAME - Name associated with <var-prefix>_WC_REVISION_HASH
  38. # <var-prefix>_WC_URL - output of command `git config --get remote.origin.url'
  39. # <var-prefix>_WC_ROOT - Same value as working copy URL
  40. # <var-prefix>_WC_GITSVN - Set to false
  41. #
  42. # ... and also the following ones if it's a git-svn repository:
  43. # <var-prefix>_WC_GITSVN - Set to True if it is a
  44. # <var-prefix>_WC_INFO - output of command `git svn info'
  45. # <var-prefix>_WC_URL - url of the associated SVN repository
  46. # <var-prefix>_WC_ROOT - root url of the associated SVN repository
  47. # <var-prefix>_WC_REVISION - current SVN revision number
  48. # <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit
  49. # <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit
  50. # <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit
  51. # <var-prefix>_WC_LAST_CHANGED_LOG - last log of base revision
  52. #
  53. # Example usage:
  54. # find_package(Git)
  55. # if(GIT_FOUND)
  56. # GIT_WC_INFO(${PROJECT_SOURCE_DIR} Project)
  57. # message("Current revision is ${Project_WC_REVISION_HASH}")
  58. # message("git found: ${GIT_EXECUTABLE}")
  59. # endif()
  60. #
  61. # Look for 'git' or 'eg' (easy git)
  62. #
  63. set(git_names git eg)
  64. # Prefer .cmd variants on Windows unless running in a Makefile
  65. # in the MSYS shell.
  66. #
  67. if(WIN32)
  68. if(NOT CMAKE_GENERATOR MATCHES "MSYS")
  69. # Note: Due to a bug in 'git.cmd' preventing it from returning the exit code of 'git',
  70. # we excluded it from the list of executables to search.
  71. # See http://code.google.com/p/msysgit/issues/detail?id=428
  72. # TODO Check if 'git' exists, get the associated version, if the corresponding version
  73. # is known to have a working version of 'git.cmd', use it.
  74. set(git_names git eg.cmd eg)
  75. endif()
  76. endif()
  77. find_program(GIT_EXECUTABLE ${git_names}
  78. PATHS
  79. "C:/Program Files/Git/bin"
  80. "C:/Program Files (x86)/Git/bin"
  81. DOC "git command line client")
  82. mark_as_advanced(GIT_EXECUTABLE)
  83. if(GIT_EXECUTABLE)
  84. macro(GIT_WC_INFO dir prefix)
  85. execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --verify -q --short=7 HEAD
  86. WORKING_DIRECTORY ${dir}
  87. ERROR_VARIABLE GIT_error
  88. OUTPUT_VARIABLE ${prefix}_WC_REVISION_HASH
  89. OUTPUT_STRIP_TRAILING_WHITESPACE)
  90. set(${prefix}_WC_REVISION ${${prefix}_WC_REVISION_HASH})
  91. if(NOT ${GIT_error} EQUAL 0)
  92. message(SEND_ERROR "Command \"${GIT_EXECUTBALE} rev-parse --verify -q --short=7 HEAD\" in directory ${dir} failed with output:\n${GIT_error}")
  93. else(NOT ${GIT_error} EQUAL 0)
  94. execute_process(COMMAND ${GIT_EXECUTABLE} name-rev ${${prefix}_WC_REVISION_HASH}
  95. WORKING_DIRECTORY ${dir}
  96. OUTPUT_VARIABLE ${prefix}_WC_REVISION_NAME
  97. OUTPUT_STRIP_TRAILING_WHITESPACE)
  98. endif(NOT ${GIT_error} EQUAL 0)
  99. execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
  100. WORKING_DIRECTORY ${dir}
  101. OUTPUT_VARIABLE ${prefix}_WC_URL
  102. OUTPUT_STRIP_TRAILING_WHITESPACE)
  103. set(${prefix}_WC_GITSVN False)
  104. # Check if this git is likely to be a git-svn repository
  105. execute_process(COMMAND ${GIT_EXECUTABLE} config --get-regexp "^svn-remote"
  106. WORKING_DIRECTORY ${dir}
  107. OUTPUT_VARIABLE git_config_output
  108. OUTPUT_STRIP_TRAILING_WHITESPACE
  109. )
  110. if(NOT "${git_config_output}" STREQUAL "")
  111. # In case git-svn is used, attempt to extract svn info
  112. execute_process(COMMAND ${GIT_EXECUTABLE} svn info
  113. WORKING_DIRECTORY ${dir}
  114. TIMEOUT 3
  115. ERROR_VARIABLE git_svn_info_error
  116. OUTPUT_VARIABLE ${prefix}_WC_INFO
  117. RESULT_VARIABLE git_svn_info_result
  118. OUTPUT_STRIP_TRAILING_WHITESPACE)
  119. if(${git_svn_info_result} EQUAL 0)
  120. set(${prefix}_WC_GITSVN True)
  121. string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*"
  122. "\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}")
  123. string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
  124. "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}")
  125. string(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*"
  126. "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}")
  127. string(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*"
  128. "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}")
  129. string(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*"
  130. "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}")
  131. string(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*"
  132. "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}")
  133. endif(${git_svn_info_result} EQUAL 0)
  134. endif(NOT "${git_config_output}" STREQUAL "")
  135. # If there is no 'remote.origin', default to "NA" value and print a warning message.
  136. if(NOT ${prefix}_WC_URL)
  137. message(WARNING "No remote origin set for git repository: ${dir}" )
  138. set( ${prefix}_WC_URL "NA" )
  139. else()
  140. set(${prefix}_WC_ROOT ${${prefix}_WC_URL})
  141. endif()
  142. endmacro(GIT_WC_INFO)
  143. endif(GIT_EXECUTABLE)
  144. # Handle the QUIETLY and REQUIRED arguments and set GIT_FOUND to TRUE if
  145. # all listed variables are TRUE
  146. include(FindPackageHandleStandardArgs)
  147. find_package_handle_standard_args(Git DEFAULT_MSG GIT_EXECUTABLE)