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
@@ -138,6 +141,11 @@ def set_paused_state(self):
138
141
save_cfg_vals_to_git_cfg (config_path = self .chosen_config_path )
139
142
set_state (WORKFLOW_STATES .BACKPORT_PAUSED )
140
143
144
+ def remember_previous_branch (self ):
145
+ """Save the current branch into Git config to be able to get back to it later."""
146
+ current_branch = get_current_branch ()
147
+ save_cfg_vals_to_git_cfg (previous_branch = current_branch )
148
+
141
149
@property
142
150
def upstream (self ):
143
151
"""Get the remote name to use for upstream branches
@@ -184,24 +192,29 @@ def run_cmd(self, cmd):
184
192
output = subprocess .check_output (cmd , stderr = subprocess .STDOUT )
185
193
return output .decode ("utf-8" )
186
194
187
- def checkout_branch (self , branch_name ):
188
- """git checkout -b <branch_name>"""
189
- cmd = [
190
- "git" ,
191
- "checkout" ,
192
- "-b" ,
193
- self .get_cherry_pick_branch (branch_name ),
194
- f"{ self .upstream } /{ branch_name } " ,
195
- ]
195
+ def checkout_branch (self , branch_name , * , create_branch = False ):
196
+ """git checkout [-b] <branch_name>"""
197
+ if create_branch :
198
+ checked_out_branch = self .get_cherry_pick_branch (branch_name )
199
+ cmd = [
200
+ "git" ,
201
+ "checkout" ,
202
+ "-b" ,
203
+ checked_out_branch ,
204
+ f"{ self .upstream } /{ branch_name } " ,
205
+ ]
206
+ else :
207
+ checked_out_branch = branch_name
208
+ cmd = ["git" , "checkout" , branch_name ]
196
209
try :
197
210
self .run_cmd (cmd )
198
211
except subprocess .CalledProcessError as err :
199
212
click .echo (
200
- f"Error checking out the branch { self . get_cherry_pick_branch ( branch_name ) } ."
213
+ f"Error checking out the branch { branch_name } ."
201
214
)
202
215
click .echo (err .output )
203
216
raise BranchCheckoutException (
204
- f"Error checking out the branch { self . get_cherry_pick_branch ( branch_name ) } ."
217
+ f"Error checking out the branch { branch_name } ."
205
218
)
206
219
207
220
def get_commit_message (self , commit_sha ):
@@ -225,11 +238,23 @@ def checkout_default_branch(self):
225
238
"""git checkout default branch"""
226
239
set_state (WORKFLOW_STATES .CHECKING_OUT_DEFAULT_BRANCH )
227
240
228
- cmd = "git" , "checkout" , self .config ["default_branch" ]
229
- self .run_cmd (cmd )
241
+ self .checkout_branch (self .config ["default_branch" ])
230
242
231
243
set_state (WORKFLOW_STATES .CHECKED_OUT_DEFAULT_BRANCH )
232
244
245
+ def checkout_previous_branch (self ):
246
+ """git checkout previous branch"""
247
+ set_state (WORKFLOW_STATES .CHECKING_OUT_PREVIOUS_BRANCH )
248
+
249
+ previous_branch = load_val_from_git_cfg ("previous_branch" )
250
+ if previous_branch is None :
251
+ self .checkout_default_branch ()
252
+ return
253
+
254
+ self .checkout_branch (previous_branch )
255
+
256
+ set_state (WORKFLOW_STATES .CHECKED_OUT_PREVIOUS_BRANCH )
257
+
233
258
def status (self ):
234
259
"""
235
260
git status
@@ -363,7 +388,12 @@ def cleanup_branch(self, branch):
363
388
Switch to the default branch before that.
364
389
"""
365
390
set_state (WORKFLOW_STATES .REMOVING_BACKPORT_BRANCH )
366
- self .checkout_default_branch ()
391
+ try :
392
+ self .checkout_previous_branch ()
393
+ except BranchCheckoutException :
394
+ click .echo (f"branch { branch } NOT deleted." )
395
+ set_state (WORKFLOW_STATES .REMOVING_BACKPORT_BRANCH_FAILED )
396
+ return
367
397
try :
368
398
self .delete_branch (branch )
369
399
except subprocess .CalledProcessError :
@@ -378,14 +408,15 @@ def backport(self):
378
408
raise click .UsageError ("At least one branch must be specified." )
379
409
set_state (WORKFLOW_STATES .BACKPORT_STARTING )
380
410
self .fetch_upstream ()
411
+ self .remember_previous_branch ()
381
412
382
413
set_state (WORKFLOW_STATES .BACKPORT_LOOPING )
383
414
for maint_branch in self .sorted_branches :
384
415
set_state (WORKFLOW_STATES .BACKPORT_LOOP_START )
385
416
click .echo (f"Now backporting '{ self .commit_sha1 } ' into '{ maint_branch } '" )
386
417
387
418
cherry_pick_branch = self .get_cherry_pick_branch (maint_branch )
388
- self .checkout_branch (maint_branch )
419
+ self .checkout_branch (maint_branch , create_branch = True )
389
420
commit_message = ""
390
421
try :
391
422
self .cherry_pick ()
@@ -419,6 +450,7 @@ def backport(self):
419
450
self .set_paused_state ()
420
451
return # to preserve the correct state
421
452
set_state (WORKFLOW_STATES .BACKPORT_LOOP_END )
453
+ reset_stored_previous_branch ()
422
454
reset_state ()
423
455
424
456
def abort_cherry_pick (self ):
@@ -440,6 +472,7 @@ def abort_cherry_pick(self):
440
472
if get_current_branch ().startswith ("backport-" ):
441
473
self .cleanup_branch (get_current_branch ())
442
474
475
+ reset_stored_previous_branch ()
443
476
reset_stored_config_ref ()
444
477
reset_state ()
445
478
@@ -499,6 +532,7 @@ def continue_cherry_pick(self):
499
532
)
500
533
set_state (WORKFLOW_STATES .CONTINUATION_FAILED )
501
534
535
+ reset_stored_previous_branch ()
502
536
reset_stored_config_ref ()
503
537
reset_state ()
504
538
@@ -828,6 +862,11 @@ def reset_stored_config_ref():
828
862
"""Config file pointer is not stored in Git config."""
829
863
830
864
865
+ def reset_stored_previous_branch ():
866
+ """Remove the previous branch information from Git config."""
867
+ wipe_cfg_vals_from_git_cfg ("previous_branch" )
868
+
869
+
831
870
def reset_state ():
832
871
"""Remove the progress state from Git config."""
833
872
wipe_cfg_vals_from_git_cfg ("state" )
0 commit comments