compilerla / conventional-pre-commit

A pre-commit hook that checks commit messages for Conventional Commits formatting
Apache License 2.0
348 stars 59 forks source link

Add commit encoding setting #49

Closed Nagico closed 1 year ago

Nagico commented 1 year ago

Problem

It's a long story. For those Chinese users developing with Windows system, the system default encoding is gbk, but the applications use uft-8 most frequently.

So when we commit with utf-8 message, the conventional-pre-commit will read text in gbk encoding, and raise UnicodeDecodeError.

with open(args.input) as f:
    message = f.read()

image

Solution

Add an --encoding argument to specify the encoding. See Readme.md for more details.

thekaveman commented 1 year ago

Hello @Nagico - thank you for raising this issue and for the PR to solve it. While I can understand the approach of allowing the user to override the encoding used, as you mention:

but the applications use uft-8 most frequently

So I wonder if a simpler change would be to enforce/assume reading in utf-8 vs. a user having to override to utf-8 if their system default is something else? E.g. just hardcode it:

with open(args.input, encoding="utf-8") as f:
    message = f.read()

What do you think about this instead?

thekaveman commented 1 year ago

Pinging back @Nagico - see my comments above. Thoughts?

Nagico commented 1 year ago

Pinging back @Nagico - see my comments above. Thoughts?

I'm sorry to be so late reply to you! I think it's a great idea to hardcode it.

Although most applications (like Pycharm) use UTF-8, the windows terminal (cmd or powershell) still uses GBK. But after my testing, hardcoding is able to submit commits within the terminal normally, possibly because Git will handle all the commit content in UTF-8.

I will close this PR and if you need any other tests, please feel free to contact me at any time!

thekaveman commented 1 year ago

Thanks again for this suggestion @Nagico! We just published 2.2.0 to PyPI with the encoding fix!

Release notes here: https://github.com/compilerla/conventional-pre-commit/releases/tag/v2.2.0