text
stringlengths
0
828
""""""Hook: for checking commit message (prevent commit).""""""
with open(argv[1], ""r"", ""utf-8"") as fh:
message = ""\n"".join(filter(lambda x: not x.startswith(""#""),
fh.readlines()))
options = {""allow_empty"": True}
if not _check_message(message, options):
click.echo(
""Aborting commit due to commit message errors (override with ""
""'git commit --no-verify')."", file=sys.stderr)
raise click.Abort
return 0"
980,"def post_commit_hook(argv):
""""""Hook: for checking commit message.""""""
_, stdout, _ = run(""git log -1 --format=%B HEAD"")
message = ""\n"".join(stdout)
options = {""allow_empty"": True}
if not _check_message(message, options):
click.echo(
""Commit message errors (fix with 'git commit --amend')."",
file=sys.stderr)
return 1 # it should not fail with exit
return 0"
981,"def _read_local_kwalitee_configuration(directory="".""):
""""""Check if the repo has a ``.kwalitee.yaml`` file.""""""
filepath = os.path.abspath(os.path.join(directory, '.kwalitee.yml'))
data = {}
if os.path.exists(filepath):
with open(filepath, 'r') as file_read:
data = yaml.load(file_read.read())
return data"
982,"def _pre_commit(files, options):
""""""Run the check on files of the added version.
They might be different than the one on disk. Equivalent than doing a git
stash, check, and git stash pop.
""""""
errors = []
tmpdir = mkdtemp()
files_to_check = []
try:
for (file_, content) in files:
# write staged version of file to temporary directory
dirname, filename = os.path.split(os.path.abspath(file_))
prefix = os.path.commonprefix([dirname, tmpdir])
dirname = os.path.relpath(dirname, start=prefix)
dirname = os.path.join(tmpdir, dirname)
if not os.path.isdir(dirname):
os.makedirs(dirname)
filename = os.path.join(dirname, filename)
with open(filename, ""wb"") as fh:
fh.write(content)
files_to_check.append((file_, filename))
for (file_, filename) in files_to_check:
errors += list(map(lambda x: ""{0}: {1}"".format(file_, x),
check_file(filename, **options) or []))
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
return errors"
983,"def pre_commit_hook(argv):
""""""Hook: checking the staged files.""""""
options = get_options()
# Check if the repo has a configuration repo
options.update(_read_local_kwalitee_configuration())
files = []
for filename in _get_files_modified():
# get the staged version of the file and
# write the staged version to temp dir with its full path to
# avoid overwriting files with the same name
_, stdout, _ = run(""git show :{0}"".format(filename), raw_output=True)
files.append((filename, stdout))
errors = _pre_commit(files, options)
for error in errors:
if hasattr(error, ""decode""):
error = error.decode()
click.echo(error, file=sys.stderr)
if errors:
click.echo(
""Aborting commit due to kwalitee errors (override with ""
""'git commit --no-verify')."",
file=sys.stderr)
raise click.Abort
return 0"
984,"def run(command, raw_output=False):
""""""Run a command using subprocess.
:param command: command line to be run
:type command: str
:param raw_output: does not attempt to convert the output as unicode
:type raw_output: bool
:return: error code, output (``stdout``) and error (``stderr``)
:rtype: tuple