2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-01-08 06:07:32 +08:00
|
|
|
import argparse
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Iterable
|
|
|
|
from collections.abc import Iterator
|
|
|
|
from collections.abc import Sequence
|
2015-01-08 06:07:32 +08:00
|
|
|
|
|
|
|
from pre_commit_hooks.util import added_files
|
2015-03-21 04:52:21 +08:00
|
|
|
from pre_commit_hooks.util import cmd_output
|
2015-01-08 06:07:32 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def lower_set(iterable: Iterable[str]) -> set[str]:
|
2017-03-13 09:01:29 +08:00
|
|
|
return {x.lower() for x in iterable}
|
2015-01-08 06:07:32 +08:00
|
|
|
|
|
|
|
|
2021-03-19 07:59:31 +08:00
|
|
|
def parents(file: str) -> Iterator[str]:
|
2021-07-10 03:26:07 +08:00
|
|
|
path_parts = file.split('/')
|
|
|
|
path_parts.pop()
|
|
|
|
while path_parts:
|
|
|
|
yield '/'.join(path_parts)
|
|
|
|
path_parts.pop()
|
2021-03-19 07:59:31 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def directories_for(files: set[str]) -> set[str]:
|
2021-03-19 07:59:31 +08:00
|
|
|
return {parent for file in files for parent in parents(file)}
|
|
|
|
|
|
|
|
|
2020-02-06 03:10:42 +08:00
|
|
|
def find_conflicting_filenames(filenames: Sequence[str]) -> int:
|
2015-03-21 04:52:21 +08:00
|
|
|
repo_files = set(cmd_output('git', 'ls-files').splitlines())
|
2021-03-19 07:59:31 +08:00
|
|
|
repo_files |= directories_for(repo_files)
|
2015-01-08 06:07:32 +08:00
|
|
|
relevant_files = set(filenames) | added_files()
|
2021-03-19 07:59:31 +08:00
|
|
|
relevant_files |= directories_for(relevant_files)
|
2015-01-08 06:07:32 +08:00
|
|
|
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):
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'Case-insensitivity conflict found: {filename}')
|
2015-01-08 06:07:32 +08:00
|
|
|
retv = 1
|
|
|
|
|
|
|
|
return retv
|
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2015-01-08 06:07:32 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
'filenames', nargs='*',
|
2017-07-13 09:35:24 +08:00
|
|
|
help='Filenames pre-commit believes are changed.',
|
2015-01-08 06:07:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
return find_conflicting_filenames(args.filenames)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|