# .agent/tools/auto_commit.py import subprocess import sys import os def run_command(command): try: result = subprocess.run(command, capture_output=True, text=True, check=True, shell=True) return result.stdout.strip() except subprocess.CalledProcessError as e: print(f"❌ Erro ao executar '{command}': {e.stderr}") sys.exit(1) def get_detailed_report(): """Tenta extrair informações de testes e planos de implementação.""" report = [] # 1. Tenta ler o implementation_plan.md if os.path.exists("implementation_plan.md"): report.append("--- implementation_plan.md ---") with open("implementation_plan.md", "r", encoding="utf-8") as f: report.append(f.read().strip()) report.append("-" * 30) # 2. Tenta encontrar logs de teste ou evidências if os.path.exists("docs/walkthrough.md"): report.append("--- docs/walkthrough.md ---") with open("docs/walkthrough.md", "r", encoding="utf-8") as f: report.append(f.read().strip()) report.append("-" * 30) return "\n".join(report) def auto_commit(commit_type, title, body=""): # 1. Verifica status do git status = run_command("git status --short") if not status: print("ℹ️ Nada para commitar. Working tree clean.") return # 2. Prepara o relatório técnico technical_report = get_detailed_report() full_body = body if technical_report: full_body += f"\n\n### RELATÓRIO TÉCNICO & VALIDAÇÃO:\n{technical_report}" # 3. Executa o Staging run_command("git add .") # 4. Formata o Commit commit_msg = f"{commit_type}: {title}" # Prepara o comando de commit # Usamos arquivos temporários para mensagens longas para evitar problemas de escape no shell do Windows msg_file = ".commit_msg_temp" with open(msg_file, "w", encoding="utf-8") as f: f.write(f"{commit_msg}\n\n{full_body}") try: run_command(f'git commit -F {msg_file}') print(f"✅ Commit realizado com sucesso: {commit_msg}") finally: if os.path.exists(msg_file): os.remove(msg_file) if __name__ == "__main__": if len(sys.argv) < 3: print("Uso: python auto_commit.py [corpo]") print("Ex: python auto_commit.py feat 'adicionando login' 'implementado com sucesso'") else: c_type = sys.argv[1] c_title = sys.argv[2] c_body = sys.argv[3] if len(sys.argv) > 3 else "" auto_commit(c_type, c_title, c_body)