AddGerritCommitHook.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Ceres Solver - A fast non-linear least squares minimizer
  2. # Copyright 2023 Google Inc. All rights reserved.
  3. # http://ceres-solver.org/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. # * Neither the name of Google Inc. nor the names of its contributors may be
  14. # used to endorse or promote products derived from this software without
  15. # specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # Authors: keir@google.com (Keir Mierle)
  30. # alexs.mac@gmail.com (Alex Stewart)
  31. # Set up the git hook to make Gerrit Change-Id: lines in commit messages.
  32. function(ADD_GERRIT_COMMIT_HOOK SOURCE_DIR BINARY_DIR)
  33. if (NOT (EXISTS ${SOURCE_DIR} AND IS_DIRECTORY ${SOURCE_DIR}))
  34. message(FATAL_ERROR "Specified SOURCE_DIR: ${SOURCE_DIR} does not exist, "
  35. "or is not a directory, cannot add Gerrit commit hook.")
  36. endif()
  37. if (NOT (EXISTS ${BINARY_DIR} AND IS_DIRECTORY ${BINARY_DIR}))
  38. message(FATAL_ERROR "Specified BINARY_DIR: ${BINARY_DIR} does not exist, "
  39. "or is not a directory, cannot add Gerrit commit hook.")
  40. endif()
  41. unset (LOCAL_GIT_DIRECTORY)
  42. if (EXISTS ${SOURCE_DIR}/.git)
  43. if (IS_DIRECTORY ${SOURCE_DIR}/.git)
  44. # .git directory can be found on Unix based system, or on Windows with
  45. # Git Bash (shipped with msysgit).
  46. set (LOCAL_GIT_DIRECTORY ${SOURCE_DIR}/.git)
  47. else(IS_DIRECTORY ${SOURCE_DIR}/.git)
  48. # .git is a file, this means Ceres is a git submodule of another project
  49. # and our .git file contains the path to the git directory which manages
  50. # Ceres, so we should add the gerrit hook there.
  51. file(READ ${SOURCE_DIR}/.git GIT_SUBMODULE_FILE_CONTENTS)
  52. # Strip any trailing newline characters, s/t we get a valid path.
  53. string(REGEX REPLACE "gitdir:[ ]*([^$].*)[\n].*" "${SOURCE_DIR}/\\1"
  54. GIT_SUBMODULE_GIT_DIRECTORY_PATH "${GIT_SUBMODULE_FILE_CONTENTS}")
  55. get_filename_component(GIT_SUBMODULE_GIT_DIRECTORY_PATH
  56. "${GIT_SUBMODULE_GIT_DIRECTORY_PATH}" ABSOLUTE)
  57. if (EXISTS ${GIT_SUBMODULE_GIT_DIRECTORY_PATH}
  58. AND IS_DIRECTORY ${GIT_SUBMODULE_GIT_DIRECTORY_PATH})
  59. set(LOCAL_GIT_DIRECTORY "${GIT_SUBMODULE_GIT_DIRECTORY_PATH}")
  60. endif()
  61. endif()
  62. else (EXISTS ${SOURCE_DIR}/.git)
  63. # TODO(keir) Add proper Windows support.
  64. endif (EXISTS ${SOURCE_DIR}/.git)
  65. if (EXISTS ${LOCAL_GIT_DIRECTORY})
  66. if (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg)
  67. message(STATUS "Detected Ceres being used as a git submodule, adding "
  68. "commit hook for Gerrit to: ${LOCAL_GIT_DIRECTORY}")
  69. # Download the hook only if it is not already present.
  70. file(DOWNLOAD https://ceres-solver-review.googlesource.com/tools/hooks/commit-msg
  71. ${BINARY_DIR}/commit-msg)
  72. # Make the downloaded file executable, since it is not by default.
  73. file(COPY ${BINARY_DIR}/commit-msg
  74. DESTINATION ${LOCAL_GIT_DIRECTORY}/hooks/
  75. FILE_PERMISSIONS
  76. OWNER_READ OWNER_WRITE OWNER_EXECUTE
  77. GROUP_READ GROUP_WRITE GROUP_EXECUTE
  78. WORLD_READ WORLD_EXECUTE)
  79. endif (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg)
  80. endif (EXISTS ${LOCAL_GIT_DIRECTORY})
  81. endfunction()