Skip to content

Commit 65aedd3

Browse files
committed
feat(commands/commit): apply prepare-commit-msg hook for Win
1 parent d31186e commit 65aedd3

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

commitizen/wrap_stdio_unix.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def get_event_loop(self):
1111
return self._local._loop
1212

1313

14-
class WrapStdioLinux:
14+
class WrapStdioUnix:
1515
def __init__(self, stdx: IOBase):
1616
self._fileno = stdx.fileno()
1717
fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
@@ -43,15 +43,15 @@ def _wrap_stdio():
4343

4444
global backup_stdin
4545
backup_stdin = sys.stdin
46-
sys.stdin = WrapStdioLinux(sys.stdin)
46+
sys.stdin = WrapStdioUnix(sys.stdin)
4747

4848
global backup_stdout
4949
backup_stdout = sys.stdout
50-
sys.stdout = WrapStdioLinux(sys.stdout)
50+
sys.stdout = WrapStdioUnix(sys.stdout)
5151

5252
global backup_stderr
5353
backup_stdout = sys.stderr
54-
sys.stderr = WrapStdioLinux(sys.stderr)
54+
sys.stderr = WrapStdioUnix(sys.stderr)
5555

5656

5757
def _unwrap_stdio():

commitizen/wrap_stdio_windows.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
1-
def _wrap_stdio():
2-
pass
1+
import sys
32

3+
if sys.platform == "win32": # pragma: no cover
4+
import msvcrt
5+
import os
6+
from ctypes import c_ulong, windll # noqa
7+
from ctypes.wintypes import HANDLE
8+
from io import IOBase
49

5-
def _unwrap_stdio():
6-
pass
10+
STD_INPUT_HANDLE = c_ulong(-10)
11+
STD_OUTPUT_HANDLE = c_ulong(-11)
12+
13+
class WrapStdioWindows:
14+
def __init__(self, stdx: IOBase):
15+
self._fileno = stdx.fileno()
16+
if self._fileno == 0:
17+
fd = os.open("CONIN$", os.O_RDWR | os.O_BINARY)
18+
tty = open(fd, "r")
19+
handle = HANDLE(msvcrt.get_osfhandle(fd)) # noqa
20+
windll.kernel32.SetStdHandle(STD_INPUT_HANDLE, handle)
21+
elif self._fileno == 1:
22+
fd = os.open("CONOUT$", os.O_RDWR | os.O_BINARY)
23+
tty = open(fd, "w")
24+
handle = HANDLE(msvcrt.get_osfhandle(fd)) # noqa
25+
windll.kernel32.SetStdHandle(STD_OUTPUT_HANDLE, handle)
26+
else:
27+
raise Exception("not defined type")
28+
self._tty = tty
29+
30+
def __getattr__(self, key):
31+
if key == "encoding" and self._fileno == 0:
32+
return "UTF-8"
33+
return getattr(self._tty, key)
34+
35+
def __del__(self):
36+
if "_tty" in self.__dict__:
37+
self._tty.close()
38+
39+
backup_stdin = None
40+
backup_stdout = None
41+
42+
def _wrap_stdio():
43+
global backup_stdin
44+
backup_stdin = sys.stdin
45+
sys.stdin = WrapStdioWindows(sys.stdin)
46+
47+
global backup_stdout
48+
backup_stdout = sys.stdout
49+
sys.stdout = WrapStdioWindows(sys.stdout)
50+
51+
def _unwrap_stdio():
52+
global backup_stdin
53+
sys.stdin.close()
54+
sys.stdin = backup_stdin
55+
56+
global backup_stdout
57+
sys.stdout.close()
58+
sys.stdout = backup_stdout

0 commit comments

Comments
 (0)