2017-06-23 15:33:13 +08:00
|
|
|
"""
|
|
|
|
A very simple pre-commit hook that, when passed one or more filenames
|
|
|
|
as arguments, will sort the lines in those files.
|
|
|
|
|
2020-06-17 23:27:11 +08:00
|
|
|
An example use case for this: you have a deploy-allowlist.txt file
|
2017-06-23 15:33:13 +08:00
|
|
|
in a repo that contains a list of filenames that is used to specify
|
|
|
|
files to be included in a docker container. This file has one filename
|
|
|
|
per line. Various users are adding/removing lines from this file; using
|
|
|
|
this hook on that file should reduce the instances of git merge
|
|
|
|
conflicts and keep the file nicely ordered.
|
|
|
|
"""
|
2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-06-23 15:33:13 +08:00
|
|
|
import argparse
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Iterable
|
|
|
|
from collections.abc import Sequence
|
2020-08-27 08:28:26 +08:00
|
|
|
from typing import Any
|
|
|
|
from typing import Callable
|
2019-02-01 11:19:10 +08:00
|
|
|
from typing import IO
|
2017-06-23 15:33:13 +08:00
|
|
|
|
|
|
|
PASS = 0
|
|
|
|
FAIL = 1
|
|
|
|
|
|
|
|
|
2020-08-27 08:28:26 +08:00
|
|
|
def sort_file_contents(
|
|
|
|
f: IO[bytes],
|
2022-01-16 08:24:05 +08:00
|
|
|
key: Callable[[bytes], Any] | None,
|
2020-10-25 12:05:40 +08:00
|
|
|
*,
|
|
|
|
unique: bool = False,
|
2020-08-27 08:28:26 +08:00
|
|
|
) -> int:
|
2017-06-26 10:51:50 +08:00
|
|
|
before = list(f)
|
2020-10-25 12:05:40 +08:00
|
|
|
lines: Iterable[bytes] = (
|
|
|
|
line.rstrip(b'\n\r') for line in before if line.strip()
|
2020-08-27 08:28:26 +08:00
|
|
|
)
|
2020-10-25 12:05:40 +08:00
|
|
|
if unique:
|
|
|
|
lines = set(lines)
|
|
|
|
after = sorted(lines, key=key)
|
2017-06-26 05:40:03 +08:00
|
|
|
|
2017-06-26 10:51:50 +08:00
|
|
|
before_string = b''.join(before)
|
2023-08-06 04:56:46 +08:00
|
|
|
after_string = b'\n'.join(after)
|
|
|
|
|
|
|
|
if after_string:
|
|
|
|
after_string += b'\n'
|
2017-06-26 05:40:03 +08:00
|
|
|
|
2017-06-26 10:51:50 +08:00
|
|
|
if before_string == after_string:
|
|
|
|
return PASS
|
|
|
|
else:
|
|
|
|
f.seek(0)
|
|
|
|
f.write(after_string)
|
|
|
|
f.truncate()
|
|
|
|
return FAIL
|
2017-06-23 15:33:13 +08:00
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2017-06-23 15:33:13 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('filenames', nargs='+', help='Files to sort')
|
2024-10-06 12:57:18 +08:00
|
|
|
|
|
|
|
mutex = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
mutex.add_argument(
|
2020-08-27 08:28:26 +08:00
|
|
|
'--ignore-case',
|
|
|
|
action='store_const',
|
|
|
|
const=bytes.lower,
|
|
|
|
default=None,
|
|
|
|
help='fold lower case to upper case characters',
|
|
|
|
)
|
2024-10-06 12:57:18 +08:00
|
|
|
mutex.add_argument(
|
2020-10-25 12:05:40 +08:00
|
|
|
'--unique',
|
|
|
|
action='store_true',
|
|
|
|
help='ensure each line is unique',
|
|
|
|
)
|
2024-10-06 12:57:18 +08:00
|
|
|
|
2017-06-23 15:33:13 +08:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
retv = PASS
|
|
|
|
|
|
|
|
for arg in args.filenames:
|
|
|
|
with open(arg, 'rb+') as file_obj:
|
2020-10-25 12:05:40 +08:00
|
|
|
ret_for_file = sort_file_contents(
|
|
|
|
file_obj, key=args.ignore_case, unique=args.unique,
|
|
|
|
)
|
2017-06-23 15:33:13 +08:00
|
|
|
|
|
|
|
if ret_for_file:
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'Sorting {arg}')
|
2017-06-23 15:33:13 +08:00
|
|
|
|
|
|
|
retv |= ret_for_file
|
|
|
|
|
|
|
|
return retv
|
2019-08-04 00:46:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|