2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-06-23 08:10:13 +08:00
|
|
|
import argparse
|
|
|
|
import os
|
2024-10-12 07:30:07 +08:00
|
|
|
from collections.abc import Sequence
|
2019-02-01 11:19:10 +08:00
|
|
|
|
2016-12-01 01:56:42 +08:00
|
|
|
from pre_commit_hooks.util import cmd_output
|
|
|
|
|
|
|
|
|
2022-01-16 08:24:05 +08:00
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
2021-06-23 08:10:13 +08:00
|
|
|
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'
|
2016-12-01 01:56:42 +08:00
|
|
|
added_diff = cmd_output(
|
2021-06-23 08:10:13 +08:00
|
|
|
'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--',
|
|
|
|
*args.filenames,
|
2016-12-01 01:56:42 +08:00
|
|
|
)
|
|
|
|
retv = 0
|
|
|
|
for line in added_diff.splitlines():
|
|
|
|
metadata, filename = line.split('\t', 1)
|
|
|
|
new_mode = metadata.split(' ')[1]
|
|
|
|
if new_mode == '160000':
|
2020-02-06 03:10:42 +08:00
|
|
|
print(f'{filename}: new submodule introduced')
|
2016-12-01 01:56:42 +08:00
|
|
|
retv = 1
|
|
|
|
|
|
|
|
if retv:
|
2016-12-01 02:10:29 +08:00
|
|
|
print()
|
2016-12-01 01:56:42 +08:00
|
|
|
print('This commit introduces new submodules.')
|
|
|
|
print('Did you unintentionally `git add .`?')
|
|
|
|
print('To fix: git rm {thesubmodule} # no trailing slash')
|
|
|
|
print('Also check .gitmodules')
|
|
|
|
|
|
|
|
return retv
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-10-24 01:23:50 +08:00
|
|
|
raise SystemExit(main())
|