113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
from ..core import config, git_api, hooks, output
|
|
|
|
|
|
def _feature_branch_name(name: str) -> str:
|
|
return f"{config.CONFIG.feature_prefix}{name}"
|
|
|
|
|
|
def handle_feature_start(args) -> int:
|
|
name: str = args.name
|
|
cfg = config.CONFIG
|
|
|
|
try:
|
|
git_api.ensure_clean_working_tree()
|
|
git_api.fetch_remote(cfg.remote_name)
|
|
git_api.ensure_not_behind_remote(cfg.develop_branch, cfg.remote_name)
|
|
|
|
branch_name = _feature_branch_name(name)
|
|
output.info(f"Aanmaken van feature branch '{branch_name}' vanaf '{cfg.develop_branch}'")
|
|
git_api.create_branch(branch_name, cfg.develop_branch)
|
|
output.success(f"Feature branch '{branch_name}' is aangemaakt en gecheckt out.")
|
|
|
|
# Hooks na succesvol aanmaken van een feature branch
|
|
hooks.run_hooks(
|
|
"feature_start",
|
|
{
|
|
"branch": branch_name,
|
|
"base_branch": cfg.develop_branch,
|
|
},
|
|
)
|
|
return 0
|
|
except git_api.GitError as exc:
|
|
output.error(str(exc))
|
|
return 1
|
|
|
|
|
|
def handle_feature_finish(args) -> int:
|
|
cfg = config.CONFIG
|
|
name: str | None = args.name
|
|
|
|
try:
|
|
git_api.ensure_clean_working_tree()
|
|
git_api.fetch_remote(cfg.remote_name)
|
|
|
|
if name is None:
|
|
current = git_api.get_current_branch()
|
|
if current.startswith(cfg.feature_prefix):
|
|
feature_branch = current
|
|
else:
|
|
# Geen naam en we zitten niet op een feature-branch: toon een lijst
|
|
# met beschikbare feature-branches om de gebruiker te helpen kiezen.
|
|
branches = git_api.list_local_branches_with_prefix(cfg.feature_prefix)
|
|
if not branches:
|
|
raise git_api.GitError(
|
|
"Er zijn geen lokale feature branches gevonden. "
|
|
"Maak eerst een feature branch aan of geef een naam op."
|
|
)
|
|
|
|
output.heading("Beschikbare feature branches")
|
|
for b in branches:
|
|
output.plain(f"- {b}")
|
|
|
|
raise git_api.GitError(
|
|
"Je zit niet op een feature branch. Kies een van de bovenstaande namen "
|
|
"en voer het commando opnieuw uit, bv.: gitflow feature finish <naam-zonder-prefix>."
|
|
)
|
|
else:
|
|
feature_branch = _feature_branch_name(name)
|
|
|
|
# Zorg dat betrokken branches niet achterlopen op remote
|
|
git_api.ensure_not_behind_remote(feature_branch, cfg.remote_name)
|
|
git_api.ensure_not_behind_remote(cfg.develop_branch, cfg.remote_name)
|
|
|
|
output.info(f"Mergen van '{feature_branch}' naar '{cfg.develop_branch}'")
|
|
git_api.checkout_branch(cfg.develop_branch)
|
|
|
|
merge_args = ["merge"]
|
|
if cfg.use_no_ff_for_feature:
|
|
merge_args.append("--no-ff")
|
|
merge_args.append(feature_branch)
|
|
|
|
try:
|
|
git_api._run_git(merge_args) # type: ignore[attr-defined]
|
|
except git_api.GitError as exc:
|
|
raise git_api.GitError(
|
|
"Merge is mislukt (mogelijk conflicten). Los de conflicten op en voltooi de merge handmatig."
|
|
) from exc
|
|
|
|
output.success(f"Feature branch '{feature_branch}' is gemerged naar '{cfg.develop_branch}'.")
|
|
|
|
# Optionele cleanup van de feature branch
|
|
if cfg.delete_feature_after_finish:
|
|
output.info(f"Opruimen van lokale feature branch '{feature_branch}'")
|
|
try:
|
|
git_api._run_git(["branch", "-d", feature_branch]) # type: ignore[attr-defined]
|
|
except git_api.GitError as exc:
|
|
output.warning(f"Kon feature branch '{feature_branch}' niet verwijderen: {exc}")
|
|
|
|
# Hooks na succesvolle feature-finish
|
|
hooks.run_hooks(
|
|
"feature_finish",
|
|
{
|
|
"branch": feature_branch,
|
|
"base_branch": cfg.develop_branch,
|
|
},
|
|
)
|
|
return 0
|
|
except git_api.GitError as exc:
|
|
output.error(str(exc))
|
|
return 1
|
|
|