| 8 | |
| 9 | |
| 10 | def main(argv: Sequence[str] | None = None) -> int: |
| 11 | parser = argparse.ArgumentParser() |
| 12 | parser.add_argument('filenames', nargs='*') |
| 13 | args = parser.parse_args(argv) |
| 14 | |
| 15 | if ( |
| 16 | 'PRE_COMMIT_FROM_REF' in os.environ and |
| 17 | 'PRE_COMMIT_TO_REF' in os.environ |
| 18 | ): |
| 19 | diff_arg = '...'.join(( |
| 20 | os.environ['PRE_COMMIT_FROM_REF'], |
| 21 | os.environ['PRE_COMMIT_TO_REF'], |
| 22 | )) |
| 23 | else: |
| 24 | diff_arg = '--staged' |
| 25 | added_diff = cmd_output( |
| 26 | 'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--', |
| 27 | *args.filenames, |
| 28 | ) |
| 29 | retv = 0 |
| 30 | for line in added_diff.splitlines(): |
| 31 | metadata, filename = line.split('\t', 1) |
| 32 | new_mode = metadata.split(' ')[1] |
| 33 | if new_mode == '160000': |
| 34 | print(f'{filename}: new submodule introduced') |
| 35 | retv = 1 |
| 36 | |
| 37 | if retv: |
| 38 | print() |
| 39 | print('This commit introduces new submodules.') |
| 40 | print('Did you unintentionally `git add .`?') |
| 41 | print('To fix: git rm {thesubmodule} # no trailing slash') |
| 42 | print('Also check .gitmodules') |
| 43 | |
| 44 | return retv |
| 45 | |
| 46 | |
| 47 | if __name__ == '__main__': |