Skip to content

Commit 82cd4d7

Browse files
saygoxLee-W
authored andcommitted
fix(commands/commit): check cz commit msg
1 parent c1018e7 commit 82cd4d7

File tree

7 files changed

+37
-26
lines changed

7 files changed

+37
-26
lines changed

commitizen/commands/commit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import subprocess
77
import tempfile
8+
from os.path import exists
89

910
import questionary
1011

@@ -93,6 +94,17 @@ def manual_edit(self, message: str) -> str:
9394
file.unlink()
9495
return message
9596

97+
def is_blank_commit_file(self, filename) -> bool:
98+
if not exists(filename):
99+
return True
100+
with open(filename, "tr") as f:
101+
for x in f:
102+
if len(x) == 0 or x[0] == "#":
103+
continue
104+
elif x[0] != "\r" and x[0] != "\n":
105+
return False
106+
return True
107+
96108
def __call__(self):
97109
extra_args: str = self.arguments.get("extra_cli_args", "")
98110

@@ -108,6 +120,8 @@ def __call__(self):
108120

109121
commit_msg_file: str = self.arguments.get("commit_msg_file")
110122
if commit_msg_file:
123+
if not self.is_blank_commit_file(commit_msg_file):
124+
return
111125
wrap_stdio()
112126

113127
if git.is_staging_clean() and not (dry_run or allow_empty):

commitizen/wrap_stdio.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

commitizen/wrap_stdio/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
3+
if sys.platform == "win32": # pragma: no cover
4+
from .windows import _unwrap_stdio, _wrap_stdio
5+
elif sys.platform == "linux":
6+
from .linux import _unwrap_stdio, _wrap_stdio # pragma: no cover
7+
else:
8+
from .unix import _unwrap_stdio, _wrap_stdio # pragma: no cover
9+
10+
11+
def wrap_stdio():
12+
_wrap_stdio()
13+
14+
15+
def unwrap_stdio():
16+
_unwrap_stdio()
File renamed without changes.
File renamed without changes.

commitizen/wrap_stdio_windows.py renamed to commitizen/wrap_stdio/windows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, stdx: IOBase):
1515
self._fileno = stdx.fileno()
1616
if self._fileno == 0:
1717
fd = os.open("CONIN$", os.O_RDWR | os.O_BINARY)
18-
tty = open(fd, "r")
18+
tty = open(fd)
1919
handle = HANDLE(msvcrt.get_osfhandle(fd)) # noqa
2020
windll.kernel32.SetStdHandle(STD_INPUT_HANDLE, handle)
2121
elif self._fileno == 1:

tests/test_wrap_stdio.py renamed to tests/wrap_stdio/test_wrap_stdio.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import sys
22

3-
from commitizen import wrap_stdio, wrap_stdio_linux, wrap_stdio_unix, wrap_stdio_windows
3+
from commitizen import wrap_stdio
44

5-
6-
def test_warp_stdio_exists():
7-
assert hasattr(wrap_stdio_windows, "sys")
8-
assert hasattr(wrap_stdio_linux, "sys")
9-
assert hasattr(wrap_stdio_unix, "sys")
5+
# def test_warp_stdio_exists():
6+
# assert hasattr(wrap_stdio_windows, "sys")
7+
# assert hasattr(wrap_stdio_linux, "sys")
8+
# assert hasattr(wrap_stdio_unix, "sys")
109

1110

1211
if sys.platform == "win32": # pragma: no cover
1312
pass
1413
elif sys.platform == "linux":
15-
from commitizen.wrap_stdio_linux import WrapStdinLinux, WrapStdoutLinux
14+
from commitizen.wrap_stdio.linux import WrapStdinLinux, WrapStdoutLinux
1615

1716
def test_wrap_stdin_linux(mocker):
18-
1917
tmp_stdin = sys.stdin
2018

2119
mocker.patch("os.open")
@@ -36,7 +34,6 @@ def test_wrap_stdin_linux(mocker):
3634
assert sys.stdin == tmp_stdin
3735

3836
def test_wrap_stdout_linux(mocker):
39-
4037
tmp_stdout = sys.stdout
4138
tmp_stderr = sys.stderr
4239

0 commit comments

Comments
 (0)