Andy Green | d169bb7 | 2013-02-18 18:34:10 +0800 | [diff] [blame] | 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 | # |
| 26 | # The module defines the following variables: |
| 27 | # GIT_EXECUTABLE - path to git command line client |
| 28 | # GIT_FOUND - true if the command line client was found |
| 29 | # |
| 30 | # If the command line client executable is found the macro |
| 31 | # GIT_WC_INFO(<dir> <var-prefix>) |
| 32 | # is defined to extract information of a git working copy at |
| 33 | # a given location. |
| 34 | # |
| 35 | # The macro defines the following variables: |
| 36 | # <var-prefix>_WC_REVISION_HASH - Current SHA1 hash |
| 37 | # <var-prefix>_WC_REVISION - Current SHA1 hash |
| 38 | # <var-prefix>_WC_REVISION_NAME - Name associated with <var-prefix>_WC_REVISION_HASH |
| 39 | # <var-prefix>_WC_URL - output of command `git config --get remote.origin.url' |
| 40 | # <var-prefix>_WC_ROOT - Same value as working copy URL |
| 41 | # <var-prefix>_WC_GITSVN - Set to false |
| 42 | # |
| 43 | # ... and also the following ones if it's a git-svn repository: |
| 44 | # <var-prefix>_WC_GITSVN - Set to True if it is a |
| 45 | # <var-prefix>_WC_INFO - output of command `git svn info' |
| 46 | # <var-prefix>_WC_URL - url of the associated SVN repository |
| 47 | # <var-prefix>_WC_ROOT - root url of the associated SVN repository |
| 48 | # <var-prefix>_WC_REVISION - current SVN revision number |
| 49 | # <var-prefix>_WC_LAST_CHANGED_AUTHOR - author of last commit |
| 50 | # <var-prefix>_WC_LAST_CHANGED_DATE - date of last commit |
| 51 | # <var-prefix>_WC_LAST_CHANGED_REV - revision of last commit |
| 52 | # <var-prefix>_WC_LAST_CHANGED_LOG - last log of base revision |
| 53 | # |
| 54 | # Example usage: |
| 55 | # find_package(Git) |
| 56 | # if(GIT_FOUND) |
| 57 | # GIT_WC_INFO(${PROJECT_SOURCE_DIR} Project) |
| 58 | # message("Current revision is ${Project_WC_REVISION_HASH}") |
| 59 | # message("git found: ${GIT_EXECUTABLE}") |
| 60 | # endif() |
| 61 | # |
| 62 | |
| 63 | # Look for 'git' or 'eg' (easy git) |
| 64 | # |
| 65 | set(git_names git eg) |
| 66 | |
| 67 | # Prefer .cmd variants on Windows unless running in a Makefile |
| 68 | # in the MSYS shell. |
| 69 | # |
| 70 | if(WIN32) |
| 71 | if(NOT CMAKE_GENERATOR MATCHES "MSYS") |
| 72 | # Note: Due to a bug in 'git.cmd' preventing it from returning the exit code of 'git', |
| 73 | # we excluded it from the list of executables to search. |
| 74 | # See http://code.google.com/p/msysgit/issues/detail?id=428 |
| 75 | # TODO Check if 'git' exists, get the associated version, if the corresponding version |
| 76 | # is known to have a working version of 'git.cmd', use it. |
| 77 | set(git_names git eg.cmd eg) |
| 78 | endif() |
| 79 | endif() |
| 80 | |
| 81 | find_program(GIT_EXECUTABLE ${git_names} |
| 82 | PATHS |
| 83 | "C:/Program Files/Git/bin" |
| 84 | "C:/Program Files (x86)/Git/bin" |
| 85 | DOC "git command line client") |
| 86 | mark_as_advanced(GIT_EXECUTABLE) |
| 87 | |
| 88 | if(GIT_EXECUTABLE) |
| 89 | macro(GIT_WC_INFO dir prefix) |
| 90 | execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --verify -q --short=7 HEAD |
| 91 | WORKING_DIRECTORY ${dir} |
| 92 | ERROR_VARIABLE GIT_error |
| 93 | OUTPUT_VARIABLE ${prefix}_WC_REVISION_HASH |
| 94 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 95 | set(${prefix}_WC_REVISION ${${prefix}_WC_REVISION_HASH}) |
| 96 | if(NOT ${GIT_error} EQUAL 0) |
| 97 | message(SEND_ERROR "Command \"${GIT_EXECUTBALE} rev-parse --verify -q --short=7 HEAD\" in directory ${dir} failed with output:\n${GIT_error}") |
| 98 | else(NOT ${GIT_error} EQUAL 0) |
| 99 | execute_process(COMMAND ${GIT_EXECUTABLE} name-rev ${${prefix}_WC_REVISION_HASH} |
| 100 | WORKING_DIRECTORY ${dir} |
| 101 | OUTPUT_VARIABLE ${prefix}_WC_REVISION_NAME |
| 102 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 103 | endif(NOT ${GIT_error} EQUAL 0) |
| 104 | |
| 105 | execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url |
| 106 | WORKING_DIRECTORY ${dir} |
| 107 | OUTPUT_VARIABLE ${prefix}_WC_URL |
| 108 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 109 | |
| 110 | set(${prefix}_WC_GITSVN False) |
| 111 | |
| 112 | # Check if this git is likely to be a git-svn repository |
| 113 | execute_process(COMMAND ${GIT_EXECUTABLE} config --get-regexp "^svn-remote" |
| 114 | WORKING_DIRECTORY ${dir} |
| 115 | OUTPUT_VARIABLE git_config_output |
| 116 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 117 | ) |
| 118 | |
| 119 | if(NOT "${git_config_output}" STREQUAL "") |
| 120 | # In case git-svn is used, attempt to extract svn info |
| 121 | execute_process(COMMAND ${GIT_EXECUTABLE} svn info |
| 122 | WORKING_DIRECTORY ${dir} |
| 123 | TIMEOUT 3 |
| 124 | ERROR_VARIABLE git_svn_info_error |
| 125 | OUTPUT_VARIABLE ${prefix}_WC_INFO |
| 126 | RESULT_VARIABLE git_svn_info_result |
| 127 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 128 | |
| 129 | if(${git_svn_info_result} EQUAL 0) |
| 130 | set(${prefix}_WC_GITSVN True) |
| 131 | string(REGEX REPLACE "^(.*\n)?URL: ([^\n]+).*" |
| 132 | "\\2" ${prefix}_WC_URL "${${prefix}_WC_INFO}") |
| 133 | string(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*" |
| 134 | "\\2" ${prefix}_WC_REVISION "${${prefix}_WC_INFO}") |
| 135 | string(REGEX REPLACE "^(.*\n)?Repository Root: ([^\n]+).*" |
| 136 | "\\2" ${prefix}_WC_ROOT "${${prefix}_WC_INFO}") |
| 137 | string(REGEX REPLACE "^(.*\n)?Last Changed Author: ([^\n]+).*" |
| 138 | "\\2" ${prefix}_WC_LAST_CHANGED_AUTHOR "${${prefix}_WC_INFO}") |
| 139 | string(REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*" |
| 140 | "\\2" ${prefix}_WC_LAST_CHANGED_REV "${${prefix}_WC_INFO}") |
| 141 | string(REGEX REPLACE "^(.*\n)?Last Changed Date: ([^\n]+).*" |
| 142 | "\\2" ${prefix}_WC_LAST_CHANGED_DATE "${${prefix}_WC_INFO}") |
| 143 | endif(${git_svn_info_result} EQUAL 0) |
| 144 | endif(NOT "${git_config_output}" STREQUAL "") |
| 145 | |
| 146 | # If there is no 'remote.origin', default to "NA" value and print a warning message. |
| 147 | if(NOT ${prefix}_WC_URL) |
| 148 | message(WARNING "No remote origin set for git repository: ${dir}" ) |
| 149 | set( ${prefix}_WC_URL "NA" ) |
| 150 | else() |
| 151 | set(${prefix}_WC_ROOT ${${prefix}_WC_URL}) |
| 152 | endif() |
| 153 | |
| 154 | endmacro(GIT_WC_INFO) |
| 155 | endif(GIT_EXECUTABLE) |
| 156 | |
| 157 | # Handle the QUIETLY and REQUIRED arguments and set GIT_FOUND to TRUE if |
| 158 | # all listed variables are TRUE |
| 159 | |
| 160 | include(FindPackageHandleStandardArgs) |
| 161 | find_package_handle_standard_args(Git DEFAULT_MSG GIT_EXECUTABLE) |
| 162 | |
| 163 | |