mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-14 09:27:21 +08:00
28 lines
820 B
Python
28 lines
820 B
Python
from __future__ import absolute_import
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
import subprocess
|
|
|
|
|
|
class CalledProcessError(RuntimeError):
|
|
pass
|
|
|
|
|
|
def added_files():
|
|
return set(cmd_output(
|
|
'git', 'diff', '--staged', '--name-only', '--diff-filter', 'A',
|
|
).splitlines())
|
|
|
|
|
|
def cmd_output(*cmd, **kwargs):
|
|
retcode = kwargs.pop('retcode', 0)
|
|
popen_kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
|
|
popen_kwargs.update(kwargs)
|
|
proc = subprocess.Popen(cmd, **popen_kwargs)
|
|
stdout, stderr = proc.communicate()
|
|
stdout, stderr = stdout.decode('UTF-8'), stderr.decode('UTF-8')
|
|
if retcode is not None and proc.returncode != retcode:
|
|
raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
|
|
return stdout
|