33import subprocess
44import sys
55from pathlib import Path
6- from subprocess import CalledProcessError
76
87try :
98 from commitizen .cz .utils import get_backup_file_path
@@ -24,38 +23,40 @@ def prepare_commit_msg(commit_msg_file: str) -> int:
2423 ],
2524 capture_output = True ,
2625 ).returncode
27- if exit_code != 0 :
28- backup_file = Path (get_backup_file_path ())
29- if backup_file .is_file ():
30- # confirm if commit message from backup file should be reused
31- answer = input ("retry with previous message? [y/N]: " )
32- if answer .lower () == "y" :
33- shutil .copyfile (backup_file , commit_msg_file )
34- return 0
35-
36- # use commitizen to generate the commit message
37- try :
38- subprocess .run (
39- [
40- "cz" ,
41- "commit" ,
42- "--dry-run" ,
43- "--write-message-to-file" ,
44- commit_msg_file ,
45- ],
46- stdin = sys .stdin ,
47- stdout = sys .stdout ,
48- ).check_returncode ()
49- except CalledProcessError as error :
50- return error .returncode
51-
52- # write message to backup file
53- shutil .copyfile (commit_msg_file , backup_file )
26+ if exit_code == 0 :
27+ return 0
28+
29+ backup_file = Path (get_backup_file_path ())
30+ if backup_file .is_file ():
31+ # confirm if commit message from backup file should be reused
32+ answer = input ("retry with previous message? [y/N]: " )
33+ if answer .lower () == "y" :
34+ shutil .copyfile (backup_file , commit_msg_file )
35+ return 0
36+
37+ # use commitizen to generate the commit message
38+ exit_code = subprocess .run (
39+ [
40+ "cz" ,
41+ "commit" ,
42+ "--dry-run" ,
43+ "--write-message-to-file" ,
44+ commit_msg_file ,
45+ ],
46+ stdin = sys .stdin ,
47+ stdout = sys .stdout ,
48+ ).returncode
49+ if exit_code :
50+ return exit_code
51+
52+ # write message to backup file
53+ shutil .copyfile (commit_msg_file , backup_file )
5454 return 0
5555
5656
5757if __name__ == "__main__" :
5858 # make hook interactive by attaching /dev/tty to stdin
5959 with open ("/dev/tty" ) as tty :
6060 sys .stdin = tty
61- exit (prepare_commit_msg (sys .argv [1 ]))
61+ exit_code = prepare_commit_msg (sys .argv [1 ])
62+ exit (exit_code )
0 commit comments