AquaDX/tools/merge.py

106 lines
3.9 KiB
Python
Raw Normal View History

2023-12-21 13:44:36 +08:00
#!/usr/bin/env python3
import argparse
2023-12-21 14:04:55 +08:00
import shlex
2023-12-21 13:44:36 +08:00
from subprocess import check_call, check_output
2024-02-06 18:26:45 +08:00
from openai import OpenAI
client = OpenAI()
2023-12-21 14:04:55 +08:00
# Readline is used to fix left/right arrow keys in input(), do not remove as unused
import readline
2023-12-21 13:44:36 +08:00
my_base = 'master'
def gen_merge_msg(commits: str):
openai_example_commits = """
0d422120 - [API] Add a feature to retrieve user photos.
4a64895e - [api] Add maimai2 userPhoto API
e271cb45 - [API] Add a feature to retrieve user photos.
0bf54e66 - [API] Let web music list read from database
0913ef20 - [api] Add maimai2 userPhoto API
7cc9fb11 - Revert "New API to return user photos."
9c51b1e0 - Revert "Add API - Get user photos (mai)"
ba1f4589 - New API to return user photos.
e7848cb9 - Add API - Get user photos (mai)
"""
openai_example_response = "Add user photos feature for maimai2"
2023-12-21 14:04:55 +08:00
openai_prompt = ("I just merged the following branch, what commit message can best summarize the changes below? "
"Please make sure it is less than 100 characters long.\n\n")
2023-12-21 13:44:36 +08:00
2024-02-06 18:26:45 +08:00
complete = client.chat.completions.create(model="gpt-4",
2023-12-21 13:44:36 +08:00
messages=[
{"role": "system",
"content": "You are a senior software engineer helping with maintaining a game server repository."},
{"role": "user",
2023-12-21 14:04:55 +08:00
"content": f"{openai_prompt}{openai_example_commits}"},
2023-12-21 13:44:36 +08:00
{"role": "assistant",
"content": f'"{openai_example_response}"'},
{"role": "user",
2023-12-21 14:04:55 +08:00
"content": f"{openai_prompt}{commits}"},
2024-02-06 18:26:45 +08:00
])
2023-12-21 13:44:36 +08:00
2023-12-21 14:04:55 +08:00
m = complete.choices[0].message.content
return m.strip().strip('"')
2023-12-21 13:44:36 +08:00
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Helper for merging remote sibling repos')
parser.add_argument('url', type=str, help='Remote repo url')
parser.add_argument('-b', '--branch', type=str, default='master', help='Branch to merge')
parser.add_argument('-m', '--msg', type=str, default=None, help='Merge commit message')
args = parser.parse_args()
url = args.url
print(f'Merging {url}...')
# Fetch head branch
print()
print('Fetching head branch...')
check_call(f'git -c http.sslVerify=false fetch {url} {args.branch}', shell=True)
# Get the list of commits from the branch
commits = check_output(f'git log --pretty=format:"%H - %s" {my_base}..FETCH_HEAD', shell=True).decode()
commits = [c.split(' - ', 1) for c in commits.split('\n')]
commits = [c for c in commits if len(c) == 2]
commits = [f"{sha[:8]} - {msg}" for sha, msg in commits]
commits = '\n'.join(commits)
print("Commits:")
print(commits)
# Get the list of authors from the branch
authors = check_output(f'git log --pretty=format:"%an <%ae>" {my_base}..FETCH_HEAD', shell=True).decode()
authors = set(authors.split('\n'))
authors = "\n".join(f"Co-authored-by: {a}" for a in authors)
print("\nAuthors:")
print(authors)
# Create a merge commit message
owner = url.split('/')[-2]
repo = url.split('/')[-1].split('.')[0]
# If there isn't a merge message, create one by asking ChatGPT to summarize the commits
2023-12-21 14:04:55 +08:00
msg = args.msg
if not msg:
2023-12-21 13:44:36 +08:00
while True:
2023-12-21 14:04:55 +08:00
msg = input('\nPlease enter a merge message (or leave blank to generate one): ').strip()
if msg:
2023-12-21 13:44:36 +08:00
break
2023-12-21 14:04:55 +08:00
msg = gen_merge_msg(commits)
print(f'Generated message: {msg}')
if input('Is this okay? [y/N] ').lower() == 'y':
2023-12-21 13:44:36 +08:00
break
# Merge head branch
print('\nMerging fetch_head...')
2023-12-21 14:04:55 +08:00
check_call([*shlex.split('git merge FETCH_HEAD --no-ff --no-edit'),
'-m', f"Merge {owner}/{repo} : {msg}",
'-m', f"{commits}\n\n{authors}"])
2023-12-21 13:44:36 +08:00
# Push
assert input('\nPush? [Enter/N]') == ""
check_call('git push', shell=True)