Skip to content

Commit 4db44b2

Browse files
authored
Feat: assume UTF-8 encoding for commit messages (#51)
2 parents 3be2eab + 6b0e5d7 commit 4db44b2

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ Add a new repo entry to your configuration file:
1717

1818
```yaml
1919
repos:
20-
21-
# - repo: ...
20+
# - repo: ...
2221

2322
- repo: https://github.com/compilerla/conventional-pre-commit
2423
rev: <git sha or tag>
@@ -93,9 +92,9 @@ Then run the command line script:
9392
conventional-pre-commit [types] input
9493
```
9594

96-
Where `[types]` is an optional list of Conventional Commit types to allow (e.g. `feat fix chore`)
95+
- `[types]` is an optional list of Conventional Commit types to allow (e.g. `feat fix chore`)
9796

98-
And `input` is a file containing the commit message to check:
97+
- `input` is a file containing the commit message to check:
9998

10099
```shell
101100
conventional-pre-commit feat fix chore ci test .git/COMMIT_MSG

conventional_pre_commit/hook.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ def main(argv=[]):
2929
except SystemExit:
3030
return RESULT_FAIL
3131

32-
with open(args.input) as f:
33-
message = f.read()
32+
try:
33+
with open(args.input, encoding="utf-8") as f:
34+
message = f.read()
35+
except UnicodeDecodeError:
36+
print(
37+
f"""
38+
{Colors.LRED}[Bad Commit message encoding] {Colors.RESTORE}
39+
40+
{Colors.YELLOW}conventional-pre-commit couldn't decode your commit message.{Colors.RESTORE}
41+
{Colors.YELLOW}UTF-8{Colors.RESTORE} encoding is assumed, please configure git to write commit messages in UTF-8.
42+
See {Colors.LBLUE}https://git-scm.com/docs/git-commit/#_discussion{Colors.RESTORE} for more.
43+
"""
44+
)
45+
return RESULT_FAIL
3446

3547
if format.is_conventional(message, args.types):
3648
return RESULT_SUCCESS

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ def conventional_commit_path():
2222
@pytest.fixture
2323
def custom_commit_path():
2424
return get_message_path("custom_commit")
25+
26+
27+
@pytest.fixture
28+
def conventional_utf8_commit_path():
29+
return get_message_path("conventional_commit_utf-8")
30+
31+
32+
@pytest.fixture
33+
def conventional_gbk_commit_path():
34+
return get_message_path("conventional_commit_gbk")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: utf-8 test ����
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: utf-8 test 测试

tests/test_hook.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,15 @@ def test_subprocess_success__custom_conventional(cmd, conventional_commit_path):
8080
result = subprocess.call((cmd, "custom", conventional_commit_path))
8181

8282
assert result == RESULT_SUCCESS
83+
84+
85+
def test_main_success__conventional_utf8(conventional_utf8_commit_path):
86+
result = main([conventional_utf8_commit_path])
87+
88+
assert result == RESULT_SUCCESS
89+
90+
91+
def test_main_fail__conventional_gbk(conventional_gbk_commit_path):
92+
result = main([conventional_gbk_commit_path])
93+
94+
assert result == RESULT_FAIL

0 commit comments

Comments
 (0)