33import os
44import re
55import subprocess
6+ import traceback
67
78import git
89
1314 CIQ_fixes_references ,
1415 CIQ_get_full_hash ,
1516 CIQ_original_commit_author_to_tag_string ,
17+ CIQ_reset_HEAD ,
1618 CIQ_run_git ,
1719)
1820
@@ -57,8 +59,11 @@ def manage_commit_message(full_sha, ciq_tags, jira_ticket, commit_successful):
5759 raise RuntimeError (f"Could not find author of commit { full_sha } " )
5860
5961 new_tags .append (author )
60- with open (MERGE_MSG , "r" ) as file :
61- original_msg = file .readlines ()
62+ try :
63+ with open (MERGE_MSG , "r" ) as file :
64+ original_msg = file .readlines ()
65+ except IOError as e :
66+ raise RuntimeError (f"Failed to read commit message from { MERGE_MSG } : { e } " ) from e
6267
6368 optional_msg = "" if commit_successful else "upstream-diff |"
6469 new_msg = CIQ_cherry_pick_commit_standardization (
@@ -68,8 +73,11 @@ def manage_commit_message(full_sha, ciq_tags, jira_ticket, commit_successful):
6873 print (f"Cherry Pick New Message for { full_sha } " )
6974 print (f"\n Original Message located here: { MERGE_MSG_BAK } " )
7075
71- with open (MERGE_MSG , "w" ) as file :
72- file .writelines (new_msg )
76+ try :
77+ with open (MERGE_MSG , "w" ) as file :
78+ file .writelines (new_msg )
79+ except IOError as e :
80+ raise RuntimeError (f"Failed to write commit message to { MERGE_MSG } : { e } " ) from e
7381
7482
7583def cherry_pick (sha , ciq_tags , jira_ticket ):
@@ -79,6 +87,8 @@ def cherry_pick(sha, ciq_tags, jira_ticket):
7987 - MERGE_MSG.bak contains the original commit message
8088 - MERGE_MSG contains the standardized commit message
8189 - Conflict has to be solved manually
90+ In case runtime errors that are not cherry pick conflicts, the cherry
91+ pick changes are reverted. (git reset --hard HEAD)
8292
8393 In case of success:
8494 - the commit is cherry picked
@@ -87,7 +97,10 @@ def cherry_pick(sha, ciq_tags, jira_ticket):
8797 """
8898
8999 # Expand the provided SHA1 to the full SHA1 in case it's either abbreviated or an expression
90- full_sha = CIQ_get_full_hash (repo = os .getcwd (), short_hash = sha )
100+ try :
101+ full_sha = CIQ_get_full_hash (repo = os .getcwd (), short_hash = sha )
102+ except RuntimeError as e :
103+ raise RuntimeError (f"Invalid commit SHA { sha } : { e } " ) from e
91104
92105 check_fixes (sha = full_sha )
93106
@@ -98,9 +111,13 @@ def cherry_pick(sha, ciq_tags, jira_ticket):
98111 except RuntimeError :
99112 commit_successful = False
100113
101- manage_commit_message (
102- full_sha = full_sha , ciq_tags = ciq_tags , jira_ticket = jira_ticket , commit_successful = commit_successful
103- )
114+ try :
115+ manage_commit_message (
116+ full_sha = full_sha , ciq_tags = ciq_tags , jira_ticket = jira_ticket , commit_successful = commit_successful
117+ )
118+ except RuntimeError as e :
119+ CIQ_reset_HEAD (repo = os .getcwd ())
120+ raise RuntimeError (f"Could not create proper commit message: { e } " ) from e
104121
105122 if not commit_successful :
106123 error_str = (
@@ -131,6 +148,9 @@ def cherry_pick_fixes(sha, ciq_tags, jira_ticket, upstream_ref):
131148def full_cherry_pick (sha , ciq_tags , jira_ticket , upstream_ref ):
132149 """
133150 It cherry picks a commit from upstream-ref along with its Fixes: references.
151+ If cherry-pick or cherry_pick_fixes fail, the exception is propagated
152+ If one of the cherry picks fails, an exception is returned and the previous
153+ successful cherry picks are left as they are.
134154 """
135155 # Cherry pick the commit
136156 cherry_pick (sha = sha , ciq_tags = ciq_tags , jira_ticket = jira_ticket )
@@ -169,5 +189,6 @@ def full_cherry_pick(sha, ciq_tags, jira_ticket, upstream_ref):
169189 try :
170190 full_cherry_pick (sha = args .sha , ciq_tags = tags , jira_ticket = args .ticket , upstream_ref = args .upstream_ref )
171191 except Exception as e :
172- print (e )
192+ print (f"full_cherry_pick failed { e } " )
193+ traceback .print_exc ()
173194 exit (1 )
0 commit comments