gitupdate.cmake.in 11 KB

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