mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 18:16:52 +00:00
Merge pull request #152 from pre-commit/forbid_new_submodules
Add a new hook to forbid new submodules
This commit is contained in:
commit
7bcb6a92bf
9 changed files with 76 additions and 7 deletions
|
|
@ -5,7 +5,6 @@ env: # These should match the tox env list
|
|||
- TOXENV=py34
|
||||
- TOXENV=py35
|
||||
- TOXENV=pypy
|
||||
- TOXENV=pypy3
|
||||
install: pip install coveralls tox
|
||||
script: tox
|
||||
# Special snowflake. Our tests depend on making real commits.
|
||||
|
|
|
|||
|
|
@ -34,5 +34,6 @@ def main():
|
|||
shutil.copyfileobj(src_file, dest_file)
|
||||
os.chmod(DEST_PATH, 0o755)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ from __future__ import unicode_literals
|
|||
import argparse
|
||||
import math
|
||||
import os
|
||||
import sys
|
||||
|
||||
from pre_commit_hooks.util import added_files
|
||||
from pre_commit_hooks.util import CalledProcessError
|
||||
|
|
@ -49,8 +48,6 @@ def find_large_added_files(filenames, maxkb):
|
|||
|
||||
|
||||
def main(argv=None):
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'filenames', nargs='*',
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ from __future__ import print_function
|
|||
|
||||
import argparse
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
CONFLICT_PATTERNS = [
|
||||
b'<<<<<<< ',
|
||||
|
|
@ -41,5 +40,6 @@ def detect_merge_conflict(argv=None):
|
|||
|
||||
return retcode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(detect_merge_conflict())
|
||||
exit(detect_merge_conflict())
|
||||
|
|
|
|||
|
|
@ -67,5 +67,6 @@ def main(argv=None):
|
|||
else:
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
|
|
|
|||
|
|
@ -138,5 +138,6 @@ def main(argv=None):
|
|||
|
||||
return retv
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
|
|
|
|||
31
pre_commit_hooks/forbid_new_submodules.py
Normal file
31
pre_commit_hooks/forbid_new_submodules.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
# `argv` is ignored, pre-commit will send us a list of files that we
|
||||
# don't care about
|
||||
added_diff = cmd_output(
|
||||
'git', 'diff', '--staged', '--diff-filter=A', '--raw',
|
||||
)
|
||||
retv = 0
|
||||
for line in added_diff.splitlines():
|
||||
metadata, filename = line.split('\t', 1)
|
||||
new_mode = metadata.split(' ')[1]
|
||||
if new_mode == '160000':
|
||||
print('{}: new submodule introduced'.format(filename))
|
||||
retv = 1
|
||||
|
||||
if retv:
|
||||
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__':
|
||||
exit(main())
|
||||
39
tests/forbid_new_submodules_test.py
Normal file
39
tests/forbid_new_submodules_test.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import pytest
|
||||
from pre_commit.util import cmd_output
|
||||
|
||||
from pre_commit_hooks.forbid_new_submodules import main
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def git_dir_with_git_dir(tmpdir):
|
||||
with tmpdir.as_cwd():
|
||||
cmd_output('git', 'init', '.')
|
||||
cmd_output('git', 'commit', '-m', 'init', '--allow-empty')
|
||||
cmd_output('git', 'init', 'foo')
|
||||
with tmpdir.join('foo').as_cwd():
|
||||
cmd_output('git', 'commit', '-m', 'init', '--allow-empty')
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'cmd',
|
||||
(
|
||||
# Actually add the submodule
|
||||
('git', 'submodule', 'add', './foo'),
|
||||
# Sneaky submodule add (that doesn't show up in .gitmodules)
|
||||
('git', 'add', 'foo'),
|
||||
),
|
||||
)
|
||||
def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
||||
cmd_output(*cmd)
|
||||
assert main() == 1
|
||||
out, _ = capsys.readouterr()
|
||||
assert out.startswith('foo: new submodule introduced\n')
|
||||
|
||||
|
||||
def test_main_no_new_submodule(git_dir_with_git_dir):
|
||||
open('test.py', 'a+').close()
|
||||
cmd_output('git', 'add', 'test.py')
|
||||
assert main() == 0
|
||||
2
tox.ini
2
tox.ini
|
|
@ -1,7 +1,7 @@
|
|||
[tox]
|
||||
project = pre_commit_hooks
|
||||
# These should match the travis env list
|
||||
envlist = py27,py34,py35,pypy,pypy3
|
||||
envlist = py27,py34,py35,pypy
|
||||
|
||||
[testenv]
|
||||
deps = -rrequirements-dev.txt
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue