Skip to content

Commit 8ebf496

Browse files
authored
Merge pull request #246 from aminya/git_switch_back
2 parents 3a1efcb + 7373cb9 commit 8ebf496

2 files changed

Lines changed: 39 additions & 32 deletions

File tree

src/Git.cmake

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,10 @@ endfunction()
432432
``git_switch_back``
433433
===================
434434
435-
Detect if the head is detached, if so, switch back before calling git pull on a detached head
435+
Detect if the head is detached, if so, switch/checkout back
436+
If the switch/checkout back fails or goes to a detached state, try to checkout the default branch
437+
438+
This is used before updating the repository in a pull
436439
437440
Input variables:
438441
@@ -447,38 +450,42 @@ function(git_switch_back)
447450
message(FATAL_ERROR "REPOSITORY_PATH is required")
448451
endif()
449452

453+
# return if the head is not detached
450454
git_is_detached(IS_DETACHED REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
455+
if(NOT ${IS_DETACHED})
456+
return()
457+
endif()
451458

452-
if(${IS_DETACHED})
453-
message(STATUS "Switch back ${_fun_REPOSITORY_PATH}")
454-
455-
# wait for lock before switching back
456-
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
457-
458-
execute_process(
459-
COMMAND "${GIT_EXECUTABLE}" "switch" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
460-
RESULT_VARIABLE _switch_back_result
461-
)
462-
463-
# if the switch back failed, try to checkout the previous branch
464-
if(NOT ${_switch_back_result} EQUAL 0)
465-
message(STATUS "Switch back failed. Trying to checkout previous branch")
466-
execute_process(
467-
COMMAND "${GIT_EXECUTABLE}" "checkout" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
468-
RESULT_VARIABLE _checkout_result
469-
)
459+
# first try to switch back
460+
message(STATUS "Switch back ${_fun_REPOSITORY_PATH}")
461+
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
462+
execute_process(
463+
COMMAND "${GIT_EXECUTABLE}" "switch" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
464+
RESULT_VARIABLE _switch_back_result
465+
)
466+
git_is_detached(IS_DETACHED REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
467+
if(${_switch_back_result} EQUAL 0 AND NOT ${IS_DETACHED})
468+
return()
469+
endif()
470470

471-
# if the checkout failed, try to checkout the default branch
472-
if(NOT ${_checkout_result} EQUAL 0)
473-
message(STATUS "Checkout previous branch failed. Trying to checkout default branch")
474-
git_default_branch(default_branch REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
475-
execute_process(
476-
COMMAND "${GIT_EXECUTABLE}" "checkout" "${default_branch}"
477-
WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}" COMMAND_ERROR_IS_FATAL LAST
478-
)
479-
endif()
471+
# if the switch back failed, try to checkout the previous branch
472+
message(STATUS "Switch back failed. Trying to checkout previous branch")
473+
git_wait(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
474+
execute_process(
475+
COMMAND "${GIT_EXECUTABLE}" "checkout" "-" WORKING_DIRECTORY "${_fun_REPOSITORY_PATH}"
476+
RESULT_VARIABLE _checkout_result
477+
)
478+
git_is_detached(IS_DETACHED REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
479+
if(${_checkout_result} EQUAL 0 AND NOT ${IS_DETACHED})
480+
return()
481+
endif()
480482

481-
endif()
483+
# switch/checkout back went to a detached state or failed, try to checkout the default branch
484+
git_is_detached(IS_DETACHED REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
485+
if(${IS_DETACHED})
486+
message(STATUS "Trying to checkout default branch")
487+
git_default_branch(default_branch REPOSITORY_PATH "${_fun_REPOSITORY_PATH}")
488+
git_checkout(REPOSITORY_PATH "${_fun_REPOSITORY_PATH}" REVISION "${default_branch}")
482489
endif()
483490
endfunction()
484491

tests/myproj/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ project_options(
6161
# ENABLE_INCLUDE_WHAT_YOU_USE
6262
# ENABLE_GCC_ANALYZER
6363
ENABLE_COVERAGE
64-
ENABLE_PCH
65-
PCH_HEADERS
66-
${PCH_HEADERS}
64+
# ENABLE_PCH
65+
# PCH_HEADERS
66+
# ${PCH_HEADERS}
6767
ENABLE_DOXYGEN
6868
ENABLE_INTERPROCEDURAL_OPTIMIZATION
6969
ENABLE_NATIVE_OPTIMIZATION

0 commit comments

Comments
 (0)