refactor: update gen_commit.py script

This commit is contained in:
2025-05-13 18:59:06 +07:00
parent a1423f1cc1
commit e97a96205a
+69 -42
View File
@@ -1,72 +1,99 @@
from interpreter import OpenInterpreter
import os
import sys
import subprocess
from dotenv import load_dotenv
from interpreter import OpenInterpreter
load_dotenv()
load_dotenv() # Load environment variables from .env
def get_git_credentials():
if len(sys.argv) != 5: # Changed from 4 to 5 to match expected arguments
print(
"Usage: python gen_commit.py <GIT_USER> <GIT_PASS> <GIT_REPO> <PROJECT_PATH>"
)
"""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()
# Set auto_run to True to always allow code execution
agent.auto_run = True
agent.system_message = """
Your name is Bifrost,
you are a helpful assistant that can help me generate a commit message base on umcommit code.
Your name is Bifrost.
You are a helpful assistant that generates commit messages based on uncommitted code.
You should follow these rules:
1. The commit message should be in English.
2. The commit message should be concise and to the point.
3. The commit message should be formatted as follows:
- feat: add a new feature
- fix: fix a bug
- refactor: refactor the code
- chore: update the code
- perf: improve the performance
- test: add a test
- docs: update the docs
- update: update the code
- remove: remove the code
- style: style the code
- revert: revert the code
- merge: merge the code
- conflict: resolve the conflict
- other: other
4. Only return the commit message follow Git Commit Style Guide, no other text or explanation.
Follow these rules:
1. Use English.
2. Be concise and follow this format:
- feat: add a new feature
- fix: fix a bug
- refactor: refactor the code
- chore: update the code
- perf: improve the performance
- test: add a test
- docs: update the docs
- update: update the code
- remove: remove the code
- style: style the code
- revert: revert the code
- merge: merge the code
- conflict: resolve the conflict
- other: other
3. Only return the commit message. Do not add explanation or extra output.
"""
response = agent.chat(
f"""
Generate a commit message for the uncommit code in project path {project_path}
You should change working directory to {project_path} and use git diff --name-status to get the changes
"""
)
prompt = f"""
Generate a commit message based on the following changes in the project directory:
First, change directory:
cd {project_path}
Then run:
git diff --name-status
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"]
return response[-1]["content"].strip()
return str(response)
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(commit_msg)
os.system(f"git pull https://{git_user}:{git_pass}@{git_repo} || true")
os.system("git add .")
os.system(f'git commit -m "{commit_msg}" || true')
os.system(f"git push https://{git_user}:{git_pass}@{git_repo} || true")
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__":