refactor: improve push_code function with submodule handling

This commit is contained in:
KaySar12 2025-05-20 17:10:03 +07:00
parent 0e0c7098e1
commit cd486b3b89

View File

@ -14,37 +14,17 @@ def get_git_credentials():
return sys.argv[1], sys.argv[2], sys.argv[3]
def get_git_diff(project_path):
def get_git_diff(project_path, summary=True):
"""Run git diff and return the output."""
result = os.popen(f'cd "{project_path}" && git diff --compact-summary').read()
staged_changes = os.popen(f'cd "{project_path}" && git diff --cached --compact-summary').read()
flag = "--compact-summary" if summary else ""
result = os.popen(f'cd "{project_path}" && git diff {flag}').read()
staged_changes = os.popen(f'cd "{project_path}" && git diff --cached {flag}').read()
# Combine both staged and unstaged changes
all_changes = result.strip() + "\n" + staged_changes.strip()
return all_changes.strip() if all_changes.strip() else "✓ No changes"
def format_tree_output(base_path, submodule_diffs):
"""Format the output in a tree structure."""
base_name = os.path.basename(base_path)
base_diff = get_git_diff(base_path)
output = [f"📦 {base_name}"]
output.append(f"└── 📝 Changes:")
for line in base_diff.split('\n'):
output.append(f" └── {line}")
if submodule_diffs:
output.append("└── 📚 Submodules:")
for submodule_path, diff in submodule_diffs.items():
name = os.path.basename(submodule_path)
output.append(f" └── 📦 {name}")
for line in diff.split('\n'):
output.append(f" └── {line}")
return '\n'.join(output)
def gen_commit(project_path):
"""Generate commit message using OpenInterpreter based on git diff output."""
agent = OpenInterpreter()
@ -205,38 +185,38 @@ async def commit_and_push_submodules():
print(f"Submodule '{name}' stderr:\n{stderr.decode()}")
def format_tree_output(project_path, project_diff, submodule_diffs):
tree = f"📦 {os.path.basename(project_path)}\n"
tree += "└── 📝 Changes:\n"
if project_diff:
tree += indent_lines(project_diff, level=2)
else:
tree += indent_lines("✓ No changes", level=2)
def format_tree_output(base_path, submodule_diffs, commit):
"""Format the output in a tree structure."""
base_name = os.path.basename(base_path)
base_diff = get_git_diff(base_path, True)
output = [f"📦 {base_name}"]
output.append(f"└── 📝 {commit[base_path]}:")
for line in base_diff.split("\n"):
output.append(f" └── {line}")
if submodule_diffs:
tree += "\n└── 📚 Submodules:"
for sub_path, diff in submodule_diffs.items():
sub_name = os.path.basename(sub_path)
tree += f"\n └── 📦 {sub_name}\n"
if diff:
tree += indent_lines(diff, level=3, prefix="└── 📝 Changes:\n")
else:
tree += " └── ✓ No changes"
return tree
output.append("└── 📚 Submodules:")
for submodule_path, diff in submodule_diffs.items():
name = os.path.basename(submodule_path)
output.append(f" └── 📦 {name}")
for line in diff.split("\n"):
output.append(f" └── {line}")
def indent_lines(text, level=1, prefix=""):
indent = " " * level
if prefix:
return f"{indent}{prefix}{indent}{text.replace(chr(10), f'{chr(10)}{indent}')}"
return f"{indent}{text.replace(chr(10), f'{chr(10)}{indent}')}"
return "\n".join(output)
async def push_code():
git_user, git_pass, project_path = get_git_credentials()
os.chdir(project_path)
print(f"Changed to project path: {project_path}")
submodule_diffs = {}
commit = {}
if await has_submodules():
submodule_info = await get_submodule_info()
for path in submodule_info:
submodule_diffs[path] = get_git_diff(path)
# Rest of the push_code function remains the same
proc = await asyncio.create_subprocess_exec(
"git",
"remote",
@ -251,14 +231,10 @@ async def push_code():
git_repo = stdout.decode().strip().replace(".git", "").replace("https://", "")
remote_url = f"https://{git_user}:{git_pass}@{git_repo}"
# Get main project diff before committing
project_diff = get_git_diff(project_path)
# Commit & push submodules
await commit_and_push_submodules()
# Generate and commit main project
commit_msg = gen_commit(project_path)
commit[project_path] = commit_msg
print(f"Generated commit message:\n{commit_msg}\n")
commands = [
@ -273,25 +249,19 @@ async def push_code():
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
if stdout:
print(stdout.decode())
if stderr:
print(stderr.decode())
if proc.returncode != 0:
print(f"Command failed: {cmd}")
print(stderr.decode())
elif stderr:
# Not an error; just info output
print(stderr.decode())
# Prepare submodule diff tree
submodule_diffs = {}
if await has_submodules():
submodule_info = await get_submodule_info()
for path in submodule_info:
submodule_diffs[path] = get_git_diff(path)
# Show tree output
tree_output = format_tree_output(project_path, project_diff, submodule_diffs)
print("\n📦 Project Tree View:")
# Format and print the tree output
tree_output = format_tree_output(project_path, submodule_diffs, commit)
print("\nProject Status:")
print(tree_output)
print()
if __name__ == "__main__":
asyncio.run(push_code())