refactor(scripts): enhance gen_commit.py logic with cleaner code

This commit is contained in:
KaySar12 2025-05-19 13:42:15 +07:00
parent 707f6d429b
commit 91c9244fdc

View File

@ -26,7 +26,9 @@ def gen_commit(project_path):
agent.llm.model = "gpt-4o"
agent.auto_run = True
convention_path = os.path.join(os.path.dirname(__file__), "resources/commit_convention.md")
convention_path = os.path.join(
os.path.dirname(__file__), "resources/commit_convention.md"
)
with open(convention_path, "r") as f:
convention_content = f.read()
@ -52,7 +54,12 @@ Analyze the following git diff and generate a commit message:
"""
response = agent.chat(prompt)
if isinstance(response, list) and response and isinstance(response[-1], dict) and "content" in response[-1]:
if (
isinstance(response, list)
and response
and isinstance(response[-1], dict)
and "content" in response[-1]
):
return response[-1]["content"].strip()
return str(response).strip()
@ -60,8 +67,14 @@ Analyze the following git diff and generate a commit message:
async def has_submodules():
"""Check if the repository has any submodules."""
proc = await asyncio.create_subprocess_exec(
"git", "config", "--file", ".gitmodules", "--get-regexp", r"^submodule\.",
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
"git",
"config",
"--file",
".gitmodules",
"--get-regexp",
r"^submodule\.",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
return bool(stdout.decode().strip())
@ -72,8 +85,14 @@ async def get_submodule_info():
return {}
proc = await asyncio.create_subprocess_exec(
"git", "config", "--file", ".gitmodules", "--get-regexp", r"^submodule\..*\.path$",
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
"git",
"config",
"--file",
".gitmodules",
"--get-regexp",
r"^submodule\..*\.path$",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
@ -85,7 +104,7 @@ async def get_submodule_info():
for line in stdout.decode().splitlines():
parts = line.split()
if len(parts) == 2:
name = parts[0].split('.')[1]
name = parts[0].split(".")[1]
path = parts[1]
abs_path = os.path.abspath(path)
submodule_info[abs_path] = name
@ -102,14 +121,15 @@ async def commit_and_push_submodules():
proc = await asyncio.create_subprocess_shell(
"git submodule foreach --quiet 'if [ -n \"$(git status --porcelain)\" ]; then echo $path; fi'",
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if stderr:
print(f"Error checking submodules:\n{stderr.decode()}")
return
submodule_paths = stdout.decode().strip().split('\n')
submodule_paths = stdout.decode().strip().split("\n")
git_user, git_pass, _ = get_git_credentials()
for submodule_path in submodule_paths:
@ -123,8 +143,13 @@ async def commit_and_push_submodules():
commit_msg = gen_commit(abs_path)
proc = await asyncio.create_subprocess_exec(
"git", "remote", "get-url", "origin",
cwd=abs_path, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
"git",
"remote",
"get-url",
"origin",
cwd=abs_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if stderr:
@ -157,8 +182,12 @@ async def push_code():
print(f"Changed to project path: {project_path}")
proc = await asyncio.create_subprocess_exec(
"git", "remote", "get-url", "origin",
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
"git",
"remote",
"get-url",
"origin",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if stderr: