Merge branch 'use_git_config_in_hook' into 'master'

Use git config in hook

Get parameter's value using `git config` before trying `os.environ`.

See merge request !20
This commit is contained in:
Ian Cordasco 2015-02-23 03:58:52 +00:00
commit eae9a6a3db
2 changed files with 49 additions and 17 deletions

View file

@ -14,21 +14,21 @@ In the case of Git, the hook won't be installed if a custom
``pre-commit`` hook file is already present in ``pre-commit`` hook file is already present in
the ``.git/hooks`` directory. the ``.git/hooks`` directory.
You can control the behavior of the pre-commit hook using environment You can control the behavior of the pre-commit hook using configuration file
variables: settings or environment variables:
``FLAKE8_COMPLEXITY`` ``flake8.complexity`` or ``FLAKE8_COMPLEXITY``
Any value > 0 enables complexity checking with McCabe. (defaults Any value > 0 enables complexity checking with McCabe. (defaults
to 10) to 10)
``FLAKE8_STRICT`` ``flake8.strict`` or ``FLAKE8_STRICT``
If True, this causes the commit to fail in case of any errors at If True, this causes the commit to fail in case of any errors at
all. (defaults to False) all. (defaults to False)
``FLAKE8_IGNORE`` ``flake8.ignore`` or ``FLAKE8_IGNORE``
Comma-separated list of errors and warnings to ignore. (defaults to Comma-separated list of errors and warnings to ignore. (defaults to
empty) empty)
``FLAKE8_LAZY`` ``flake8.lazy`` or ``FLAKE8_LAZY``
If True, also scans those files not added to the index before If True, also scans those files not added to the index before
commit. (defaults to False) commit. (defaults to False)

View file

@ -128,11 +128,10 @@ def run(command, raw_output=False, decode=True):
# endswith to be given a bytes object or a tuple of bytes but not native # endswith to be given a bytes object or a tuple of bytes but not native
# string objects. This is simply less mysterious than using b'.py' in the # string objects. This is simply less mysterious than using b'.py' in the
# endswith method. That should work but might still fail horribly. # endswith method. That should work but might still fail horribly.
if hasattr(stdout, 'decode'): if decode:
if decode: if hasattr(stdout, 'decode'):
stdout = stdout.decode('utf-8') stdout = stdout.decode('utf-8')
if hasattr(stderr, 'decode'): if hasattr(stderr, 'decode'):
if decode:
stderr = stderr.decode('utf-8') stderr = stderr.decode('utf-8')
if not raw_output: if not raw_output:
stdout = [line.strip() for line in stdout.splitlines()] stdout = [line.strip() for line in stdout.splitlines()]
@ -164,16 +163,49 @@ def find_vcs():
return '' return ''
def get_git_config(option, type='', convert_type=True):
# type can be --bool, --int or an empty string
_, git_cfg_value, _ = run('git config --get %s %s' % (type, option),
raw_output=True)
git_cfg_value = git_cfg_value.strip()
if not convert_type:
return git_cfg_value
if type == '--bool':
git_cfg_value = git_cfg_value == 'true'
elif git_cfg_value and type == '--int':
git_cfg_value = int(git_cfg_value)
return git_cfg_value
_params = dict([
('FLAKE8_COMPLEXITY', '--int'),
('FLAKE8_STRICT', '--bool'),
('FLAKE8_IGNORE', ''),
('FLAKE8_LAZY', '--bool'),
])
def get_git_param(option, default=''):
type = _params[option]
param_value = get_git_config(option.lower().replace('_', '.'),
type=type, convert_type=False)
if param_value == '':
param_value = os.getenv(option, default)
if (type == '--bool') and not isinstance(param_value, bool):
param_value = param_value.lower() == 'true'
elif param_value and type == '--int':
param_value = int(param_value)
return param_value
git_hook_file = """#!/usr/bin/env python git_hook_file = """#!/usr/bin/env python
import sys import sys
import os from flake8.hooks import git_hook, get_git_param
from flake8.hooks import git_hook
COMPLEXITY = os.getenv('FLAKE8_COMPLEXITY', 10)
STRICT = os.getenv('FLAKE8_STRICT', False)
IGNORE = os.getenv('FLAKE8_IGNORE')
LAZY = os.getenv('FLAKE8_LAZY', False)
COMPLEXITY = get_git_param('FLAKE8_COMPLEXITY', 10)
STRICT = get_git_param('FLAKE8_STRICT', False)
IGNORE = get_git_param('FLAKE8_IGNORE')
LAZY = get_git_param('FLAKE8_LAZY', False)
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(git_hook( sys.exit(git_hook(