@@ -274,6 +274,40 @@ class InvalidBranchNameError(Failure):
274
274
pass
275
275
276
276
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
+
277
311
class GitRepository (object ):
278
312
MERGE_STATE_REFNAME_RE = re .compile (
279
313
r"""
@@ -617,6 +651,16 @@ class GitRepository(object):
617
651
[commit ] = parents
618
652
return commit
619
653
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
+
620
664
621
665
def rev_parse (arg ):
622
666
return check_output (['git' , 'rev-parse' , '--verify' , '--quiet' , arg ]).strip ()
@@ -891,40 +935,6 @@ def get_boundaries(tip1, tip2, first_parent):
891
935
return (merge_base , commits1 , commits2 )
892
936
893
937
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
-
928
938
def reparent (commit , parent_sha1s , msg = None ):
929
939
"""Create a new commit object like commit, but with the specified parents.
930
940
@@ -3522,7 +3532,7 @@ def cmd_autofill(parser, options):
3522
3532
git = GitRepository ()
3523
3533
git .require_clean_work_tree ('proceed' )
3524
3534
merge_state = read_merge_state (git , options .name )
3525
- with TemporaryHead ( ):
3535
+ with git . temporary_head ( message = 'imerge: restoring' ):
3526
3536
try :
3527
3537
merge_state .auto_complete_frontier ()
3528
3538
except FrontierBlockedError as e :
0 commit comments