- Writing custom git flow scripts - a start
This commit is contained in:
108
scripts/git/commands/hotfix.py
Normal file
108
scripts/git/commands/hotfix.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from ..core import config, git_api, 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.")
|
||||
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)
|
||||
|
||||
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:
|
||||
name = _ensure_version(None)
|
||||
hotfix_branch = _hotfix_branch_name(name)
|
||||
|
||||
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)
|
||||
|
||||
# Voor nu vragen we ook hier de versie/naam en gebruiken die voor de tag.
|
||||
# In een latere iteratie kunnen we hier automatische patch-bumping doen.
|
||||
version_for_tag = name
|
||||
tag_name = cfg.tag_format.format(version=version_for_tag)
|
||||
|
||||
# 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}'."
|
||||
)
|
||||
return 0
|
||||
except git_api.GitError as exc:
|
||||
output.error(str(exc))
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user