gitupdate.cmake.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. cmake_minimum_required(VERSION 3.5)
  4. function(do_fetch)
  5. message(VERBOSE "Fetching latest from the remote @git_remote_name@")
  6. execute_process(
  7. COMMAND "@git_EXECUTABLE@" --git-dir=.git fetch --tags --force "@git_remote_name@"
  8. WORKING_DIRECTORY "@work_dir@"
  9. COMMAND_ERROR_IS_FATAL LAST
  10. )
  11. endfunction()
  12. function(get_hash_for_ref ref out_var err_var)
  13. execute_process(
  14. COMMAND "@git_EXECUTABLE@" --git-dir=.git rev-parse "${ref}^0"
  15. WORKING_DIRECTORY "@work_dir@"
  16. RESULT_VARIABLE error_code
  17. OUTPUT_VARIABLE ref_hash
  18. ERROR_VARIABLE error_msg
  19. OUTPUT_STRIP_TRAILING_WHITESPACE
  20. )
  21. if(error_code)
  22. set(${out_var} "" PARENT_SCOPE)
  23. else()
  24. set(${out_var} "${ref_hash}" PARENT_SCOPE)
  25. endif()
  26. set(${err_var} "${error_msg}" PARENT_SCOPE)
  27. endfunction()
  28. get_hash_for_ref(HEAD head_sha error_msg)
  29. if(head_sha STREQUAL "")
  30. message(FATAL_ERROR "Failed to get the hash for HEAD:\n${error_msg}")
  31. endif()
  32. execute_process(
  33. COMMAND "@git_EXECUTABLE@" --git-dir=.git show-ref "@git_tag@"
  34. WORKING_DIRECTORY "@work_dir@"
  35. OUTPUT_VARIABLE show_ref_output
  36. )
  37. if(show_ref_output MATCHES "^[a-z0-9]+[ \\t]+refs/remotes/")
  38. # Given a full remote/branch-name and we know about it already. Since
  39. # branches can move around, we should always fetch, if permitted.
  40. if(can_fetch)
  41. do_fetch()
  42. endif()
  43. set(checkout_name "@git_tag@")
  44. elseif(show_ref_output MATCHES "^[a-z0-9]+[ \\t]+refs/tags/")
  45. # Given a tag name that we already know about. We don't know if the tag we
  46. # have matches the remote though (tags can move), so we should fetch. As a
  47. # special case to preserve backward compatibility, if we are already at the
  48. # same commit as the tag we hold locally, don't do a fetch and assume the tag
  49. # hasn't moved on the remote.
  50. # FIXME: We should provide an option to always fetch for this case
  51. get_hash_for_ref("@git_tag@" tag_sha error_msg)
  52. if(tag_sha STREQUAL head_sha)
  53. message(VERBOSE "Already at requested tag: ${tag_sha}")
  54. return()
  55. endif()
  56. if(can_fetch)
  57. do_fetch()
  58. endif()
  59. set(checkout_name "@git_tag@")
  60. elseif(show_ref_output MATCHES "^[a-z0-9]+[ \\t]+refs/heads/")
  61. # Given a branch name without any remote and we already have a branch by that
  62. # name. We might already have that branch checked out or it might be a
  63. # different branch. It isn't fully safe to use a bare branch name without the
  64. # remote, so do a fetch (if allowed) and replace the ref with one that
  65. # includes the remote.
  66. if(can_fetch)
  67. do_fetch()
  68. endif()
  69. set(checkout_name "@git_remote_name@/@git_tag@")
  70. else()
  71. get_hash_for_ref("@git_tag@" tag_sha error_msg)
  72. if(tag_sha STREQUAL head_sha)
  73. # Have the right commit checked out already
  74. message(VERBOSE "Already at requested ref: ${tag_sha}")
  75. return()
  76. elseif(tag_sha STREQUAL "")
  77. # We don't know about this ref yet, so we have no choice but to fetch.
  78. if(NOT can_fetch)
  79. message(FATAL_ERROR
  80. "Requested git ref \"@git_tag@\" is not present locally, and not "
  81. "allowed to contact remote due to UPDATE_DISCONNECTED setting."
  82. )
  83. endif()
  84. # We deliberately swallow any error message at the default log level
  85. # because it can be confusing for users to see a failed git command.
  86. # That failure is being handled here, so it isn't an error.
  87. if(NOT error_msg STREQUAL "")
  88. message(VERBOSE "${error_msg}")
  89. endif()
  90. do_fetch()
  91. set(checkout_name "@git_tag@")
  92. else()
  93. # We have the commit, so we know we were asked to find a commit hash
  94. # (otherwise it would have been handled further above), but we don't
  95. # have that commit checked out yet. We don't need to fetch from the remote.
  96. set(checkout_name "@git_tag@")
  97. if(NOT error_msg STREQUAL "")
  98. message(WARNING "${error_msg}")
  99. endif()
  100. endif()
  101. endif()
  102. set(git_update_strategy "@git_update_strategy@")
  103. if(git_update_strategy STREQUAL "")
  104. # Backward compatibility requires REBASE as the default behavior
  105. set(git_update_strategy REBASE)
  106. endif()
  107. if(git_update_strategy MATCHES "^REBASE(_CHECKOUT)?$")
  108. # Asked to potentially try to rebase first, maybe with fallback to checkout.
  109. # We can't if we aren't already on a branch and we shouldn't if that local
  110. # branch isn't tracking the one we want to checkout.
  111. execute_process(
  112. COMMAND "@git_EXECUTABLE@" --git-dir=.git symbolic-ref -q HEAD
  113. WORKING_DIRECTORY "@work_dir@"
  114. OUTPUT_VARIABLE current_branch
  115. OUTPUT_STRIP_TRAILING_WHITESPACE
  116. # Don't test for an error. If this isn't a branch, we get a non-zero error
  117. # code but empty output.
  118. )
  119. if(current_branch STREQUAL "")
  120. # Not on a branch, checkout is the only sensible option since any rebase
  121. # would always fail (and backward compatibility requires us to checkout in
  122. # this situation)
  123. set(git_update_strategy CHECKOUT)
  124. else()
  125. execute_process(
  126. COMMAND "@git_EXECUTABLE@" --git-dir=.git for-each-ref "--format=%(upstream:short)" "${current_branch}"
  127. WORKING_DIRECTORY "@work_dir@"
  128. OUTPUT_VARIABLE upstream_branch
  129. OUTPUT_STRIP_TRAILING_WHITESPACE
  130. COMMAND_ERROR_IS_FATAL ANY # There is no error if no upstream is set
  131. )
  132. if(NOT upstream_branch STREQUAL checkout_name)
  133. # Not safe to rebase when asked to checkout a different branch to the one
  134. # we are tracking. If we did rebase, we could end up with arbitrary
  135. # commits added to the ref we were asked to checkout if the current local
  136. # branch happens to be able to rebase onto the target branch. There would
  137. # be no error message and the user wouldn't know this was occurring.
  138. set(git_update_strategy CHECKOUT)
  139. endif()
  140. endif()
  141. elseif(NOT git_update_strategy STREQUAL "CHECKOUT")
  142. message(FATAL_ERROR "Unsupported git update strategy: ${git_update_strategy}")
  143. endif()
  144. # Check if stash is needed
  145. execute_process(
  146. COMMAND "@git_EXECUTABLE@" --git-dir=.git status --porcelain
  147. WORKING_DIRECTORY "@work_dir@"
  148. RESULT_VARIABLE error_code
  149. OUTPUT_VARIABLE repo_status
  150. )
  151. if(error_code)
  152. message(FATAL_ERROR "Failed to get the status")
  153. endif()
  154. string(LENGTH "${repo_status}" need_stash)
  155. # If not in clean state, stash changes in order to be able to perform a
  156. # rebase or checkout without losing those changes permanently
  157. if(need_stash)
  158. execute_process(
  159. COMMAND "@git_EXECUTABLE@" --git-dir=.git stash save @git_stash_save_options@
  160. WORKING_DIRECTORY "@work_dir@"
  161. COMMAND_ERROR_IS_FATAL ANY
  162. )
  163. endif()
  164. if(git_update_strategy STREQUAL "CHECKOUT")
  165. execute_process(
  166. COMMAND "@git_EXECUTABLE@" --git-dir=.git checkout "${checkout_name}"
  167. WORKING_DIRECTORY "@work_dir@"
  168. COMMAND_ERROR_IS_FATAL ANY
  169. )
  170. else()
  171. execute_process(
  172. COMMAND "@git_EXECUTABLE@" --git-dir=.git rebase "${checkout_name}"
  173. WORKING_DIRECTORY "@work_dir@"
  174. RESULT_VARIABLE error_code
  175. OUTPUT_VARIABLE rebase_output
  176. ERROR_VARIABLE rebase_output
  177. )
  178. if(error_code)
  179. # Rebase failed, undo the rebase attempt before continuing
  180. execute_process(
  181. COMMAND "@git_EXECUTABLE@" --git-dir=.git rebase --abort
  182. WORKING_DIRECTORY "@work_dir@"
  183. )
  184. if(NOT git_update_strategy STREQUAL "REBASE_CHECKOUT")
  185. # Not allowed to do a checkout as a fallback, so cannot proceed
  186. if(need_stash)
  187. execute_process(
  188. COMMAND "@git_EXECUTABLE@" --git-dir=.git stash pop --index --quiet
  189. WORKING_DIRECTORY "@work_dir@"
  190. )
  191. endif()
  192. message(FATAL_ERROR "\nFailed to rebase in: '@work_dir@'."
  193. "\nOutput from the attempted rebase follows:"
  194. "\n${rebase_output}"
  195. "\n\nYou will have to resolve the conflicts manually")
  196. endif()
  197. # Fall back to checkout. We create an annotated tag so that the user
  198. # can manually inspect the situation and revert if required.
  199. # We can't log the failed rebase output because MSVC sees it and
  200. # intervenes, causing the build to fail even though it completes.
  201. # Write it to a file instead.
  202. string(TIMESTAMP tag_timestamp "%Y%m%dT%H%M%S" UTC)
  203. set(tag_name _cmake_ExternalProject_moved_from_here_${tag_timestamp}Z)
  204. set(error_log_file ${CMAKE_CURRENT_LIST_DIR}/rebase_error_${tag_timestamp}Z.log)
  205. file(WRITE ${error_log_file} "${rebase_output}")
  206. message(WARNING "Rebase failed, output has been saved to ${error_log_file}"
  207. "\nFalling back to checkout, previous commit tagged as ${tag_name}")
  208. execute_process(
  209. COMMAND "@git_EXECUTABLE@" --git-dir=.git tag -a
  210. -m "ExternalProject attempting to move from here to ${checkout_name}"
  211. ${tag_name}
  212. WORKING_DIRECTORY "@work_dir@"
  213. COMMAND_ERROR_IS_FATAL ANY
  214. )
  215. execute_process(
  216. COMMAND "@git_EXECUTABLE@" --git-dir=.git checkout "${checkout_name}"
  217. WORKING_DIRECTORY "@work_dir@"
  218. COMMAND_ERROR_IS_FATAL ANY
  219. )
  220. endif()
  221. endif()
  222. if(need_stash)
  223. # Put back the stashed changes
  224. execute_process(
  225. COMMAND "@git_EXECUTABLE@" --git-dir=.git stash pop --index --quiet
  226. WORKING_DIRECTORY "@work_dir@"
  227. RESULT_VARIABLE error_code
  228. )
  229. if(error_code)
  230. # Stash pop --index failed: Try again dropping the index
  231. execute_process(
  232. COMMAND "@git_EXECUTABLE@" --git-dir=.git reset --hard --quiet
  233. WORKING_DIRECTORY "@work_dir@"
  234. )
  235. execute_process(
  236. COMMAND "@git_EXECUTABLE@" --git-dir=.git stash pop --quiet
  237. WORKING_DIRECTORY "@work_dir@"
  238. RESULT_VARIABLE error_code
  239. )
  240. if(error_code)
  241. # Stash pop failed: Restore previous state.
  242. execute_process(
  243. COMMAND "@git_EXECUTABLE@" --git-dir=.git reset --hard --quiet ${head_sha}
  244. WORKING_DIRECTORY "@work_dir@"
  245. )
  246. execute_process(
  247. COMMAND "@git_EXECUTABLE@" --git-dir=.git stash pop --index --quiet
  248. WORKING_DIRECTORY "@work_dir@"
  249. )
  250. message(FATAL_ERROR "\nFailed to unstash changes in: '@work_dir@'."
  251. "\nYou will have to resolve the conflicts manually")
  252. endif()
  253. endif()
  254. endif()
  255. set(init_submodules "@init_submodules@")
  256. if(init_submodules)
  257. execute_process(
  258. COMMAND "@git_EXECUTABLE@"
  259. --git-dir=.git @git_submodules_config_options@
  260. submodule update @git_submodules_recurse@ --init @git_submodules@
  261. WORKING_DIRECTORY "@work_dir@"
  262. COMMAND_ERROR_IS_FATAL ANY
  263. )
  264. endif()