mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-01 18:56:52 +00:00
Merge pull request #22 from guykisel/check-case-conflict
Add check_case_conflict hook
This commit is contained in:
commit
9b830ad92e
11 changed files with 173 additions and 20 deletions
|
|
@ -8,15 +8,13 @@ import math
|
|||
import os
|
||||
import sys
|
||||
|
||||
from plumbum import local
|
||||
from pre_commit_hooks.util import added_files
|
||||
|
||||
|
||||
def find_large_added_files(filenames, maxkb):
|
||||
# Find all added files that are also in the list of files pre-commit tells
|
||||
# us about
|
||||
filenames = set(local['git'](
|
||||
'diff', '--staged', '--name-only', '--diff-filter', 'A',
|
||||
).splitlines()) & set(filenames)
|
||||
filenames = added_files() & set(filenames)
|
||||
|
||||
retv = 0
|
||||
for filename in filenames:
|
||||
|
|
|
|||
58
pre_commit_hooks/check_case_conflict.py
Normal file
58
pre_commit_hooks/check_case_conflict.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit_hooks.util import added_files
|
||||
|
||||
|
||||
def lower_set(iterable):
|
||||
return set(x.lower() for x in iterable)
|
||||
|
||||
|
||||
def find_conflicting_filenames(filenames):
|
||||
repo_files = set(local['git']('ls-files').splitlines())
|
||||
relevant_files = set(filenames) | added_files()
|
||||
repo_files -= relevant_files
|
||||
retv = 0
|
||||
|
||||
# new file conflicts with existing file
|
||||
conflicts = lower_set(repo_files) & lower_set(relevant_files)
|
||||
|
||||
# new file conflicts with other new file
|
||||
lowercase_relevant_files = lower_set(relevant_files)
|
||||
for filename in set(relevant_files):
|
||||
if filename.lower() in lowercase_relevant_files:
|
||||
lowercase_relevant_files.remove(filename.lower())
|
||||
else:
|
||||
conflicts.add(filename.lower())
|
||||
|
||||
if conflicts:
|
||||
conflicting_files = [
|
||||
x for x in repo_files | relevant_files
|
||||
if x.lower() in conflicts
|
||||
]
|
||||
for filename in sorted(conflicting_files):
|
||||
print('Case-insensitivity conflict found: {0}'.format(filename))
|
||||
retv = 1
|
||||
|
||||
return retv
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'filenames', nargs='*',
|
||||
help='Filenames pre-commit believes are changed.'
|
||||
)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
return find_conflicting_filenames(args.filenames)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
11
pre_commit_hooks/util.py
Normal file
11
pre_commit_hooks/util.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from plumbum import local
|
||||
|
||||
|
||||
def added_files():
|
||||
return set(local['git'](
|
||||
'diff', '--staged', '--name-only', '--diff-filter', 'A',
|
||||
).splitlines())
|
||||
Loading…
Add table
Add a link
Reference in a new issue