Skip to content

Commit bbc6612

Browse files
Nagicothekaveman
authored andcommitted
feat: Add commit encoding setting
1 parent 3be2eab commit bbc6612

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repos:
2626
- id: conventional-pre-commit
2727
stages: [commit-msg]
2828
args: [] # optional: list of Conventional Commits types to allow e.g. [feat, fix, ci, chore, test]
29+
# if you want to set encoding, use --encoding=<encoding>, before types
2930
```
3031

3132
Install the `pre-commit` script:
@@ -79,6 +80,22 @@ Conventional Commit......................................................Passed
7980
- duration: 0.05s
8081
```
8182

83+
### Configure encoding
84+
85+
**For Windows user**, if you want to use `conventional-pre-commit` with non-ascii characters, you can set encoding with `--encoding=<encoding>`.
86+
87+
```yaml
88+
args: [--encoding=<encoding>]
89+
```
90+
91+
or
92+
93+
```yaml
94+
args: [--encoding=<encoding>, feat, ...(other custom types)]
95+
```
96+
97+
**The encoding argument must be in front of types.**
98+
8299
## Install with pip
83100
84101
`conventional-pre-commit` can also be installed and used from the command line:
@@ -90,15 +107,17 @@ pip install conventional-pre-commit
90107
Then run the command line script:
91108

92109
```shell
93-
conventional-pre-commit [types] input
110+
conventional-pre-commit [--encoding] [types] input
94111
```
95112

96-
Where `[types]` is an optional list of Conventional Commit types to allow (e.g. `feat fix chore`)
113+
- `--encoding` is an optional encoding to use (e.g. `--encoding=utf-8`)
114+
115+
- `[types]` is an optional list of Conventional Commit types to allow (e.g. `feat fix chore`)
97116

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

100119
```shell
101-
conventional-pre-commit feat fix chore ci test .git/COMMIT_MSG
120+
conventional-pre-commit --encoding=utf-8 feat fix chore ci test .git/COMMIT_MSG
102121
```
103122

104123
Or from a Python program:

conventional_pre_commit/hook.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def main(argv=[]):
1818
parser = argparse.ArgumentParser(
1919
prog="conventional-pre-commit", description="Check a git commit message for Conventional Commits formatting."
2020
)
21+
parser.add_argument("--encoding", type=str, default=None, help="Optional encoding to use when reading the commit message")
2122
parser.add_argument("types", type=str, nargs="*", default=format.DEFAULT_TYPES, help="Optional list of types to support")
2223
parser.add_argument("input", type=str, help="A file containing a git commit message")
2324

@@ -29,8 +30,29 @@ def main(argv=[]):
2930
except SystemExit:
3031
return RESULT_FAIL
3132

32-
with open(args.input) as f:
33-
message = f.read()
33+
try:
34+
with open(args.input, encoding=args.encoding) as f:
35+
message = f.read()
36+
except UnicodeDecodeError:
37+
print(
38+
f"""
39+
{Colors.LRED}[Bad Commit message encoding] {Colors.RESTORE}
40+
41+
{Colors.YELLOW}It looks like we couldn't decode your commit message using the encoding you or your system specified.
42+
You can specify an encoding using the --encoding flag.{Colors.RESTORE}
43+
44+
For example, if your commit message is encoded in {Colors.YELLOW}UTF-8{Colors.RESTORE},
45+
you can add this to your .pre-commit-config.yaml file:
46+
47+
- repo: https://github.com/compilerla/conventional-pre-commit
48+
rev: xxx
49+
hooks:
50+
- id: conventional-pre-commit
51+
stages: [ commit-msg ]
52+
args: [ {Colors.YELLOW}--encoding=utf-8, {Colors.RESTORE}custom-types, ... ]
53+
"""
54+
)
55+
return RESULT_FAIL
3456

3557
if format.is_conventional(message, args.types):
3658
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,28 @@ 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(["--encoding=utf-8", conventional_utf8_commit_path])
87+
88+
assert result == RESULT_SUCCESS
89+
90+
91+
@pytest.mark.skip(reason="read utf-8 file with gbk encoding will cause mojibake instead of raising UnicodeDecodeError")
92+
def test_main_fail__conventional_utf8(conventional_utf8_commit_path):
93+
result = main(["--encoding=gbk", conventional_utf8_commit_path])
94+
95+
assert result == RESULT_FAIL
96+
97+
98+
def test_main_success__conventional_gbk(conventional_gbk_commit_path):
99+
result = main(["--encoding=gbk", conventional_gbk_commit_path])
100+
101+
assert result == RESULT_SUCCESS
102+
103+
104+
def test_main_fail__conventional_gbk(conventional_gbk_commit_path):
105+
result = main(["--encoding=utf-8", conventional_gbk_commit_path])
106+
107+
assert result == RESULT_FAIL

0 commit comments

Comments
 (0)