refactor(scripts): enhance gen_commit.py logic with cleaner code
This commit is contained in:
parent
707f6d429b
commit
91c9244fdc
@ -26,7 +26,9 @@ def gen_commit(project_path):
|
|||||||
agent.llm.model = "gpt-4o"
|
agent.llm.model = "gpt-4o"
|
||||||
agent.auto_run = True
|
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:
|
with open(convention_path, "r") as f:
|
||||||
convention_content = f.read()
|
convention_content = f.read()
|
||||||
|
|
||||||
@ -52,7 +54,12 @@ Analyze the following git diff and generate a commit message:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
response = agent.chat(prompt)
|
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 response[-1]["content"].strip()
|
||||||
return str(response).strip()
|
return str(response).strip()
|
||||||
|
|
||||||
@ -60,8 +67,14 @@ Analyze the following git diff and generate a commit message:
|
|||||||
async def has_submodules():
|
async def has_submodules():
|
||||||
"""Check if the repository has any submodules."""
|
"""Check if the repository has any submodules."""
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"git", "config", "--file", ".gitmodules", "--get-regexp", r"^submodule\.",
|
"git",
|
||||||
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
|
"config",
|
||||||
|
"--file",
|
||||||
|
".gitmodules",
|
||||||
|
"--get-regexp",
|
||||||
|
r"^submodule\.",
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
return bool(stdout.decode().strip())
|
return bool(stdout.decode().strip())
|
||||||
@ -72,8 +85,14 @@ async def get_submodule_info():
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"git", "config", "--file", ".gitmodules", "--get-regexp", r"^submodule\..*\.path$",
|
"git",
|
||||||
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
|
"config",
|
||||||
|
"--file",
|
||||||
|
".gitmodules",
|
||||||
|
"--get-regexp",
|
||||||
|
r"^submodule\..*\.path$",
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
|
|
||||||
@ -85,7 +104,7 @@ async def get_submodule_info():
|
|||||||
for line in stdout.decode().splitlines():
|
for line in stdout.decode().splitlines():
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) == 2:
|
if len(parts) == 2:
|
||||||
name = parts[0].split('.')[1]
|
name = parts[0].split(".")[1]
|
||||||
path = parts[1]
|
path = parts[1]
|
||||||
abs_path = os.path.abspath(path)
|
abs_path = os.path.abspath(path)
|
||||||
submodule_info[abs_path] = name
|
submodule_info[abs_path] = name
|
||||||
@ -102,14 +121,15 @@ async def commit_and_push_submodules():
|
|||||||
|
|
||||||
proc = await asyncio.create_subprocess_shell(
|
proc = await asyncio.create_subprocess_shell(
|
||||||
"git submodule foreach --quiet 'if [ -n \"$(git status --porcelain)\" ]; then echo $path; fi'",
|
"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()
|
stdout, stderr = await proc.communicate()
|
||||||
if stderr:
|
if stderr:
|
||||||
print(f"Error checking submodules:\n{stderr.decode()}")
|
print(f"Error checking submodules:\n{stderr.decode()}")
|
||||||
return
|
return
|
||||||
|
|
||||||
submodule_paths = stdout.decode().strip().split('\n')
|
submodule_paths = stdout.decode().strip().split("\n")
|
||||||
git_user, git_pass, _ = get_git_credentials()
|
git_user, git_pass, _ = get_git_credentials()
|
||||||
|
|
||||||
for submodule_path in submodule_paths:
|
for submodule_path in submodule_paths:
|
||||||
@ -123,8 +143,13 @@ async def commit_and_push_submodules():
|
|||||||
commit_msg = gen_commit(abs_path)
|
commit_msg = gen_commit(abs_path)
|
||||||
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"git", "remote", "get-url", "origin",
|
"git",
|
||||||
cwd=abs_path, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
|
"remote",
|
||||||
|
"get-url",
|
||||||
|
"origin",
|
||||||
|
cwd=abs_path,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
if stderr:
|
if stderr:
|
||||||
@ -157,8 +182,12 @@ async def push_code():
|
|||||||
print(f"Changed to project path: {project_path}")
|
print(f"Changed to project path: {project_path}")
|
||||||
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"git", "remote", "get-url", "origin",
|
"git",
|
||||||
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
|
"remote",
|
||||||
|
"get-url",
|
||||||
|
"origin",
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
)
|
)
|
||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
if stderr:
|
if stderr:
|
||||||
|
Loading…
Reference in New Issue
Block a user