from argparse import Namespace from datetime import datetime from git_flow import COMMIT_TYPES, GitFlowError from git_flow.command.base import Command from git_flow.git import Git class CommitCommand(Command): def name(self) -> str: return "commit" def description(self) -> str: return """Crea un commit siguiendo el formato de Conventional Commits""" def run(self, args: Namespace = Namespace()): self.ensure_initialized() branch = self.ensure_right_branch() if not self._has_files_staged(): self.warning("No hay cambios en el indice para commitear.") if self.confirm("¿Desea agregar la carpeta actual (`git add .`)?"): Git("add", ".").exec() else: raise GitFlowError( "Debe agregar algún cambio al indice para continuar." ) commit_type = self.choice("Tipo de commit", COMMIT_TYPES) commit_message = self.prompt( "Mensaje (máximo recomendado: 100 caracteres)", persistent=True ) message = commit_type + ": " + commit_message if branch.startswith("wip/"): original_branch = branch.removeprefix("wip/") Git("commit", m=message).exec() if commit_type != "wip": suffix = datetime.now().strftime("%Y%m%dT%H%M") Git("switch", original_branch).exec() Git("merge", branch, squash=True).exec() Git("commit", m=message).exec() Git("branch", branch, "trash/" + branch + "/" + suffix, move=True).exec() else: if commit_type == "wip": Git("switch", "wip/" + branch, create=True).exec() Git("commit", m=message).exec() def _has_files_staged(self): status = Git.status() if not status: raise GitFlowError("No hay cambios para commitear.") files_in_index = [] for s in status: if s.worktree != "?" and s.worktree != " ": files_in_index.append(s.file) return len(files_in_index) > 0