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