[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
import os
|
2025-05-13 18:39:21 +07:00
|
|
|
import sys
|
2025-05-13 18:59:06 +07:00
|
|
|
import subprocess
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
from dotenv import load_dotenv
|
2025-05-13 18:59:06 +07:00
|
|
|
from interpreter import OpenInterpreter
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
|
2025-05-13 18:59:06 +07:00
|
|
|
load_dotenv() # Load environment variables from .env
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
|
2025-05-13 18:39:21 +07:00
|
|
|
|
|
|
|
|
def get_git_credentials():
|
2025-05-13 18:59:06 +07:00
|
|
|
"""Retrieve Git credentials and project path from command-line arguments."""
|
|
|
|
|
if len(sys.argv) != 5:
|
2025-05-13 19:02:13 +07:00
|
|
|
print(
|
|
|
|
|
"Usage: python gen_commit.py <GIT_USER> <GIT_PASS> <GIT_REPO> <PROJECT_PATH>"
|
|
|
|
|
)
|
2025-05-13 18:39:21 +07:00
|
|
|
sys.exit(1)
|
2025-05-13 18:50:45 +07:00
|
|
|
return sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
|
2025-05-13 18:39:21 +07:00
|
|
|
|
|
|
|
|
|
2025-05-13 18:59:06 +07:00
|
|
|
def run_command(command, cwd=None):
|
|
|
|
|
"""Run a shell command and return the output."""
|
|
|
|
|
try:
|
2025-05-13 19:02:13 +07:00
|
|
|
result = subprocess.run(
|
|
|
|
|
command,
|
|
|
|
|
cwd=cwd,
|
|
|
|
|
shell=True,
|
|
|
|
|
check=True,
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
|
text=True,
|
|
|
|
|
)
|
2025-05-13 18:59:06 +07:00
|
|
|
return result.stdout.strip()
|
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
|
print(f"Command failed: {command}\n{e.stderr}")
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
2025-05-13 18:50:45 +07:00
|
|
|
def gen_commit(project_path):
|
2025-05-13 18:59:06 +07:00
|
|
|
"""Use OpenInterpreter to generate a commit message based on git diff."""
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
agent = OpenInterpreter()
|
|
|
|
|
agent.auto_run = True
|
2025-05-13 19:02:13 +07:00
|
|
|
with open(
|
2025-05-13 19:10:15 +07:00
|
|
|
f"{project_path}/scripts/interpreter/resources/commit_convention.txt", "r"
|
2025-05-13 19:02:13 +07:00
|
|
|
) as f:
|
|
|
|
|
file_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:
|
|
|
|
|
{file_content}
|
|
|
|
|
3. 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 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"].strip()
|
|
|
|
|
|
|
|
|
|
return str(response).strip()
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def push_code():
|
2025-05-13 18:59:06 +07:00
|
|
|
"""Pull, commit, and push code with AI-generated message."""
|
2025-05-13 18:50:45 +07:00
|
|
|
git_user, git_pass, git_repo, project_path = get_git_credentials()
|
2025-05-13 18:59:06 +07:00
|
|
|
|
|
|
|
|
# Change to project path
|
|
|
|
|
os.chdir(project_path)
|
|
|
|
|
|
|
|
|
|
# Generate commit message
|
2025-05-13 18:50:45 +07:00
|
|
|
commit_msg = gen_commit(project_path)
|
2025-05-13 18:59:06 +07:00
|
|
|
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")
|
[{'role': 'assistant', 'type': 'code', 'format': 'shell', 'content': 'cd /root/dev/Bifrost && git diff --name-status'}, {'role': 'computer', 'type': 'console', 'format': 'output', 'content': '\nM\tMakefile\nM\tbackend/app/service/app.go\nM\tregistry\n'}, {'role': 'assistant', 'type': 'message', 'content': 'The files that have been modified are:\n\n- \n- \n- \n\nBased on these changes, here is a generated commit message:\n\n\n\nThis message indicates updates have been made to the specified files. If you have specific details about the changes, feel free to share them for a more tailored message.'}]
2025-05-13 18:29:25 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
push_code()
|