From 307d578d739eb254ef3000fdde94271af9b8923e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 30 Jan 2022 12:02:58 +0100 Subject: Adding upstream version 4.1.0. Signed-off-by: Daniel Baumann --- pre_commit_hooks/util.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pre_commit_hooks/util.py (limited to 'pre_commit_hooks/util.py') diff --git a/pre_commit_hooks/util.py b/pre_commit_hooks/util.py new file mode 100644 index 0000000..402e33e --- /dev/null +++ b/pre_commit_hooks/util.py @@ -0,0 +1,33 @@ +import subprocess +from typing import Any +from typing import List +from typing import Optional +from typing import Set + + +class CalledProcessError(RuntimeError): + pass + + +def added_files() -> Set[str]: + cmd = ('git', 'diff', '--staged', '--name-only', '--diff-filter=A') + return set(cmd_output(*cmd).splitlines()) + + +def cmd_output(*cmd: str, retcode: Optional[int] = 0, **kwargs: Any) -> str: + kwargs.setdefault('stdout', subprocess.PIPE) + kwargs.setdefault('stderr', subprocess.PIPE) + proc = subprocess.Popen(cmd, **kwargs) + stdout, stderr = proc.communicate() + stdout = stdout.decode() + if retcode is not None and proc.returncode != retcode: + raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr) + return stdout + + +def zsplit(s: str) -> List[str]: + s = s.strip('\0') + if s: + return s.split('\0') + else: + return [] -- cgit v1.2.3