BASIS  r3148
RevisionTools.cmake
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  RevisionTools.cmake
00003 # @brief CMake functions and macros related to revision control systems.
00004 #
00005 # Copyright (c) 2011, 2012 University of Pennsylvania. All rights reserved.<br />
00006 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00007 #
00008 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00009 #
00010 # @ingroup CMakeTools
00011 ##############################################################################
00012 
00013 if (__BASIS_REVISIONTOOLS_INCLUDED)
00014   return ()
00015 else ()
00016   set (__BASIS_REVISIONTOOLS_INCLUDED TRUE)
00017 endif ()
00018 
00019 
00020 # ============================================================================
00021 # required commands
00022 # ============================================================================
00023 
00024 find_package (Subversion QUIET)
00025 find_package (Git        QUIET)
00026 
00027 
00028 ## @addtogroup CMakeUtilities
00029 #  @{
00030 
00031 
00032 # ============================================================================
00033 # Subversion
00034 # ============================================================================
00035 
00036 # ----------------------------------------------------------------------------
00037 ## @brief Get current revision of file or directory.
00038 #
00039 # @param [in]  URL  Absolute path of directory or file. May also be a URL to the
00040 #                   directory or file in the repository. A leading "file://" is
00041 #                   automatically removed such that the svn command treats it as a
00042 #                   local path.
00043 # @param [out] REV  The revision number of URL. If URL is not under revision
00044 #                   control or Subversion_SVN_EXECUTABLE is invalid, "0" is returned.
00045 #
00046 # @returns Sets @p REV to the revision of the working copy/repository
00047 #          at URL @p URL.
00048 function (basis_svn_get_revision URL REV)
00049   set (OUT 0)
00050   if (Subversion_SVN_EXECUTABLE)
00051     # remove "file://" from URL
00052     string (REGEX REPLACE "file://" "" TMP "${URL}")
00053     # retrieve SVN info
00054     execute_process (
00055       COMMAND         "${Subversion_SVN_EXECUTABLE}" info "${TMP}"
00056       OUTPUT_VARIABLE OUT
00057       ERROR_QUIET
00058       OUTPUT_STRIP_TRAILING_WHITESPACE
00059     )
00060     if (BASIS_DEBUG)
00061       message ("** basis_svn_get_revision()")
00062       message ("**   svn info: ${OUT}")
00063     endif ()
00064     # extract revision
00065     if (OUT MATCHES "^(.*\n)?Revision: ([^\n]+).*" AND NOT CMAKE_MATCH_2 STREQUAL "")
00066       set (OUT "${CMAKE_MATCH_2}")
00067     else ()
00068       set (OUT 0)
00069     endif ()
00070   endif ()
00071   # return
00072   set ("${REV}" "${OUT}" PARENT_SCOPE)
00073 endfunction ()
00074 
00075 # ----------------------------------------------------------------------------
00076 ## @brief Get revision number when directory or file was last changed.
00077 #
00078 # @param [in]  URL  Absolute path of directory or file. May also be a URL to the
00079 #                   directory or file in the repository. A leading "file://" is
00080 #                   automatically removed such that the svn command treats it as a
00081 #                   local path.
00082 # @param [out] REV  Revision number when URL was last modified. If URL is not
00083 #                   under Subversion control or Subversion_SVN_EXECUTABLE is invalid,
00084 #                   "0" is returned.
00085 #
00086 # @returns Sets @p REV to revision number at which the working copy/repository
00087 #          specified by the URL @p URL was last modified.
00088 function (basis_svn_get_last_changed_revision URL REV)
00089   set (OUT 0)
00090   if (Subversion_SVN_EXECUTABLE)
00091     # remove "file://" from URL
00092     string (REGEX REPLACE "file://" "" TMP "${URL}")
00093     # retrieve SVN info
00094     execute_process (
00095       COMMAND         "${Subversion_SVN_EXECUTABLE}" info "${TMP}"
00096       OUTPUT_VARIABLE OUT
00097       ERROR_QUIET
00098       OUTPUT_STRIP_TRAILING_WHITESPACE
00099     )
00100     if (BASIS_DEBUG)
00101       message ("** basis_svn_get_revision()")
00102       message ("**   svn info: ${OUT}")
00103     endif ()
00104     # extract last changed revision
00105     if (OUT MATCHES "^(.*\n)?Last Changed Rev: ([^\n]+).*" AND NOT CMAKE_MATCH_2 STREQUAL "")
00106       set (OUT "${CMAKE_MATCH_2}")
00107     else ()
00108       set (OUT 0)
00109     endif ()
00110   endif ()
00111   # return
00112   set ("${REV}" "${OUT}" PARENT_SCOPE)
00113 endfunction ()
00114 
00115 # ----------------------------------------------------------------------------
00116 ## @brief Get status of revision controlled file.
00117 #
00118 # @param [in]  URL    Absolute path of directory or file. May also be a URL to
00119 #                     the directory or file in the repository.
00120 #                     A leading "file://" will be removed such that the svn
00121 #                     command treats it as a local path.
00122 # @param [out] STATUS The status of URL as returned by 'svn status'.
00123 #                     If the local directory or file is unmodified, an
00124 #                     empty string is returned. An empty string is also
00125 #                     returned when Subversion_SVN_EXECUTABLE is invalid.
00126 #
00127 # @returns Sets @p STATUS to the output of the <tt>svn info</tt> command.
00128 function (basis_svn_status URL STATUS)
00129   if (Subversion_SVN_EXECUTABLE)
00130     # remove "file://" from URL
00131     string (REGEX REPLACE "file://" "" TMP "${URL}")
00132 
00133     # retrieve SVN status of URL
00134     execute_process (
00135       COMMAND         "${Subversion_SVN_EXECUTABLE}" status "${TMP}"
00136       OUTPUT_VARIABLE OUT
00137       ERROR_QUIET
00138     )
00139 
00140     # return
00141     set ("${STATUS}" "${OUT}" PARENT_SCOPE)
00142   else ()
00143     set ("${STATUS}" "" PARENT_SCOPE)
00144   endif ()
00145 endfunction ()
00146 
00147 # ============================================================================
00148 # Git
00149 # ============================================================================
00150 
00151 
00152 
00153 ## @}
00154 # end of Doxygen group