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