1
1
import contextlib
2
2
import os
3
+ import selectors
3
4
import sys
4
5
import tempfile
5
6
7
+ from asyncio import set_event_loop_policy , get_event_loop_policy , DefaultEventLoopPolicy
8
+ from io import IOBase
9
+
6
10
import questionary
7
11
8
12
from commitizen import factory , git , out
19
23
)
20
24
21
25
22
- class WrapStdin :
23
- def __init__ (self ):
24
- fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
25
- tty = open (fd , "wb+" , buffering = 0 )
26
+ class CZEventLoopPolicy (DefaultEventLoopPolicy ):
27
+ def get_event_loop (self ):
28
+ self .set_event_loop (self ._loop_factory (selectors .SelectSelector ()))
29
+ return self ._local ._loop
30
+
31
+ class WrapStdx :
32
+ def __init__ (self , stdx :IOBase ):
33
+ self ._fileno = stdx .fileno ()
34
+ if sys .platform == 'linux' :
35
+ if self ._fileno == 0 :
36
+ fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
37
+ tty = open (fd , "wb+" , buffering = 0 )
38
+ else :
39
+ tty = open ("/dev/tty" , "w" )
40
+ else :
41
+ fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
42
+ if self ._fileno == 0 :
43
+ tty = open (fd , "wb+" , buffering = 0 )
44
+ else :
45
+ tty = open (fd , "rb+" , buffering = 0 )
26
46
self .tty = tty
27
47
28
48
def __getattr__ (self , key ):
29
- if key == "encoding" :
49
+ if key == "encoding" and ( sys . platform != 'linux' or self . _fileno == 0 ) :
30
50
return "UTF-8"
31
51
return getattr (self .tty , key )
32
52
@@ -84,9 +104,11 @@ def __call__(self):
84
104
old_stdin = sys .stdin
85
105
old_stdout = sys .stdout
86
106
old_stderr = sys .stderr
87
- sys .stdin = WrapStdin ()
88
- sys .stdout = open ("/dev/tty" , "w" )
89
- sys .stderr = open ("/dev/tty" , "w" )
107
+ old_event_loop_policy = get_event_loop_policy ()
108
+ set_event_loop_policy (CZEventLoopPolicy ())
109
+ sys .stdin = WrapStdx (sys .stdin )
110
+ sys .stdout = WrapStdx (sys .stdout )
111
+ sys .stderr = WrapStdx (sys .stderr )
90
112
91
113
if git .is_staging_clean () and not dry_run :
92
114
raise NothingToCommitError ("No files added to staging!" )
@@ -98,18 +120,21 @@ def __call__(self):
98
120
else :
99
121
m = self .prompt_commit_questions ()
100
122
101
- out .info (f"\n { m } \n " )
102
-
103
- if dry_run :
104
- raise DryRunExit ()
105
-
106
123
if commit_msg_file :
107
124
sys .stdin .close ()
108
125
sys .stdout .close ()
109
126
sys .stderr .close ()
127
+ set_event_loop_policy (old_event_loop_policy )
110
128
sys .stdin = old_stdin
111
129
sys .stdout = old_stdout
112
130
sys .stderr = old_stderr
131
+
132
+ out .info (f"\n { m } \n " )
133
+
134
+ if dry_run :
135
+ raise DryRunExit ()
136
+
137
+ if commit_msg_file :
113
138
defaultmesaage = ""
114
139
with open (commit_msg_file ) as f :
115
140
defaultmesaage = f .read ()
@@ -126,6 +151,7 @@ def __call__(self):
126
151
else :
127
152
c = git .commit (m )
128
153
154
+
129
155
if c .return_code != 0 :
130
156
out .error (c .err )
131
157
0 commit comments