forbid-new-submodules: fix triggering failure when only a submodule is committed (without any other file); support --from-ref and --to-ref; fixes #609

This commit is contained in:
Mikhail Khvoinitsky 2021-06-23 03:10:13 +03:00
parent 8c1183c0c8
commit 10c5e4e166
9 changed files with 65 additions and 25 deletions

View file

@ -1,3 +1,5 @@
import argparse
import os
from typing import Optional
from typing import Sequence
@ -5,10 +7,23 @@ from pre_commit_hooks.util import cmd_output
def main(argv: Optional[Sequence[str]] = None) -> int:
# `argv` is ignored, pre-commit will send us a list of files that we
# don't care about
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
if (
'PRE_COMMIT_FROM_REF' in os.environ and
'PRE_COMMIT_TO_REF' in os.environ
):
diff_arg = '...'.join((
os.environ['PRE_COMMIT_FROM_REF'],
os.environ['PRE_COMMIT_TO_REF'],
))
else:
diff_arg = '--staged'
added_diff = cmd_output(
'git', 'diff', '--staged', '--diff-filter=A', '--raw',
'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--',
*args.filenames,
)
retv = 0
for line in added_diff.splitlines():