Add mixed line ending hook#218
Conversation
To make it easier to use in the program (single string instead of a list of strings)
'--fix=cr' is not yet implemented though
asottile
left a comment
There was a problem hiding this comment.
Overall looks great! This'll be a great addition to the hooks 👍
The branch needs a rebase / pull (there's some conflicts) but overall the idea looks sound.
I've highlighted some small issues below
| language: python | ||
| entry: mixed-line-ending | ||
| description: Replaces or checks mixed line ending | ||
| files: '' |
There was a problem hiding this comment.
(once you rebase) this'll be types: [text] instead (using the brand new in pre-commit 0.15.0 types)
| - `--fix={auto,crlf,lf,no}` | ||
| - `auto` - Replaces automatically the most frequent line ending. This is the default argument. | ||
| - `crlf`, `lf` - Forces to replace line ending by respectively CRLF and LF. | ||
| - `no` - Checks if there is any mixed line ending. |
There was a problem hiding this comment.
I'd say something like: "Error on mixed line endings but do not automatically fix" myself (just so it's more clear that this changes the hook from a fixer into just a checker)
There was a problem hiding this comment.
"Checks if there is any mixed line ending without modifying any file"?
| class LineEnding(Enum): | ||
| CR = b'\r', '\\r', 'cr', re.compile(b'\r(?!\n)', re.DOTALL) | ||
| CRLF = b'\r\n', '\\r\\n', 'crlf', re.compile(b'\r\n', re.DOTALL) | ||
| LF = b'\n', '\\n', 'lf', re.compile(b'(?<!\r)\n', re.DOTALL) |
There was a problem hiding this comment.
hmmm I wonder if there's a way that str_print can be done automatically with repr(...)
|
|
||
| class MixedLineDetection(Enum): | ||
| NOT_MIXED = 1, False, None | ||
| UNKNOWN = 2, False, None |
There was a problem hiding this comment.
ah, we could remove the equality hack if we do something simple like "lf wins on ties" or something. Just an idea
There was a problem hiding this comment.
Well, isn't it platform-related? I mean, Windows users would not appreciate to have their files changed in lf file ending.
There was a problem hiding this comment.
if they're 50/50 I think it's probably fine?
There was a problem hiding this comment.
Actually, mixed_line_ending.py is able to detect LF, CRLF, and CR. So it would be 33% each.
There was a problem hiding this comment.
that makes it even more rare, I'd say just pick one of them if there's ties (since it also simplifies other code elsewhere iirc)
Up to you though, this is fine as is :)
| # or \r | ||
| b'|' + LineEnding.CR.regex.pattern + | ||
| b')' | ||
| ) |
There was a problem hiding this comment.
I think this constant here reads fine (if not better) without the comments -- the previous constants allow this expression to read nicely without
| logging.info('The following files have mixed line endings:\n\t%s', | ||
| '\n\t'.join(mle_filenames)) | ||
|
|
||
| return 1 if mle_found else 0 |
There was a problem hiding this comment.
aka return int(mle_found), though this is fine
| if detect_result == MixedLineDetection.NOT_MIXED: | ||
| logging.info('The file %s has no mixed line ending', filename) | ||
|
|
||
| mle_found |= False |
There was a problem hiding this comment.
x |= False is a noop (can remove this line)
There was a problem hiding this comment.
Completely true omg!
| mle_found |= False | ||
| elif detect_result == MixedLineDetection.UNKNOWN: | ||
| logging.info('Could not define most frequent line ending in ' | ||
| 'file %s. File skiped.', filename) |
|
|
||
|
|
||
| def _convert_line_ending(filename, line_ending): | ||
| # read the file |
There was a problem hiding this comment.
can remove this comment, it doesn't aid in readability (generally stick to why comments and not what comments)
| import re | ||
| import sys | ||
|
|
||
| from enum import Enum |
There was a problem hiding this comment.
in python 2 we'll need to declare this as a dependency.
The magic to doing this is to add this to setup.py:
extras_require={':python_version=="2.7"': ['enum34']}I actually made a video on this topic as well :D
There was a problem hiding this comment.
Well I think I miss something. The tests pass with Python 2.7. But I haven't declared anything relative to enum. Yet, it seems to be "imported" with some magic.
Is it some kind of pre-built environment provided by tox?
There was a problem hiding this comment.
Also, how can I install dependencies provided in extras_require? So far, it worked because I installed enum34 with sudo apt install python-enum34, which is not the universal way to do this FWIU.
There was a problem hiding this comment.
Using requirements-tools's visualize-requirements (that I wrote as well, lol), it seems that enum34 is being pulled in implicitly from flake8:
# first comment out `-e .`
$ visualize-requirements requirements-dev.txt
coverage
flake8
- pyflakes<1.6.0,>=1.5.0
- enum34
- configparser
- mccabe<0.7.0,>=0.6.0
- pycodestyle>=2.0.0,<2.4.0
mock
- six>=1.9
- funcsigs>=1
- pbr>=0.11
pre-commit
- aspy.yaml
- pyyaml
- six
- cached-property
- pyyaml
- identify>=1.0.0
- virtualenv
- nodeenv>=0.11.1
pytest
- setuptools
- py>=1.4.33
The extras_require syntax above just tells (recent enough) pip that "for python2.7, I have these dependencies" -- pip will know what to do automatically.
Do you mean I'm in charge of the integration of this new hook, and I need to resolve the conflict? I'm not used to do pull request, so I don't know who has to do the job. Please tell me 😄 I'll fix soon the issues you pointed out. |
Yep! On your branch run git remote add upstream git@github.com:pre-commit/pre-commit-hooks
git fetch upstream
# either of the following two:
# this one is probably easier -- if you're not super comfortable with git go with this option -- it will preserve your current history and add a merge commit (where you'll resolve the conflicts)
git merge upstream/master
# this will replay your commits on top of master, many consider this more "clean" but it doesn't matter too much
git rebase -i upstream/master |
|
I'm quite comfortable with Git, no problem with that. Was just asking about who is usually in charge of the merges in your work flow. Personally, I prefer to merge a branch to another instead of rebase. I rebase branches when I work on the same branch as others. But it is an endless debate 😃 |
|
👍 whatever you're comfortable with! |
95d161e to
41ff0e1
Compare
|
Let me know when you're ready for me to do another pass at review (maybe you're already ready?) -- there's been a lot of emails from this thread :) |
|
we'll continue this in #233 |
Closes #196