@@ -43,28 +43,36 @@ def update_single_repository(args):
43
43
try :
44
44
print ("Updating '" + repo_path + "'" )
45
45
with shell .pushd (repo_path , dry_run = False , echo = False ):
46
- shell .run (["git" , "fetch" , "--recurse-submodules=yes" ], echo = True )
47
-
46
+ # The clean option should restore a repository to pristine condition.
48
47
if should_clean :
49
48
shell .run (['git' , 'clean' , '-fdx' ], echo = True )
50
49
shell .run (['git' , 'submodule' , 'foreach' , '--recursive' , 'git' ,
51
50
'clean' , '-fdx' ], echo = True )
52
51
shell .run (['git' , 'submodule' , 'foreach' , '--recursive' , 'git' ,
53
52
'reset' , '--hard' , 'HEAD' ], echo = True )
54
- shell .run (['git' , 'reset' , '--hard' , 'HEAD' ],
55
- echo = True )
53
+ shell .run (['git' , 'reset' , '--hard' , 'HEAD' ], echo = True )
54
+ # It is possible to reset --hard and still be in the middle of a rebase.
55
+ try :
56
+ shell .run (['git' , 'rebase' , '--abort' ], echo = True )
57
+ except :
58
+ pass
56
59
57
60
if branch :
58
61
shell .run (['git' , 'status' , '--porcelain' , '-uno' ],
59
62
echo = False )
60
63
shell .run (['git' , 'checkout' , branch ], echo = True )
61
64
62
- # If we were asked to reset to the specified branch, do the hard
63
- # reset and return.
64
- if reset_to_remote and not cross_repo :
65
- shell .run (['git' , 'reset' , '--hard' , "origin/%s" % branch ],
66
- echo = True )
67
- return
65
+ # It's important that we checkout, fetch, and rebase, in order.
66
+ # .git/FETCH_HEAD updates the not-for-merge attributes based on which
67
+ # branch was checked out during the fetch.
68
+ shell .run (["git" , "fetch" , "--recurse-submodules=yes" ], echo = True )
69
+
70
+ # If we were asked to reset to the specified branch, do the hard
71
+ # reset and return.
72
+ if branch and reset_to_remote and not cross_repo :
73
+ shell .run (['git' , 'reset' , '--hard' , "origin/%s" % branch ],
74
+ echo = True )
75
+ return
68
76
69
77
# Prior to Git 2.6, this is the way to do a "git pull
70
78
# --rebase" that respects rebase.autostash. See
0 commit comments