|
| 1 | +# ---------------------------------------------------------------------------- # |
| 2 | +# |
| 3 | +# Copyright (c) 2020 C++ Modern Framework |
| 4 | +# |
| 5 | +# https://github.com/cppmf/GitInfo.cmake |
| 6 | +# |
| 7 | +# ---------------------------------------------------------------------------- # |
| 8 | + |
| 9 | +# Modified June 2024 - Andy Maloney <[email protected]> |
| 10 | +# - remove some vars we aren't using |
| 11 | +# - fix spelling/grammar |
| 12 | + |
| 13 | +# ---------------------------------------------------------------------------- # |
| 14 | +# |
| 15 | +# Following variables will be set when calling GitInfo |
| 16 | +# |
| 17 | +# GIT_DIR: path to the project .git directory |
| 18 | +# GIT_IS_DIRTY: whether or not the working tree is dirty |
| 19 | +# GIT_HEAD_BRANCH : name of the branch associated to HEAD |
| 20 | +# GIT_REVISION_HASH: current HEAD sha hash |
| 21 | +# GIT_REVISION: short version of GIT_REVISION_HASH |
| 22 | +# GIT_REVISION_NAME: name associated to GIT_REVISION_HASH |
| 23 | +# GIT_REMOTE_ORIGIN_URL : origin remote url |
| 24 | +# GIT_LATEST_TAG_LONG : most recent tag of the current branch |
| 25 | +# GIT_LATEST_TAG : most recent tagname of the current branch |
| 26 | +# |
| 27 | +# ---------------------------------------------------------------------------- # |
| 28 | + |
| 29 | +# This is the main function to call in project CMakeLists.txt |
| 30 | +# source should point to the root project directory |
| 31 | +function(GitInfo source) |
| 32 | + |
| 33 | + # Check is source is a valid path |
| 34 | + if(NOT EXISTS ${source}) |
| 35 | + message(FATAL_ERROR "'${source}' is not a valid path") |
| 36 | + endif() |
| 37 | + |
| 38 | + # Define the possible location of the .git directory |
| 39 | + set(GIT_DIR "${source}/.git") |
| 40 | + |
| 41 | + # Check if .git folder exist |
| 42 | + if(EXISTS ${GIT_DIR}) |
| 43 | + |
| 44 | + # |
| 45 | + set(GIT_DIR "${GIT_DIR}" CACHE PATH "Project .git directory") |
| 46 | + |
| 47 | + # Check if git is installed |
| 48 | + if(NOT GIT_FOUND) |
| 49 | + find_package(Git QUIET) |
| 50 | + endif() |
| 51 | + if(NOT GIT_FOUND) |
| 52 | + message(AUTHOR_WARNING "Git not found, cannot get git informations") |
| 53 | + return() |
| 54 | + endif() |
| 55 | + |
| 56 | + # whether or not the working tree is dirty |
| 57 | + execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain |
| 58 | + WORKING_DIRECTORY ${source} |
| 59 | + RESULT_VARIABLE exit_code |
| 60 | + OUTPUT_VARIABLE GIT_IS_DIRTY OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 61 | + # the working tree is dirty when the error code is different from 0 |
| 62 | + # or if the output is not empty |
| 63 | + if(NOT exit_code EQUAL 0 OR NOT ${GIT_IS_DIRTY} STREQUAL "") |
| 64 | + unset(GIT_IS_DIRTY) |
| 65 | + set(GIT_IS_DIRTY ON CACHE BOOL "Indicate if current branch is dirty") |
| 66 | + else() |
| 67 | + unset(GIT_IS_DIRTY) |
| 68 | + set(GIT_IS_DIRTY OFF CACHE BOOL "Indicate if current branch is dirty") |
| 69 | + endif() |
| 70 | + |
| 71 | + # name of the branch associated to HEAD |
| 72 | + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD |
| 73 | + WORKING_DIRECTORY ${source} |
| 74 | + OUTPUT_VARIABLE GIT_HEAD_BRANCH OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 75 | + set(GIT_HEAD_BRANCH "${GIT_HEAD_BRANCH}" CACHE INTERNAL "name of the branch associated to HEAD") |
| 76 | + |
| 77 | + # git revision full hash |
| 78 | + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%H" HEAD |
| 79 | + WORKING_DIRECTORY ${source} |
| 80 | + OUTPUT_VARIABLE GIT_REVISION_HASH OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 81 | + set(GIT_REVISION_HASH "${GIT_REVISION_HASH}" CACHE INTERNAL "git revision full hash") |
| 82 | + |
| 83 | + # short version of git revision |
| 84 | + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%h" HEAD |
| 85 | + WORKING_DIRECTORY ${source} |
| 86 | + OUTPUT_VARIABLE GIT_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 87 | + set(GIT_REVISION "${GIT_REVISION}" CACHE INTERNAL "short version of git revision") |
| 88 | + |
| 89 | + # short version of git revision name |
| 90 | + execute_process(COMMAND ${GIT_EXECUTABLE} show -s "--format=%s" HEAD |
| 91 | + WORKING_DIRECTORY ${source} |
| 92 | + OUTPUT_VARIABLE GIT_REVISION_NAME OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 93 | + set(GIT_REVISION_NAME "${GIT_REVISION_NAME}" CACHE INTERNAL "short version of git revision name") |
| 94 | + |
| 95 | + # origin remote url |
| 96 | + execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url |
| 97 | + WORKING_DIRECTORY ${source} |
| 98 | + OUTPUT_VARIABLE GIT_REMOTE_ORIGIN_URL OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 99 | + set(GIT_REMOTE_ORIGIN_URL "${GIT_REMOTE_ORIGIN_URL}" CACHE INTERNAL "git origin remote url") |
| 100 | + |
| 101 | + # most recent tag of the current branch |
| 102 | + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 HEAD |
| 103 | + WORKING_DIRECTORY ${source} |
| 104 | + OUTPUT_VARIABLE GIT_LATEST_TAG_LONG OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 105 | + set(GIT_LATEST_TAG_LONG "${GIT_LATEST_TAG_LONG}" CACHE INTERNAL "git most recent tag of the current branch") |
| 106 | + |
| 107 | + # most recent tagname of the current branch |
| 108 | + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags HEAD |
| 109 | + WORKING_DIRECTORY ${source} |
| 110 | + OUTPUT_VARIABLE GIT_LATEST_TAG OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 111 | + set(GIT_LATEST_TAG "${GIT_LATEST_TAG}" CACHE INTERNAL "git most recent tagname of the current branch") |
| 112 | + |
| 113 | + endif() |
| 114 | + |
| 115 | +endfunction() |
| 116 | + |
| 117 | + |
| 118 | +# Report git information |
| 119 | +function(GitInfoReport) |
| 120 | + message(STATUS "") |
| 121 | + message(STATUS "----------------------------------------------------") |
| 122 | + message(STATUS " GitInfo.cmake") |
| 123 | + message(STATUS "") |
| 124 | + message(STATUS "GIT_DIR : ${GIT_DIR}") |
| 125 | + message(STATUS "") |
| 126 | + message(STATUS "GIT_IS_DIRTY : ${GIT_IS_DIRTY}") |
| 127 | + message(STATUS "GIT_HEAD_BRANCH : ${GIT_HEAD_BRANCH}") |
| 128 | + message(STATUS "GIT_REVISION : ${GIT_REVISION}") |
| 129 | + message(STATUS "GIT_REVISION_HASH : ${GIT_REVISION_HASH}") |
| 130 | + message(STATUS "GIT_REVISION_NAME : ${GIT_REVISION_NAME}") |
| 131 | + message(STATUS "") |
| 132 | + message(STATUS "GIT_REMOTE_ORIGIN_URL : ${GIT_REMOTE_ORIGIN_URL}") |
| 133 | + message(STATUS "GIT_LATEST_TAG_LONG : ${GIT_LATEST_TAG_LONG}") |
| 134 | + message(STATUS "GIT_LATEST_TAG : ${GIT_LATEST_TAG}") |
| 135 | + message(STATUS "") |
| 136 | + message(STATUS "----------------------------------------------------") |
| 137 | + message(STATUS "") |
| 138 | +endfunction() |
0 commit comments