menderbot.git_client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 6

3 Functions

Rating   Name   Duplication   Size   Complexity  
A git_commit() 0 7 2
A git_diff_head() 0 6 2
A git_show_top_level() 0 8 2
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