Files
eveAI/scripts/git/commands/hotfix.py

155 lines
5.7 KiB
Python

from __future__ import annotations
from ..core import config, git_api, hooks, output
def _hotfix_branch_name(name: str) -> str:
return f"{config.CONFIG.hotfix_prefix}{name}"
def _ensure_version(name: str | None) -> str:
# Voor hotfix kunnen we dezelfde prompt gebruiken indien geen naam/versie is opgegeven
if name:
return name
output.info("Geen hotfix-naam/versie opgegeven. Gelieve een identificatie in te geven (bijv. 1.2.1 of short-name):")
entered = input("Hotfix: ").strip()
if not entered:
raise git_api.GitError("Geen hotfix-naam/versie opgegeven.")
return entered
def handle_hotfix_start(args) -> int:
cfg = config.CONFIG
try:
raw_name: str = args.name
name = _ensure_version(raw_name)
branch_name = _hotfix_branch_name(name)
git_api.ensure_clean_working_tree()
git_api.fetch_remote(cfg.remote_name)
git_api.ensure_not_behind_remote(cfg.main_branch, cfg.remote_name)
output.info(f"Aanmaken van hotfix branch '{branch_name}' vanaf '{cfg.main_branch}'")
git_api.create_branch(branch_name, cfg.main_branch)
output.success(f"Hotfix branch '{branch_name}' is aangemaakt en gecheckt out.")
# Hooks na succesvol aanmaken van een hotfix branch
hooks.run_hooks(
"hotfix_start",
{
"branch": branch_name,
"base_branch": cfg.main_branch,
"version": name,
},
)
return 0
except git_api.GitError as exc:
output.error(str(exc))
return 1
def handle_hotfix_finish(args) -> int:
cfg = config.CONFIG
try:
name_arg = getattr(args, "name", None)
env_arg = getattr(args, "env", None)
git_api.ensure_clean_working_tree()
git_api.fetch_remote(cfg.remote_name)
if name_arg:
name = name_arg
hotfix_branch = _hotfix_branch_name(name)
else:
current = git_api.get_current_branch()
prefix = cfg.hotfix_prefix
if current.startswith(prefix):
name = current[len(prefix) :]
hotfix_branch = current
else:
# Toon lijst van beschikbare hotfix branches
branches = git_api.list_local_branches_with_prefix(cfg.hotfix_prefix)
if not branches:
raise git_api.GitError(
"Er zijn geen lokale hotfix branches gevonden. "
"Maak eerst een hotfix branch aan of geef een naam op."
)
output.heading("Beschikbare hotfix branches")
for b in branches:
output.plain(f"- {b}")
raise git_api.GitError(
"Je zit niet op een hotfix branch. Kies een van de bovenstaande namen "
"en voer het commando opnieuw uit, bv.: gitflow hotfix finish <naam-of-versie>."
)
git_api.ensure_not_behind_remote(hotfix_branch, cfg.remote_name)
git_api.ensure_not_behind_remote(cfg.main_branch, cfg.remote_name)
git_api.ensure_not_behind_remote(cfg.develop_branch, cfg.remote_name)
# Bepaal omgeving en tagnaam
env = env_arg or cfg.hotfix_default_env
suffix = cfg.environments.get(env, "")
base_tag = cfg.tag_format.format(version=name)
tag_name = f"{base_tag}{suffix}"
# Merge naar main
output.info(f"Mergen van '{hotfix_branch}' naar '{cfg.main_branch}' en tag '{tag_name}' aanmaken")
git_api.checkout_branch(cfg.main_branch)
merge_args = ["merge"]
if cfg.use_no_ff_for_hotfix:
merge_args.append("--no-ff")
merge_args.append(hotfix_branch)
try:
git_api._run_git(merge_args) # type: ignore[attr-defined]
except git_api.GitError as exc:
raise git_api.GitError(
"Merge van hotfix naar main is mislukt (mogelijk conflicten). Los de conflicten op en voltooi de merge handmatig."
) from exc
git_api._run_git(["tag", tag_name]) # type: ignore[attr-defined]
# Merge naar develop
output.info(f"Mergen van '{hotfix_branch}' naar '{cfg.develop_branch}'")
git_api.checkout_branch(cfg.develop_branch)
merge_args = ["merge"]
if cfg.use_no_ff_for_hotfix:
merge_args.append("--no-ff")
merge_args.append(hotfix_branch)
try:
git_api._run_git(merge_args) # type: ignore[attr-defined]
except git_api.GitError as exc:
raise git_api.GitError(
"Merge van hotfix naar develop is mislukt (mogelijk conflicten). Los de conflicten op en voltooi de merge handmatig."
) from exc
output.success(
f"Hotfix '{name}' is voltooid: gemerged naar '{cfg.main_branch}' en '{cfg.develop_branch}' en getagd als '{tag_name}'."
)
# Optionele cleanup
if cfg.delete_hotfix_after_finish:
output.info(f"Opruimen van lokale hotfix branch '{hotfix_branch}'")
try:
git_api._run_git(["branch", "-d", hotfix_branch]) # type: ignore[attr-defined]
except git_api.GitError as exc:
output.warning(f"Kon hotfix branch '{hotfix_branch}' niet verwijderen: {exc}")
# Hooks na succesvolle hotfix-finish
hooks.run_hooks(
"hotfix_finish",
{
"branch": hotfix_branch,
"base_branch": cfg.main_branch,
"version": name,
"env": env,
"tag": tag_name,
},
)
return 0
except git_api.GitError as exc:
output.error(str(exc))
return 1