38
38
CHECKING_OUT_DEFAULT_BRANCH
39
39
CHECKED_OUT_DEFAULT_BRANCH
40
40
41
+ CHECKING_OUT_PREVIOUS_BRANCH
42
+ CHECKED_OUT_PREVIOUS_BRANCH
43
+
41
44
PUSHING_TO_REMOTE
42
45
PUSHED_TO_REMOTE
43
46
PUSHING_TO_REMOTE_FAILED
@@ -134,6 +137,11 @@ def set_paused_state(self):
134
137
save_cfg_vals_to_git_cfg (config_path = self .chosen_config_path )
135
138
set_state (WORKFLOW_STATES .BACKPORT_PAUSED )
136
139
140
+ def remember_previous_branch (self ):
141
+ """Save the current branch into Git config to be able to get back to it later."""
142
+ current_branch = get_current_branch ()
143
+ save_cfg_vals_to_git_cfg (previous_branch = current_branch )
144
+
137
145
@property
138
146
def upstream (self ):
139
147
"""Get the remote name to use for upstream branches
@@ -180,24 +188,27 @@ def run_cmd(self, cmd):
180
188
output = subprocess .check_output (cmd , stderr = subprocess .STDOUT )
181
189
return output .decode ("utf-8" )
182
190
183
- def checkout_branch (self , branch_name ):
191
+ def checkout_branch (self , branch_name , * , create_branch = False ):
184
192
"""git checkout -b <branch_name>"""
185
- cmd = [
186
- "git" ,
187
- "checkout" ,
188
- "-b" ,
189
- self .get_cherry_pick_branch (branch_name ),
190
- f"{ self .upstream } /{ branch_name } " ,
191
- ]
193
+ if create_branch :
194
+ cmd = [
195
+ "git" ,
196
+ "checkout" ,
197
+ "-b" ,
198
+ branch_name ,
199
+ f"{ self .upstream } /{ branch_name } " ,
200
+ ]
201
+ else :
202
+ cmd = ["git" , "checkout" , branch_name ]
192
203
try :
193
204
self .run_cmd (cmd )
194
205
except subprocess .CalledProcessError as err :
195
206
click .echo (
196
- f"Error checking out the branch { self . get_cherry_pick_branch ( branch_name ) } ."
207
+ f"Error checking out the branch { branch_name } ."
197
208
)
198
209
click .echo (err .output )
199
210
raise BranchCheckoutException (
200
- f"Error checking out the branch { self . get_cherry_pick_branch ( branch_name ) } ."
211
+ f"Error checking out the branch { branch_name } ."
201
212
)
202
213
203
214
def get_commit_message (self , commit_sha ):
@@ -221,11 +232,23 @@ def checkout_default_branch(self):
221
232
"""git checkout default branch"""
222
233
set_state (WORKFLOW_STATES .CHECKING_OUT_DEFAULT_BRANCH )
223
234
224
- cmd = "git" , "checkout" , self .config ["default_branch" ]
225
- self .run_cmd (cmd )
235
+ self .checkout_branch (self .config ["default_branch" ])
226
236
227
237
set_state (WORKFLOW_STATES .CHECKED_OUT_DEFAULT_BRANCH )
228
238
239
+ def checkout_previous_branch (self ):
240
+ """git checkout previous branch"""
241
+ set_state (WORKFLOW_STATES .CHECKING_OUT_PREVIOUS_BRANCH )
242
+
243
+ previous_branch = load_val_from_git_cfg ("previous_branch" )
244
+ if previous_branch is None :
245
+ self .checkout_default_branch ()
246
+ return
247
+
248
+ self .checkout_branch (previous_branch )
249
+
250
+ set_state (WORKFLOW_STATES .CHECKED_OUT_PREVIOUS_BRANCH )
251
+
229
252
def status (self ):
230
253
"""
231
254
git status
@@ -357,7 +380,12 @@ def cleanup_branch(self, branch):
357
380
Switch to the default branch before that.
358
381
"""
359
382
set_state (WORKFLOW_STATES .REMOVING_BACKPORT_BRANCH )
360
- self .checkout_default_branch ()
383
+ try :
384
+ self .checkout_previous_branch ()
385
+ except BranchCheckoutException :
386
+ click .echo (f"branch { branch } NOT deleted." )
387
+ set_state (WORKFLOW_STATES .REMOVING_BACKPORT_BRANCH_FAILED )
388
+ return
361
389
try :
362
390
self .delete_branch (branch )
363
391
except subprocess .CalledProcessError :
@@ -372,14 +400,15 @@ def backport(self):
372
400
raise click .UsageError ("At least one branch must be specified." )
373
401
set_state (WORKFLOW_STATES .BACKPORT_STARTING )
374
402
self .fetch_upstream ()
403
+ self .remember_previous_branch ()
375
404
376
405
set_state (WORKFLOW_STATES .BACKPORT_LOOPING )
377
406
for maint_branch in self .sorted_branches :
378
407
set_state (WORKFLOW_STATES .BACKPORT_LOOP_START )
379
408
click .echo (f"Now backporting '{ self .commit_sha1 } ' into '{ maint_branch } '" )
380
409
381
410
cherry_pick_branch = self .get_cherry_pick_branch (maint_branch )
382
- self .checkout_branch (maint_branch )
411
+ self .checkout_branch (cherry_pick_branch , create_branch = True )
383
412
commit_message = ""
384
413
try :
385
414
self .cherry_pick ()
@@ -413,6 +442,7 @@ def backport(self):
413
442
self .set_paused_state ()
414
443
return # to preserve the correct state
415
444
set_state (WORKFLOW_STATES .BACKPORT_LOOP_END )
445
+ reset_stored_previous_branch ()
416
446
reset_state ()
417
447
418
448
def abort_cherry_pick (self ):
@@ -434,6 +464,7 @@ def abort_cherry_pick(self):
434
464
if get_current_branch ().startswith ("backport-" ):
435
465
self .cleanup_branch (get_current_branch ())
436
466
467
+ reset_stored_previous_branch ()
437
468
reset_stored_config_ref ()
438
469
reset_state ()
439
470
@@ -493,6 +524,7 @@ def continue_cherry_pick(self):
493
524
)
494
525
set_state (WORKFLOW_STATES .CONTINUATION_FAILED )
495
526
527
+ reset_stored_previous_branch ()
496
528
reset_stored_config_ref ()
497
529
reset_state ()
498
530
@@ -822,6 +854,11 @@ def reset_stored_config_ref():
822
854
"""Config file pointer is not stored in Git config."""
823
855
824
856
857
+ def reset_stored_previous_branch ():
858
+ """Remove the previous branch information from Git config."""
859
+ wipe_cfg_vals_from_git_cfg ("previous_branch" )
860
+
861
+
825
862
def reset_state ():
826
863
"""Remove the progress state from Git config."""
827
864
wipe_cfg_vals_from_git_cfg ("state" )
0 commit comments