Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a3ffb6

Browse files
committed
feat(commands/commit): apply prepare-commit-msg hook for Mac
1 parent 09fa312 commit 1a3ffb6

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

‎commitizen/commands/commit.py‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import contextlib
22
import os
3+
import selectors
34
import sys
45
import tempfile
56

7+
from asyncio import set_event_loop_policy, get_event_loop_policy, DefaultEventLoopPolicy
8+
from io import IOBase
9+
610
import questionary
711

812
from commitizen import factory, git, out
@@ -19,14 +23,30 @@
1923
)
2024

2125

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)
2646
self.tty = tty
2747

2848
def __getattr__(self, key):
29-
if key == "encoding":
49+
if key == "encoding"and (sys.platform!='linux'orself._fileno==0) :
3050
return "UTF-8"
3151
return getattr(self.tty, key)
3252

@@ -79,9 +99,11 @@ def __call__(self):
7999
old_stdin = sys.stdin
80100
old_stdout = sys.stdout
81101
old_stderr = sys.stderr
82-
sys.stdin = WrapStdin()
83-
sys.stdout = open("/dev/tty", "w")
84-
sys.stderr = open("/dev/tty", "w")
102+
old_event_loop_policy=get_event_loop_policy()
103+
set_event_loop_policy(CZEventLoopPolicy())
104+
sys.stdin = WrapStdx(sys.stdin)
105+
sys.stdout = WrapStdx(sys.stdout)
106+
sys.stderr = WrapStdx(sys.stderr)
85107

86108
if git.is_staging_clean() and not dry_run:
87109
raise NothingToCommitError("No files added to staging!")
@@ -102,6 +124,7 @@ def __call__(self):
102124
sys.stdin.close()
103125
sys.stdout.close()
104126
sys.stderr.close()
127+
set_event_loop_policy(old_event_loop_policy)
105128
sys.stdin = old_stdin
106129
sys.stdout = old_stdout
107130
sys.stderr = old_stderr
@@ -116,6 +139,14 @@ def __call__(self):
116139

117140
c = git.commit(m)
118141

142+
signoff: bool = self.arguments.get("signoff")
143+
144+
if signoff:
145+
c = git.commit(m, "-s")
146+
else:
147+
c = git.commit(m)
148+
149+
119150
if c.return_code != 0:
120151
out.error(c.err)
121152

‎tests/commands/test_commit_command.py‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
177177
}
178178

179179
commit_mock = mocker.patch("commitizen.git.commit")
180-
mocker.patch("commitizen.commands.commit.WrapStdin")
180+
commit_mock.return_value = cmd.Command("success", "", "", "", 0)
181+
mocker.patch("commitizen.commands.commit.WrapStdx")
181182
mocker.patch("os.open")
182183
reader_mock = mocker.mock_open(read_data="\n\n#test\n")
183184
mocker.patch("builtins.open", reader_mock, create=True)
@@ -188,13 +189,38 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
188189
assert "Commit message is successful!" in out
189190
commit_mock.assert_not_called()
190191

191-
192-
def test_WrapStdin(mocker):
192+
def test_WrapStdx(mocker):
193193
mocker.patch("os.open")
194194
reader_mock = mocker.mock_open(read_data="data")
195195
mocker.patch("builtins.open", reader_mock, create=True)
196196

197-
wrap_stdin = commands.commit.WrapStdin()
197+
stdin_mock_fileno=mocker.patch.object(sys.stdin, 'fileno')
198+
stdin_mock_fileno.return_value = 0
199+
wrap_stdin = commands.commit.WrapStdx(sys.stdin)
198200

199201
assert wrap_stdin.encoding == "UTF-8"
200202
assert wrap_stdin.read() == "data"
203+
204+
205+
writer_mock = mocker.mock_open(read_data="data")
206+
mocker.patch("builtins.open", writer_mock, create=True)
207+
stdout_mock_fileno=mocker.patch.object(sys.stdout, 'fileno')
208+
stdout_mock_fileno.return_value = 1
209+
wrap_stout = commands.commit.WrapStdx(sys.stdout)
210+
wrap_stout.write("data")
211+
212+
writer_mock.assert_called_once_with("/dev/tty", 'w')
213+
writer_mock().write.assert_called_once_with("data")
214+
215+
216+
writer_mock = mocker.mock_open(read_data="data")
217+
mocker.patch("builtins.open", writer_mock, create=True)
218+
stderr_mock_fileno=mocker.patch.object(sys.stdout, 'fileno')
219+
stderr_mock_fileno.return_value = 2
220+
wrap_sterr = commands.commit.WrapStdx(sys.stderr)
221+
222+
223+
wrap_sterr.write("data")
224+
225+
writer_mock.assert_called_once_with("/dev/tty", 'w')
226+
writer_mock().write.assert_called_once_with("data")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /