Skip to content

Commit 0312579

Browse files
colinhowellerg
authored andcommitted
utils/update-checkout: Fix for mishandling detached HEAD in SR-3854 (swiftlang#7232)
* utils/update-checkout: Fix for mishandling detached HEAD in SR-3854 In update_single_repository, before doing the rebase step, check whether the current HEAD is a "detached HEAD", normally the result of checking out a tag by previously invoking update-checkout with the --tag option. If HEAD is a detached HEAD, skip the rebase (and print an explanatory message), because rebasing would do the wrong thing and make a mess. * utils/update-checkout: more robust error handling for SR-3854 fix We now examine the exception raised by the "git symbolic-ref -q HEAD" command to check whether the exception was indeed due to a detached HEAD. If there was an actual error in the command, we re-raise the exception so that it can be handled like errors from other commands.
1 parent 2a06415 commit 0312579

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

utils/update-checkout

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,35 @@ def update_single_repository(args):
7474
echo=True)
7575
return
7676

77+
# Query whether we have a "detached HEAD", which will mean that
78+
# we previously checked out a tag rather than a branch.
79+
detached_head = False
80+
try:
81+
# This git command returns error code 1 if HEAD is detached.
82+
# Otherwise there was some other error, and we need to handle
83+
# it like other command errors.
84+
shell.run(["git", "symbolic-ref", "-q", "HEAD"], echo=False)
85+
except Exception as e:
86+
if e.ret == 1:
87+
detached_head = True
88+
else:
89+
raise # Pass this error up the chain.
90+
91+
# If we have a detached HEAD in this repository, we don't want
92+
# to rebase. With a detached HEAD, the fetch will have marked
93+
# all the branches in FETCH_HEAD as not-for-merge, and the
94+
# "git rebase FETCH_HEAD" will try to rebase the tree from the
95+
# default branch's current head, making a mess.
96+
7797
# Prior to Git 2.6, this is the way to do a "git pull
7898
# --rebase" that respects rebase.autostash. See
7999
# http://stackoverflow.com/a/30209750/125349
80-
if not cross_repo:
100+
if not cross_repo and not detached_head:
81101
shell.run(["git", "rebase", "FETCH_HEAD"], echo=True)
102+
elif detached_head:
103+
print(repo_path,
104+
"\nDetached HEAD; probably checked out a tag. No need to rebase.\n")
105+
82106
shell.run(["git", "submodule", "update", "--recursive"], echo=True)
83107
except:
84108
(type, value, tb) = sys.exc_info()

0 commit comments

Comments
 (0)