@@ -19,7 +19,7 @@ def capitalize(text, capitalize_spaces):
1919 return text [:capitalize_spaces ].upper () + text [capitalize_spaces :] if text else text
2020
2121
22- def update_commit_message (filename , regex , mode , format_string , conventionalcommits = False , capitalize_spaces = 0 ):
22+ def update_commit_message (filename , regex , mode , format_string , conventionalcommits = False , capitalize_spaces = 0 , to_trailer = False ):
2323 with io .open (filename , 'r+' ) as fd :
2424 contents = fd .readlines ()
2525 commit_msg = contents [0 ].rstrip ('\r \n ' )
@@ -36,6 +36,18 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm
3636 tickets = [branch .split (six .text_type ('_' ))[0 ]]
3737 tickets = [t .strip () for t in tickets ]
3838
39+ if to_trailer :
40+ trailer = 'Refs: {tickets}\n ' .format (tickets = ', ' .join (tickets ))
41+ if contents and not contents [- 1 ].endswith ('\n ' ):
42+ contents [- 1 ] += '\n '
43+ if not contents or contents [- 1 ].strip () != '' :
44+ contents .append ('\n ' )
45+ contents .append (trailer )
46+ fd .seek (0 )
47+ fd .writelines (contents )
48+ fd .truncate ()
49+ return
50+
3951 if conventionalcommits and (match := re .match (conventionalcommit_regex , commit_msg )):
4052 # If the commit message matches the Conventional Commits spec, we can use the captured groups.
4153 type = match .group ('type' )
@@ -86,6 +98,7 @@ def main(argv=None):
8698 parser .add_argument ('filenames' , nargs = '+' )
8799 parser .add_argument ('--conventionalcommits' , action = 'store_true' )
88100 parser .add_argument ('--capitalize' , nargs = '?' , const = 1 , type = int , default = 0 )
101+ parser .add_argument ('--to_trailer' , action = 'store_true' )
89102 parser .add_argument ('--regex' )
90103 parser .add_argument ('--format' , nargs = '?' )
91104 parser .add_argument ('--mode' , nargs = '?' , const = underscore_split_mode ,
@@ -97,7 +110,7 @@ def main(argv=None):
97110 return 1
98111 regex = args .regex or r'[A-Z]+-\d+' # noqa
99112 format_string = args .format or '{ticket} {commit_msg}' # noqa
100- update_commit_message (args .filenames [0 ], regex , args .mode , format_string , args .conventionalcommits , args .capitalize )
113+ update_commit_message (args .filenames [0 ], regex , args .mode , format_string , args .conventionalcommits , args .capitalize , args . to_trailer )
101114
102115
103116if __name__ == '__main__' :
0 commit comments