Skip to content

Commit a1d1c69

Browse files
committed
GitRepository.temporary_head(): new method
Wrapper for creating a `GitTemporaryHead` context manager (which used to be called `TemporaryHead`).
1 parent 3bdd310 commit a1d1c69

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

git-imerge

+45-35
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,40 @@ class InvalidBranchNameError(Failure):
274274
pass
275275

276276

277+
class GitTemporaryHead(object):
278+
"""A context manager that records the current HEAD state then restores it.
279+
280+
This should only be used when the working copy is clean. message
281+
is used for the reflog.
282+
283+
"""
284+
285+
def __enter__(self, git, message):
286+
self.message = message
287+
try:
288+
self.head_name = check_output(['git', 'symbolic-ref', '--quiet', 'HEAD']).strip()
289+
except CalledProcessError:
290+
self.head_name = None
291+
return self
292+
293+
def __exit__(self, exc_type, exc_val, exc_tb):
294+
if self.head_name:
295+
try:
296+
check_call([
297+
'git', 'symbolic-ref',
298+
'-m', self.message, 'HEAD',
299+
self.head_name,
300+
])
301+
check_call(['git', 'reset', '--hard'])
302+
except CalledProcessError as e:
303+
raise Failure(
304+
'Could not restore HEAD to %r!: %s\n'
305+
% (self.head_name, e.message,)
306+
)
307+
308+
return False
309+
310+
277311
class GitRepository(object):
278312
MERGE_STATE_REFNAME_RE = re.compile(
279313
r"""
@@ -617,6 +651,16 @@ class GitRepository(object):
617651
[commit] = parents
618652
return commit
619653

654+
def temporary_head(self, message):
655+
"""Return a context manager to manage a temporary HEAD.
656+
657+
On entry, record the current HEAD state. On exit, restore it.
658+
message is used for the reflog.
659+
660+
"""
661+
662+
return GitTemporaryHead(self, message)
663+
620664

621665
def rev_parse(arg):
622666
return check_output(['git', 'rev-parse', '--verify', '--quiet', arg]).strip()
@@ -891,40 +935,6 @@ def get_boundaries(tip1, tip2, first_parent):
891935
return (merge_base, commits1, commits2)
892936

893937

894-
class TemporaryHead(object):
895-
"""A context manager that records the current HEAD state then restores it.
896-
897-
This should only be used when the working copy is clean. message
898-
is used for the reflog.
899-
900-
"""
901-
902-
def __enter__(self, message='imerge: restoring'):
903-
self.message = message
904-
try:
905-
self.head_name = check_output(['git', 'symbolic-ref', '--quiet', 'HEAD']).strip()
906-
except CalledProcessError:
907-
self.head_name = None
908-
return self
909-
910-
def __exit__(self, exc_type, exc_val, exc_tb):
911-
if self.head_name:
912-
try:
913-
check_call([
914-
'git', 'symbolic-ref',
915-
'-m', self.message, 'HEAD',
916-
self.head_name,
917-
])
918-
check_call(['git', 'reset', '--hard'])
919-
except CalledProcessError as e:
920-
raise Failure(
921-
'Could not restore HEAD to %r!: %s\n'
922-
% (self.head_name, e.message,)
923-
)
924-
925-
return False
926-
927-
928938
def reparent(commit, parent_sha1s, msg=None):
929939
"""Create a new commit object like commit, but with the specified parents.
930940
@@ -3522,7 +3532,7 @@ def cmd_autofill(parser, options):
35223532
git = GitRepository()
35233533
git.require_clean_work_tree('proceed')
35243534
merge_state = read_merge_state(git, options.name)
3525-
with TemporaryHead():
3535+
with git.temporary_head(message='imerge: restoring'):
35263536
try:
35273537
merge_state.auto_complete_frontier()
35283538
except FrontierBlockedError as e:

0 commit comments

Comments
 (0)