Skip to content

Add mixed line ending hook#218

Closed
nagromc wants to merge 43 commits intopre-commit:masterfrom
nagromc:mixed-line-ending
Closed

Add mixed line ending hook#218
nagromc wants to merge 43 commits intopre-commit:masterfrom
nagromc:mixed-line-ending

Conversation

@nagromc
Copy link
Copy Markdown
Contributor

@nagromc nagromc commented Jul 17, 2017

Closes #196

Copy link
Copy Markdown
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread .pre-commit-hooks.yaml
language: python
entry: mixed-line-ending
description: Replaces or checks mixed line ending
files: ''
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(once you rebase) this'll be types: [text] instead (using the brand new in pre-commit 0.15.0 types)

Comment thread README.md Outdated
- `--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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Checks if there is any mixed line ending without modifying any file"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread pre_commit_hooks/mixed_line_ending.py Outdated
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, we could remove the equality hack if we do something simple like "lf wins on ties" or something. Just an idea

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, isn't it platform-related? I mean, Windows users would not appreciate to have their files changed in lf file ending.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if they're 50/50 I think it's probably fine?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, mixed_line_ending.py is able to detect LF, CRLF, and CR. So it would be 33% each.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')'
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aka return int(mle_found), though this is fine

Comment thread pre_commit_hooks/mixed_line_ending.py Outdated
if detect_result == MixedLineDetection.NOT_MIXED:
logging.info('The file %s has no mixed line ending', filename)

mle_found |= False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x |= False is a noop (can remove this line)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely true omg!

Comment thread pre_commit_hooks/mixed_line_ending.py Outdated
mle_found |= False
elif detect_result == MixedLineDetection.UNKNOWN:
logging.info('Could not define most frequent line ending in '
'file %s. File skiped.', filename)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"skipped"

Comment thread pre_commit_hooks/mixed_line_ending.py Outdated


def _convert_line_ending(filename, line_ending):
# read the file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nagromc
Copy link
Copy Markdown
Contributor Author

nagromc commented Jul 18, 2017

The branch needs a rebase / pull

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.

@asottile
Copy link
Copy Markdown
Member

The branch needs a rebase / pull

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 😄

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

@nagromc
Copy link
Copy Markdown
Contributor Author

nagromc commented Jul 18, 2017

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 😃

@asottile
Copy link
Copy Markdown
Member

👍 whatever you're comfortable with!

@nagromc nagromc force-pushed the mixed-line-ending branch from 95d161e to 41ff0e1 Compare July 18, 2017 18:29
@asottile
Copy link
Copy Markdown
Member

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 :)

@asottile
Copy link
Copy Markdown
Member

asottile commented Sep 6, 2017

we'll continue this in #233

@asottile asottile closed this Sep 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants