• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adamws / kicad-git / b4407247-595e-43ba-a475-674279b678f7

19 Dec 2024 08:47PM UTC coverage: 93.431% (+1.2%) from 92.248%
b4407247-595e-43ba-a475-674279b678f7

push

circleci

adamws
Check if git gui subprocess unexpectedly terminated

8 of 11 new or added lines in 1 file covered. (72.73%)

128 of 137 relevant lines covered (93.43%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/source/git.py
1
import configparser
1✔
2
import os
1✔
3
import platform
1✔
4
import shlex
1✔
5
import subprocess
1✔
6
import sys
1✔
7
from pathlib import Path
1✔
8
from typing import Optional
1✔
9

10
if platform.system() == "Darwin":
1✔
11
    DEFAULT_CONFIG = """[paths]
×
12
        git = /opt/homebrew/bin/git
13
        git_gui = /opt/homebrew/bin/git gui"""
14
else:
15
    DEFAULT_CONFIG = """[paths]
1✔
16
        git = git
17
        git_gui = git gui"""
18

19
config = configparser.ConfigParser()
1✔
20
config.read_string(DEFAULT_CONFIG)
1✔
21
config.read(os.path.dirname(__file__) + "/config.ini")
1✔
22

23
git: str = config["paths"]["git"]
1✔
24
git_gui: str = config["paths"]["git_gui"]
1✔
25

26

27
def __run(command: str, cwd: Path) -> str:
1✔
28
    args = f"{git} {command}"
1✔
29
    if sys.platform != "win32":
1✔
30
        args = shlex.split(args)
1✔
31
    process = subprocess.Popen(
1✔
32
        args,
33
        cwd=cwd,
34
        stdout=subprocess.PIPE,
35
        stderr=subprocess.STDOUT,
36
        universal_newlines=True,
37
    )
38
    process.wait()
1✔
39
    output = ""
1✔
40
    if process.stdout:
1✔
41
        output = process.stdout.read()
1✔
42
    if process.returncode == 0:
1✔
43
        return output
1✔
44
    error = f"Error: Failed to '{git} {command}', received: {output}"
1✔
45
    raise RuntimeError(error)
1✔
46

47

48
def version() -> str:
1✔
49
    return __run("version", Path(".")).strip("\n")
1✔
50

51

52
def toplevel(path: Path) -> Optional[Path]:
1✔
53
    if path.exists():
1✔
54
        try:
1✔
55
            result = __run("rev-parse --show-toplevel", path).strip()
1✔
56
            if result:
1✔
57
                return Path(result)
1✔
58
        except RuntimeError:
1✔
59
            pass
1✔
60
    return None
1✔
61

62

63
def guitool(repo_dir: Path) -> subprocess.Popen:
1✔
64
    args = git_gui
1✔
65
    if sys.platform != "win32":
1✔
66
        args = shlex.split(args)
1✔
67
    process = subprocess.Popen(args, cwd=repo_dir)
1✔
68
    try:
1✔
69
        # using short timeout because we do not want to freeze kicad
70
        # window when git gui running. This exist only to check
71
        # if we terminated unexpectedly.
72
        process.wait(timeout=1)
1✔
NEW
73
    except subprocess.TimeoutExpired:
×
NEW
74
        pass  # this is ok, process still running
×
75
    else:
76
        if process.returncode != 0:
1✔
77
            # this means that something has failed,
78
            # for example git gui not installed.
79
            error = f"Error: Failed to run '{git_gui}'"
1✔
80
            raise RuntimeError(error)
1✔
NEW
81
    return process
×
82

83

84
__all__ = ["version", "toplevel", "guitool"]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc