|
| 1 | +# - Extract information from a Mercurial repository |
| 2 | +# The module defines the following variables: |
| 3 | +# MERCURIAL_EXECUTABLE - path to mercurial executable |
| 4 | +# MERCURIAL_VERSION - mercurial version |
| 5 | +# MERCURIAL_FOUND - true if mercurial was found |
| 6 | +# If the mercurial executable is found the macro |
| 7 | +# MERCURIAL_HG_INFO(<dir> <var-prefix>) |
| 8 | +# is defined to extract information of a mercurial repository at |
| 9 | +# a given location. The macro defines the following variables: |
| 10 | +# <var-prefix>_HG_ID - global revision id of the working copy (at <dir>) |
| 11 | +# <var-prefix>_HG_CHANGESET - changeset id of the working copy |
| 12 | +# <var-prefix>_HG_AUTHOR - commit author of this changeset |
| 13 | +# <var-prefix>_HG_DATE - commit date of this changeset |
| 14 | +# <var-prefix>_HG_TAGS - tags belonging to this changeset |
| 15 | +# <var-prefix>_HG_BRANCH - (non-default) branch name of this changeset |
| 16 | +# <var-prefix>_HG_SUMMARY - commit message summary of this changeset |
| 17 | +# Example usage: |
| 18 | +# FIND_PACKAGE(Mercurial) |
| 19 | +# IF(MERCURIAL_FOUND) |
| 20 | +# MERCURIAL_HG_INFO(${PROJECT_SOURCE_DIR} Project) |
| 21 | +# MESSAGE("Current revision is ${Project_HG_ID}") |
| 22 | +# ENDIF(MERCURIAL_FOUND) |
| 23 | + |
| 24 | +# Copyright (C) 2008 Peter Colberg |
| 25 | +# |
| 26 | +# This file was derived from FindSubversion.cmake shipped with CMake 2.4.7. |
| 27 | +# |
| 28 | +# Copyright (c) 2006, Tristan Carel |
| 29 | +# All rights reserved. |
| 30 | +# Redistribution and use in source and binary forms, with or without |
| 31 | +# modification, are permitted provided that the following conditions are met: |
| 32 | +# |
| 33 | +# * Redistributions of source code must retain the above copyright |
| 34 | +# notice, this list of conditions and the following disclaimer. |
| 35 | +# * Redistributions in binary form must reproduce the above copyright |
| 36 | +# notice, this list of conditions and the following disclaimer in the |
| 37 | +# documentation and/or other materials provided with the distribution. |
| 38 | +# * Neither the name of the University of California, Berkeley nor the |
| 39 | +# names of its contributors may be used to endorse or promote products |
| 40 | +# derived from this software without specific prior written permission. |
| 41 | +# |
| 42 | +# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
| 43 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 44 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 45 | +# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
| 46 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 47 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 48 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 49 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 50 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 51 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 52 | + |
| 53 | + |
| 54 | +FIND_PROGRAM(MERCURIAL_EXECUTABLE hg |
| 55 | + DOC "Mercurial Distributed SCM executable") |
| 56 | +MARK_AS_ADVANCED(MERCURIAL_EXECUTABLE) |
| 57 | + |
| 58 | +IF(MERCURIAL_EXECUTABLE) |
| 59 | + SET(MERCURIAL_FOUND TRUE) |
| 60 | + |
| 61 | + MACRO(MERCURIAL_COMMAND dir command) |
| 62 | + EXECUTE_PROCESS(COMMAND ${MERCURIAL_EXECUTABLE} ${command} ${ARGN} |
| 63 | + WORKING_DIRECTORY ${dir} |
| 64 | + OUTPUT_VARIABLE MERCURIAL_${command}_OUTPUT |
| 65 | + ERROR_VARIABLE MERCURIAL_${command}_ERROR |
| 66 | + RESULT_VARIABLE MERCURIAL_${command}_RESULT |
| 67 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 68 | + |
| 69 | + IF(NOT ${MERCURIAL_${command}_RESULT} EQUAL 0) |
| 70 | + SET(cmdline "${MERCURIAL_EXECUTABLE} ${command}") |
| 71 | + FOREACH(arg ${ARGN}) |
| 72 | + SET(cmdline "${cmdline} ${arg}") |
| 73 | + ENDFOREACH(arg ${ARGN}) |
| 74 | + MESSAGE(SEND_ERROR "Command \"${cmdline}\" failed with output:\n${MERCURIAL_${command}_ERROR}") |
| 75 | + |
| 76 | + SET(MERCURIAL_${command}_OUTPUT) |
| 77 | + ENDIF(NOT ${MERCURIAL_${command}_RESULT} EQUAL 0) |
| 78 | + |
| 79 | + ENDMACRO(MERCURIAL_COMMAND dir command) |
| 80 | + |
| 81 | + |
| 82 | + MACRO(MERCURIAL_HG_INFO dir prefix) |
| 83 | + IF(IS_DIRECTORY "${dir}") |
| 84 | + MERCURIAL_COMMAND(${dir} id -i) |
| 85 | + |
| 86 | + IF(MERCURIAL_id_OUTPUT) |
| 87 | + # global revision id of the working copy |
| 88 | + SET(${prefix}_HG_ID "${MERCURIAL_id_OUTPUT}") |
| 89 | + |
| 90 | + # changeset id of the working copy |
| 91 | + STRING(REGEX REPLACE "^([0-9a-f]+).*" |
| 92 | + "\\1" ${prefix}_HG_CHANGESET "${MERCURIAL_id_OUTPUT}") |
| 93 | + |
| 94 | + MERCURIAL_COMMAND(${dir} log -r ${${prefix}_HG_CHANGESET}) |
| 95 | + |
| 96 | + STRING(REGEX REPLACE ";" "\\\\;" |
| 97 | + MERCURIAL_log_OUTPUT "${MERCURIAL_log_OUTPUT}") |
| 98 | + STRING(REGEX REPLACE "\n" ";" |
| 99 | + MERCURIAL_log_OUTPUT "${MERCURIAL_log_OUTPUT}") |
| 100 | + |
| 101 | + FOREACH(line ${MERCURIAL_log_OUTPUT}) |
| 102 | + # commit author of this changeset |
| 103 | + IF(line MATCHES "^user:") |
| 104 | + STRING(REGEX REPLACE "^user:[ ]+(.+)" |
| 105 | + "\\1" ${prefix}_HG_AUTHOR "${line}") |
| 106 | + ENDIF(line MATCHES "^user:") |
| 107 | + |
| 108 | + # commit date of this changeset |
| 109 | + IF(line MATCHES "^date:") |
| 110 | + STRING(REGEX REPLACE "^date:[ ]+(.+)" |
| 111 | + "\\1" ${prefix}_HG_DATE "${line}") |
| 112 | + ENDIF(line MATCHES "^date:") |
| 113 | + |
| 114 | + # tags belonging to this changeset |
| 115 | + IF(line MATCHES "^tag:") |
| 116 | + STRING(REGEX REPLACE "^tag:[ ]+(.+)" |
| 117 | + "\\1" tag "${line}") |
| 118 | + STRING(REGEX REPLACE ";" "\\\\;" tag "${tag}") |
| 119 | + LIST(APPEND ${prefix}_HG_TAGS "${tag}") |
| 120 | + ENDIF(line MATCHES "^tag:") |
| 121 | + |
| 122 | + # (non-default) branch name of this changeset |
| 123 | + IF(line MATCHES "^branch:") |
| 124 | + STRING(REGEX REPLACE "^branch:[ ]+(.+)" |
| 125 | + "\\1" ${prefix}_HG_BRANCH "${line}") |
| 126 | + ENDIF(line MATCHES "^branch:") |
| 127 | + |
| 128 | + # commit message summary of this changeset |
| 129 | + IF(line MATCHES "^summary:") |
| 130 | + STRING(REGEX REPLACE "^summary:[ ]+(.+)" |
| 131 | + "\\1" ${prefix}_HG_SUMMARY "${line}") |
| 132 | + ENDIF(line MATCHES "^summary:") |
| 133 | + |
| 134 | + ENDFOREACH(line ${MERCURIAL_log_OUTPUT}) |
| 135 | + ENDIF(MERCURIAL_id_OUTPUT) |
| 136 | + |
| 137 | + ELSE(IS_DIRECTORY "${dir}") |
| 138 | + MESSAGE(SEND_ERROR "Invalid MERCURIAL_HG_INFO directory \"${dir}\"") |
| 139 | + ENDIF(IS_DIRECTORY "${dir}") |
| 140 | + |
| 141 | + ENDMACRO(MERCURIAL_HG_INFO dir prefix) |
| 142 | + |
| 143 | + |
| 144 | + # mercurial version |
| 145 | + MERCURIAL_COMMAND(${CMAKE_BINARY_DIR} version) |
| 146 | + |
| 147 | + STRING(REGEX REPLACE "^Mercurial Distributed SCM \\(version ([.0-9]+)\\).*" |
| 148 | + "\\1" MERCURIAL_VERSION "${MERCURIAL_version_OUTPUT}") |
| 149 | + |
| 150 | +ENDIF(MERCURIAL_EXECUTABLE) |
| 151 | + |
| 152 | +IF(NOT MERCURIAL_FOUND) |
| 153 | + IF(NOT MERCURIAL_FIND_QUIETLY) |
| 154 | + MESSAGE(STATUS "Mercurial was not found.") |
| 155 | + ELSE(NOT MERCURIAL_FIND_QUIETLY) |
| 156 | + IF(MERCURIAL_FIND_REQUIRED) |
| 157 | + MESSAGE(FATAL_ERROR "Mercurial was not found.") |
| 158 | + ENDIF(MERCURIAL_FIND_REQUIRED) |
| 159 | + ENDIF(NOT MERCURIAL_FIND_QUIETLY) |
| 160 | +ENDIF(NOT MERCURIAL_FOUND) |
0 commit comments