Skip to content

Commit 6b0e5d7

Browse files
committed
refactor: assume utf-8 encoding
1 parent bbc6612 commit 6b0e5d7

File tree

3 files changed

+9
-52
lines changed

3 files changed

+9
-52
lines changed

README.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@ 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>
2524
hooks:
2625
- id: conventional-pre-commit
2726
stages: [commit-msg]
2827
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
3028
```
3129
3230
Install the `pre-commit` script:
@@ -80,22 +78,6 @@ Conventional Commit......................................................Passed
8078
- duration: 0.05s
8179
```
8280

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-
9981
## Install with pip
10082

10183
`conventional-pre-commit` can also be installed and used from the command line:
@@ -107,17 +89,15 @@ pip install conventional-pre-commit
10789
Then run the command line script:
10890

10991
```shell
110-
conventional-pre-commit [--encoding] [types] input
92+
conventional-pre-commit [types] input
11193
```
11294

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

11797
- `input` is a file containing the commit message to check:
11898

11999
```shell
120-
conventional-pre-commit --encoding=utf-8 feat fix chore ci test .git/COMMIT_MSG
100+
conventional-pre-commit feat fix chore ci test .git/COMMIT_MSG
121101
```
122102

123103
Or from a Python program:

conventional_pre_commit/hook.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ 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")
2221
parser.add_argument("types", type=str, nargs="*", default=format.DEFAULT_TYPES, help="Optional list of types to support")
2322
parser.add_argument("input", type=str, help="A file containing a git commit message")
2423

@@ -31,25 +30,16 @@ def main(argv=[]):
3130
return RESULT_FAIL
3231

3332
try:
34-
with open(args.input, encoding=args.encoding) as f:
33+
with open(args.input, encoding="utf-8") as f:
3534
message = f.read()
3635
except UnicodeDecodeError:
3736
print(
3837
f"""
3938
{Colors.LRED}[Bad Commit message encoding] {Colors.RESTORE}
4039
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, ... ]
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.
5343
"""
5444
)
5545
return RESULT_FAIL

tests/test_hook.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,12 @@ def test_subprocess_success__custom_conventional(cmd, conventional_commit_path):
8383

8484

8585
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])
86+
result = main([conventional_utf8_commit_path])
10087

10188
assert result == RESULT_SUCCESS
10289

10390

10491
def test_main_fail__conventional_gbk(conventional_gbk_commit_path):
105-
result = main(["--encoding=utf-8", conventional_gbk_commit_path])
92+
result = main([conventional_gbk_commit_path])
10693

10794
assert result == RESULT_FAIL

0 commit comments

Comments
 (0)