blob: d6c90ae02f7b07f10232fb6d00fb6d2bf9e8784f [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile713fab42015-03-20 13:52:21 -07003import subprocess
Anthony Sottile030bfac2019-01-31 19:19:10 -08004from typing import Any
Anthony Sottile713fab42015-03-20 13:52:21 -07005
6
7class CalledProcessError(RuntimeError):
8 pass
gkiselc682b502015-01-07 14:07:32 -08009
10
Anthony Sottile8f615292022-01-15 19:24:05 -050011def added_files() -> set[str]:
Anthony Sottilefea76b92020-02-03 08:41:48 -080012 cmd = ('git', 'diff', '--staged', '--name-only', '--diff-filter=A')
13 return set(cmd_output(*cmd).splitlines())
Anthony Sottile713fab42015-03-20 13:52:21 -070014
15
Anthony Sottile8f615292022-01-15 19:24:05 -050016def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
Anthony Sottile030bfac2019-01-31 19:19:10 -080017 kwargs.setdefault('stdout', subprocess.PIPE)
18 kwargs.setdefault('stderr', subprocess.PIPE)
19 proc = subprocess.Popen(cmd, **kwargs)
Anthony Sottile713fab42015-03-20 13:52:21 -070020 stdout, stderr = proc.communicate()
Anthony Sottilef5c42a02020-02-05 11:10:42 -080021 stdout = stdout.decode()
Anthony Sottile713fab42015-03-20 13:52:21 -070022 if retcode is not None and proc.returncode != retcode:
23 raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
24 return stdout
Mikhail Khvoinitsky1e87d592020-08-02 21:25:07 +030025
26
Anthony Sottile8f615292022-01-15 19:24:05 -050027def zsplit(s: str) -> list[str]:
Mikhail Khvoinitsky1e87d592020-08-02 21:25:07 +030028 s = s.strip('\0')
29 if s:
30 return s.split('\0')
31 else:
32 return []