1
0
Fork 0

Newer FindGit

This commit is contained in:
James Turner 2020-08-21 12:12:14 +01:00
parent 47ff3e9f67
commit eb55f25997
2 changed files with 79 additions and 28 deletions

View file

@ -1,24 +1,34 @@
# The module defines the following variables: # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# GIT_EXECUTABLE - path to git command line client # file Copyright.txt or https://cmake.org/licensing for details.
# GIT_FOUND - true if the command line client was found
# Example usage:
# find_package(Git)
# if(GIT_FOUND)
# message("git found: ${GIT_EXECUTABLE}")
# endif()
#============================================================================= #[=======================================================================[.rst:
# Copyright 2010 Kitware, Inc. FindGit
# -------
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details. The module defines the following ``IMPORTED`` targets (when
# :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``Git::Git``
# See the License for more information. Executable of the Git command-line client.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full The module defines the following variables:
# License text for the above reference.)
``GIT_EXECUTABLE``
Path to Git command-line client.
``Git_FOUND``, ``GIT_FOUND``
True if the Git command-line client was found.
``GIT_VERSION_STRING``
The version of Git found.
Example usage:
.. code-block:: cmake
find_package(Git)
if(Git_FOUND)
message("Git found: ${GIT_EXECUTABLE}")
endif()
#]=======================================================================]
# Look for 'git' or 'eg' (easy git) # Look for 'git' or 'eg' (easy git)
# #
@ -27,20 +37,61 @@ set(git_names git eg)
# Prefer .cmd variants on Windows unless running in a Makefile # Prefer .cmd variants on Windows unless running in a Makefile
# in the MSYS shell. # in the MSYS shell.
# #
if(WIN32) if(CMAKE_HOST_WIN32)
if(NOT CMAKE_GENERATOR MATCHES "MSYS") if(NOT CMAKE_GENERATOR MATCHES "MSYS")
set(git_names git.cmd git eg.cmd eg) set(git_names git.cmd git eg.cmd eg)
# GitHub search path for Windows
file(GLOB github_path
"$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd"
"$ENV{LOCALAPPDATA}/Github/PortableGit*/bin"
)
# SourceTree search path for Windows
set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
endif() endif()
endif() endif()
# First search the PATH and specific locations.
find_program(GIT_EXECUTABLE find_program(GIT_EXECUTABLE
NAMES ${git_names} NAMES ${git_names}
DOC "git command line client" PATHS ${github_path} ${_git_sourcetree_path}
DOC "Git command line client"
) )
if(CMAKE_HOST_WIN32)
# Now look for installations in Git/ directories under typical installation
# prefixes on Windows. Exclude PATH from this search because VS 2017's
# command prompt happens to have a PATH entry with a Git/ subdirectory
# containing a minimal git not meant for general use.
find_program(GIT_EXECUTABLE
NAMES ${git_names}
PATH_SUFFIXES Git/cmd Git/bin
NO_SYSTEM_ENVIRONMENT_PATH
DOC "Git command line client"
)
endif()
mark_as_advanced(GIT_EXECUTABLE) mark_as_advanced(GIT_EXECUTABLE)
# Handle the QUIETLY and REQUIRED arguments and set GIT_FOUND to TRUE if unset(git_names)
# all listed variables are TRUE unset(_git_sourcetree_path)
include(FindPackageHandleStandardArgs) if(GIT_EXECUTABLE)
find_package_handle_standard_args(Git DEFAULT_MSG GIT_EXECUTABLE) execute_process(COMMAND ${GIT_EXECUTABLE} --version
OUTPUT_VARIABLE git_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (git_version MATCHES "^git version [0-9]")
string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
endif()
unset(git_version)
get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE)
if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git)
add_executable(Git::Git IMPORTED)
set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
endif()
endif()
find_package_handle_standard_args(Git
REQUIRED_VARS GIT_EXECUTABLE
VERSION_VAR GIT_VERSION_STRING)

View file

@ -8,8 +8,8 @@ else()
endif() endif()
find_package(Git) find_package(Git)
if (GIT_FOUND) if (Git_FOUND)
execute_process(COMMAND git --git-dir ${SRC}/.git rev-parse HEAD execute_process(COMMAND ${GIT_EXECUTABLE} --git-dir ${SRC}/.git rev-parse HEAD
OUTPUT_VARIABLE REVISION OUTPUT_VARIABLE REVISION
OUTPUT_STRIP_TRAILING_WHITESPACE) OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Git revision is ${REVISION}") message(STATUS "Git revision is ${REVISION}")