- Writing custom git flow scripts - finishing up without extensive testing

This commit is contained in:
Josako
2025-12-11 09:47:19 +01:00
parent fe9fc047ff
commit 2c8347c91b
8 changed files with 389 additions and 24 deletions

View File

@@ -3,7 +3,7 @@ from __future__ import annotations
import subprocess
from typing import Iterable
from . import output
from . import config, output
class GitError(RuntimeError):
@@ -12,6 +12,16 @@ class GitError(RuntimeError):
def _run_git(args: Iterable[str], *, capture_output: bool = False, check: bool = True) -> subprocess.CompletedProcess:
cmd = ["git", *args]
# Dry-run ondersteuning: voer geen echte git-commando's uit, maar log enkel
# wat er zou gebeuren en geef een "geslaagde" CompletedProcess terug.
if config.CONFIG.dry_run:
output.info(f"[DRY-RUN] zou uitvoeren: {' '.join(cmd)}")
return subprocess.CompletedProcess(
cmd,
0,
stdout="" if capture_output else None,
stderr="",
)
result = subprocess.run(
cmd,
text=True,
@@ -92,3 +102,15 @@ def ensure_not_behind_remote(branch: str, remote: str) -> None:
f"Doe eerst een 'git pull --ff-only' of werk te wijzigingen lokaal bij."
)
def list_local_branches_with_prefix(prefix: str) -> list[str]:
"""Geef een gesorteerde lijst van lokale branches die met het prefix starten."""
result = _run_git(
["for-each-ref", "--format=%(refname:short)", "refs/heads"],
capture_output=True,
)
lines = (result.stdout or "").splitlines()
branches = sorted(b for b in (ln.strip() for ln in lines) if b.startswith(prefix))
return branches