BASIS  version 1.2.3 (revision 2104)
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)
00025 find_package (Git)
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 
00051   if (Subversion_SVN_EXECUTABLE)
00052     # remove "file://" from URL
00053     string (REGEX REPLACE "file://" "" TMP "${URL}")
00054 
00055     # retrieve SVN info
00056     execute_process (
00057       COMMAND         "${Subversion_SVN_EXECUTABLE}" info "${TMP}"
00058       OUTPUT_VARIABLE OUT
00059       ERROR_QUIET
00060       OUTPUT_STRIP_TRAILING_WHITESPACE
00061     )
00062 
00063     if (BASIS_DEBUG)
00064       message ("** basis_svn_get_revision()")
00065       message ("**   svn info: ${OUT}")
00066     endif ()
00067 
00068     # extract revision
00069     string (REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*" "\\2" OUT "${OUT}")
00070 
00071     if (OUT STREQUAL "")
00072       set (OUT "0")
00073     endif ()
00074   endif ()
00075 
00076   # return
00077   set ("${REV}" "${OUT}" PARENT_SCOPE)
00078 endfunction ()
00079 
00080 # ----------------------------------------------------------------------------
00081 ## @brief Get revision number when directory or file was last changed.
00082 #
00083 # @param [in]  URL  Absolute path of directory or file. May also be a URL to the
00084 #                   directory or file in the repository. A leading "file://" is
00085 #                   automatically removed such that the svn command treats it as a
00086 #                   local path.
00087 # @param [out] REV  Revision number when URL was last modified. If URL is not
00088 #                   under Subversion control or Subversion_SVN_EXECUTABLE is invalid,
00089 #                   "0" is returned.
00090 #
00091 # @returns Sets @p REV to revision number at which the working copy/repository
00092 #          specified by the URL @p URL was last modified.
00093 function (basis_svn_get_last_changed_revision URL REV)
00094   set (OUT "0")
00095 
00096   if (Subversion_SVN_EXECUTABLE)
00097     # remove "file://" from URL
00098     string (REGEX REPLACE "file://" "" TMP "${URL}")
00099 
00100     # retrieve SVN info
00101     execute_process (
00102       COMMAND         "${Subversion_SVN_EXECUTABLE}" info "${TMP}"
00103       OUTPUT_VARIABLE OUT
00104       ERROR_QUIET
00105       OUTPUT_STRIP_TRAILING_WHITESPACE
00106     )
00107 
00108     # extract last changed revision
00109     string (REGEX REPLACE "^(.*\n)?Last Changed Rev: ([^\n]+).*" "\\2" OUT "${OUT}")
00110 
00111     if (OUT STREQUAL "")
00112       set (OUT "0")
00113     endif ()
00114   endif ()
00115 
00116   # return
00117   set ("${REV}" "${OUT}" PARENT_SCOPE)
00118 endfunction ()
00119 
00120 # ----------------------------------------------------------------------------
00121 ## @brief Get status of revision controlled file.
00122 #
00123 # @param [in]  URL    Absolute path of directory or file. May also be a URL to
00124 #                     the directory or file in the repository.
00125 #                     A leading "file://" will be removed such that the svn
00126 #                     command treats it as a local path.
00127 # @param [out] STATUS The status of URL as returned by 'svn status'.
00128 #                     If the local directory or file is unmodified, an
00129 #                     empty string is returned. An empty string is also
00130 #                     returned when Subversion_SVN_EXECUTABLE is invalid.
00131 #
00132 # @returns Sets @p STATUS to the output of the <tt>svn info</tt> command.
00133 function (basis_svn_status URL STATUS)
00134   if (Subversion_SVN_EXECUTABLE)
00135     # remove "file://" from URL
00136     string (REGEX REPLACE "file://" "" TMP "${URL}")
00137 
00138     # retrieve SVN status of URL
00139     execute_process (
00140       COMMAND         "${Subversion_SVN_EXECUTABLE}" status "${TMP}"
00141       OUTPUT_VARIABLE OUT
00142       ERROR_QUIET
00143     )
00144 
00145     # return
00146     set ("${STATUS}" "${OUT}" PARENT_SCOPE)
00147   else ()
00148     set ("${STATUS}" "" PARENT_SCOPE)
00149   endif ()
00150 endfunction ()
00151 
00152 # ============================================================================
00153 # Git
00154 # ============================================================================
00155 
00156 
00157 
00158 ## @}
00159 # end of Doxygen group