3
3
import subprocess
4
4
import sys
5
5
from pathlib import Path
6
- from subprocess import CalledProcessError
7
6
8
7
try :
9
8
from commitizen .cz .utils import get_backup_file_path
@@ -24,38 +23,40 @@ def prepare_commit_msg(commit_msg_file: str) -> int:
24
23
],
25
24
capture_output = True ,
26
25
).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 )
54
54
return 0
55
55
56
56
57
57
if __name__ == "__main__" :
58
58
# make hook interactive by attaching /dev/tty to stdin
59
59
with open ("/dev/tty" ) as tty :
60
60
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