feat: show actual current branch in settings menu (#588)

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-10-24 12:26:18 +02:00
committed by GitHub
parent 4427ae94af
commit 02ed3e7da0
4 changed files with 65 additions and 40 deletions

View File

@@ -11,6 +11,8 @@ from __future__ import annotations
import textwrap
from typing import Literal, Tuple, Type
from components.klipper.klipper_utils import get_klipper_status
from components.moonraker.moonraker_utils import get_moonraker_status
from core.constants import COLOR_CYAN, COLOR_GREEN, RESET_FORMAT
from core.logger import DialogType, Logger
from core.menus import Option
@@ -26,8 +28,8 @@ class SettingsMenu(BaseMenu):
def __init__(self, previous_menu: Type[BaseMenu] | None = None) -> None:
super().__init__()
self.previous_menu: Type[BaseMenu] | None = previous_menu
self.klipper_repo: str | None = None
self.moonraker_repo: str | None = None
self.klipper_status = get_klipper_status()
self.moonraker_status = get_moonraker_status()
self.mainsail_unstable: bool | None = None
self.fluidd_unstable: bool | None = None
self.auto_backups_enabled: bool | None = None
@@ -49,31 +51,41 @@ class SettingsMenu(BaseMenu):
def print_menu(self) -> None:
header = " [ KIAUH Settings ] "
color = COLOR_CYAN
count = 62 - len(color) - len(RESET_FORMAT)
checked = f"[{COLOR_GREEN}x{RESET_FORMAT}]"
color, rst = COLOR_CYAN, RESET_FORMAT
count = 62 - len(color) - len(rst)
checked = f"[{COLOR_GREEN}x{rst}]"
unchecked = "[ ]"
kl_repo: str = f"{color}{self.klipper_status.repo}{rst}"
kl_branch: str = f"{color}{self.klipper_status.branch}{rst}"
kl_owner: str = f"{color}{self.klipper_status.owner}{rst}"
mr_repo: str = f"{color}{self.moonraker_status.repo}{rst}"
mr_branch: str = f"{color}{self.moonraker_status.branch}{rst}"
mr_owner: str = f"{color}{self.moonraker_status.owner}{rst}"
o1 = checked if self.mainsail_unstable else unchecked
o2 = checked if self.fluidd_unstable else unchecked
o3 = checked if self.auto_backups_enabled else unchecked
menu = textwrap.dedent(
f"""
╔═══════════════════════════════════════════════════════╗
{color}{header:~^{count}}{RESET_FORMAT}
{color}{header:~^{count}}{rst}
╟───────────────────────────────────────────────────────╢
║ Klipper source repository:
{self.klipper_repo:<67}
Moonraker source repository:
║ ● {self.moonraker_repo:<67}
Install unstable Webinterface releases:
║ Klipper:
● Repo: {kl_repo:51}
● Owner: {kl_owner:51}
● Branch: {kl_branch:51}
╟───────────────────────────────────────────────────────╢
Moonraker:
● Repo: {mr_repo:51}
║ ● Owner: {mr_owner:51}
║ ● Branch: {mr_branch:51}
╟───────────────────────────────────────────────────────╢
║ Install unstable releases: ║
{o1} Mainsail ║
{o2} Fluidd ║
║ ║
╟───────────────────────────────────────────────────────╢
║ Auto-Backup: ║
{o3} Automatic backup before update ║
║ ║
╟───────────────────────────────────────────────────────╢
║ 1) Set Klipper source repository ║
║ 2) Set Moonraker source repository ║
@@ -89,25 +101,10 @@ class SettingsMenu(BaseMenu):
def _load_settings(self) -> None:
self.settings = KiauhSettings()
self._format_repo_str("klipper")
self._format_repo_str("moonraker")
self.auto_backups_enabled = self.settings.kiauh.backup_before_update
self.mainsail_unstable = self.settings.mainsail.unstable_releases
self.fluidd_unstable = self.settings.fluidd.unstable_releases
def _format_repo_str(self, repo_name: Literal["klipper", "moonraker"]) -> None:
repo: RepoSettings = self.settings[repo_name]
repo_str = f"{'/'.join(repo.repo_url.rsplit('/', 2)[-2:])}"
branch_str = f"({COLOR_CYAN}@ {repo.branch}{RESET_FORMAT})"
setattr(
self,
f"{repo_name}_repo",
f"{COLOR_CYAN}{repo_str}{RESET_FORMAT} {branch_str}",
)
def _gather_input(self) -> Tuple[str, str]:
Logger.print_dialog(
DialogType.ATTENTION,

View File

@@ -25,6 +25,7 @@ class ComponentStatus:
status: StatusCode
owner: str | None = None
repo: str | None = None
branch: str = ""
local: str | None = None
remote: str | None = None
instances: int | None = None

View File

@@ -24,6 +24,7 @@ from core.constants import (
from core.logger import DialogType, Logger
from core.types import ComponentStatus, StatusCode
from utils.git_utils import (
get_current_branch,
get_local_commit,
get_local_tags,
get_remote_commit,
@@ -103,7 +104,12 @@ def get_install_status(
"""
from utils.instance_utils import get_instances
checks = [repo_dir.exists()]
checks = []
branch: str = ""
if repo_dir.exists():
checks.append(True)
branch = get_current_branch(repo_dir)
if env_dir is not None:
checks.append(env_dir.exists())
@@ -131,6 +137,7 @@ def get_install_status(
instances=instances,
owner=org,
repo=repo,
branch=branch,
local=get_local_commit(repo_dir),
remote=get_remote_commit(repo_dir),
)

View File

@@ -87,12 +87,29 @@ def get_repo_name(repo: Path) -> Tuple[str, str]:
orga: str = substrings[0] if substrings[0] else "-"
name: str = substrings[1] if substrings[1] else "-"
return orga, name
return orga, name.replace(".git", "")
except CalledProcessError:
return "-", "-"
def get_current_branch(repo: Path) -> str:
"""
Get the current branch of a local Git repository
:param repo: Path to the local Git repository
:return: Current branch
"""
try:
cmd = ["git", "branch", "--show-current"]
result: str = check_output(cmd, stderr=DEVNULL, cwd=repo).decode(
encoding="utf-8"
)
return result.strip()
except CalledProcessError:
return ""
def get_local_tags(repo_path: Path, _filter: str | None = None) -> List[str]:
"""
Get all tags of a local Git repository
@@ -209,8 +226,8 @@ def get_local_commit(repo: Path) -> str | None:
return None
try:
cmd = f"cd {repo} && git describe HEAD --always --tags | cut -d '-' -f 1,2"
return check_output(cmd, shell=True, text=True).strip()
cmd = "git describe HEAD --always --tags | cut -d '-' -f 1,2"
return check_output(cmd, shell=True, text=True, cwd=repo).strip()
except CalledProcessError:
return None
@@ -220,12 +237,15 @@ def get_remote_commit(repo: Path) -> str | None:
return None
try:
# get locally checked out branch
branch_cmd = f"cd {repo} && git branch | grep -E '\*'"
branch = check_output(branch_cmd, shell=True, text=True)
branch = branch.split("*")[-1].strip()
cmd = f"cd {repo} && git describe 'origin/{branch}' --always --tags | cut -d '-' -f 1,2"
return check_output(cmd, shell=True, text=True).strip()
branch = get_current_branch(repo)
cmd = f"git describe 'origin/{branch}' --always --tags | cut -d '-' -f 1,2"
return check_output(
cmd,
shell=True,
text=True,
cwd=repo,
stderr=DEVNULL,
).strip()
except CalledProcessError:
return None