| Total Complexity | 6 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import subprocess |
||
| 2 | import tempfile |
||
| 3 | from typing import Optional |
||
| 4 | |||
| 5 | |||
| 6 | def git_diff_head(staged=False) -> str: |
||
| 7 | staged_arg = [] |
||
| 8 | if staged: |
||
| 9 | staged_arg = ["--staged"] |
||
| 10 | args = ["git", "diff"] + staged_arg + ["HEAD", "--", ".", ":!Pipfile.lock"] |
||
| 11 | return subprocess.check_output(args, text=True) |
||
| 12 | |||
| 13 | |||
| 14 | def git_commit(message: str) -> None: |
||
| 15 | """Invoke git commit, allowing user to edit""" |
||
| 16 | with tempfile.NamedTemporaryFile(mode="w+t", delete=True) as f: |
||
| 17 | f.write(message) |
||
| 18 | f.seek(0) |
||
| 19 | args = ["git", "commit", "--allow-empty", "--template", f.name] |
||
| 20 | subprocess.run(args, check=True) |
||
| 21 | |||
| 22 | |||
| 23 | def git_show_top_level() -> Optional[str]: |
||
| 24 | try: |
||
| 25 | args = ["git", "rev-parse", "--show-toplevel"] |
||
| 26 | return subprocess.check_output( |
||
| 27 | args, text=True, stderr=subprocess.DEVNULL |
||
| 28 | ).strip() |
||
| 29 | except subprocess.CalledProcessError: |
||
| 30 | return None |
||
| 31 |