Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 3 | import argparse |
| 4 | import os |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 5 | from typing import Sequence |
| 6 | |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 7 | from pre_commit_hooks.util import cmd_output |
| 8 | |
| 9 | |
Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 10 | def main(argv: Sequence[str] | None = None) -> int: |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 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' |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 25 | added_diff = cmd_output( |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 26 | 'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--', |
| 27 | *args.filenames, |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 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': |
Anthony Sottile | f5c42a0 | 2020-02-05 11:10:42 -0800 | [diff] [blame] | 34 | print(f'{filename}: new submodule introduced') |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 35 | retv = 1 |
| 36 | |
| 37 | if retv: |
Chris Kuehl | 4b928ab | 2016-11-30 10:10:29 -0800 | [diff] [blame] | 38 | print() |
Anthony Sottile | 70e405e | 2016-11-30 09:56:42 -0800 | [diff] [blame] | 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__': |
Anthony Sottile | 39ab2ed | 2021-10-23 13:23:50 -0400 | [diff] [blame] | 48 | raise SystemExit(main()) |