Files
bifrost-registry/scripts/interpreter/gen_commit.py
T
hoangvv 31e4b8e392 refactor(interpreter): improve commit message generation and push process
enhance clarity by updating instructions and restructuring functions related to commit message generation from AI responses. streamline git operations by combining actions and renaming functions for better readability.
2025-05-14 09:44:19 +07:00

114 lines
3.2 KiB
Python
Executable File

import os
import sys
import subprocess
from dotenv import load_dotenv
from interpreter import OpenInterpreter
load_dotenv() # Load environment variables from .env
def get_git_credentials():
"""Retrieve Git credentials and project path from command-line arguments."""
if len(sys.argv) != 5:
print(
"Usage: python gen_commit.py <GIT_USER> <GIT_PASS> <GIT_REPO> <PROJECT_PATH>"
)
sys.exit(1)
return sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
def run_command(command, cwd=None):
"""Run a shell command and return the output."""
try:
result = subprocess.run(
command,
cwd=cwd,
shell=True,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Command failed: {command}\n{e.stderr}")
return ""
def gen_commit(project_path):
"""Use OpenInterpreter to generate a commit message based on git diff."""
agent = OpenInterpreter()
agent.llm.model = "gpt-4o"
agent.auto_run = True
# Read both convention and git commit files
convention_path = os.path.join(
os.path.dirname(__file__), "resources/commit_convention.md"
)
git_commit_path = os.path.join(os.path.dirname(__file__), "resources/git_commit.md")
with open(convention_path, "r") as f:
convention_content = f.read()
with open(git_commit_path, "r") as f:
git_commit_content = f.read()
agent.system_message = f"""
Your name is Bifrost.
You are a helpful assistant that generates commit messages based on uncommitted code.
Follow these rules:
1. Use English.
2. Be concise and follow this format:
{convention_content}
3. Use these git commands to analyze changes as you see fit:
{git_commit_content}
4. Only return the commit message. Do not add explanation or extra output.
"""
prompt = f"""
Generate a commit message based on the following changes in the project directory:
First, change directory:
cd {project_path}
Then check the uncommitted changes.
Then generate a commit message according to the changes.
"""
response = agent.chat(prompt)
# Extract just the content from the response
if isinstance(response, list) and len(response) > 0:
if isinstance(response[-1], dict) and "content" in response[-1]:
return response[-1]["content"].strip()
return str(response).strip()
def push_code():
"""Pull, commit, and push code with AI-generated message."""
git_user, git_pass, git_repo, project_path = get_git_credentials()
# Change to project path
os.chdir(project_path)
# Generate commit message
commit_msg = gen_commit(project_path)
print(f"Generated commit message:\n{commit_msg}\n")
# Pull, add, commit, and push
remote_url = f"https://{git_user}:{git_pass}@{git_repo}"
run_command(f"git pull {remote_url} || true")
run_command("git add .")
run_command(f'git commit -m "{commit_msg}" || true')
run_command(f"git push {remote_url} || true")
if __name__ == "__main__":
push_code()