refactor: improve push_code function with submodule handling
This commit is contained in:
parent
0e0c7098e1
commit
cd486b3b89
@ -14,37 +14,17 @@ def get_git_credentials():
|
|||||||
return sys.argv[1], sys.argv[2], sys.argv[3]
|
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."""
|
"""Run git diff and return the output."""
|
||||||
result = os.popen(f'cd "{project_path}" && git diff --compact-summary').read()
|
flag = "--compact-summary" if summary else ""
|
||||||
staged_changes = os.popen(f'cd "{project_path}" && git diff --cached --compact-summary').read()
|
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
|
# Combine both staged and unstaged changes
|
||||||
all_changes = result.strip() + "\n" + staged_changes.strip()
|
all_changes = result.strip() + "\n" + staged_changes.strip()
|
||||||
return all_changes.strip() if all_changes.strip() else "✓ No changes"
|
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):
|
def gen_commit(project_path):
|
||||||
"""Generate commit message using OpenInterpreter based on git diff output."""
|
"""Generate commit message using OpenInterpreter based on git diff output."""
|
||||||
agent = OpenInterpreter()
|
agent = OpenInterpreter()
|
||||||
@ -205,38 +185,38 @@ async def commit_and_push_submodules():
|
|||||||
print(f"Submodule '{name}' stderr:\n{stderr.decode()}")
|
print(f"Submodule '{name}' stderr:\n{stderr.decode()}")
|
||||||
|
|
||||||
|
|
||||||
def format_tree_output(project_path, project_diff, submodule_diffs):
|
def format_tree_output(base_path, submodule_diffs, commit):
|
||||||
tree = f"📦 {os.path.basename(project_path)}\n"
|
"""Format the output in a tree structure."""
|
||||||
tree += "└── 📝 Changes:\n"
|
base_name = os.path.basename(base_path)
|
||||||
if project_diff:
|
base_diff = get_git_diff(base_path, True)
|
||||||
tree += indent_lines(project_diff, level=2)
|
|
||||||
else:
|
output = [f"📦 {base_name}"]
|
||||||
tree += indent_lines("✓ No changes", level=2)
|
output.append(f"└── 📝 {commit[base_path]}:")
|
||||||
|
for line in base_diff.split("\n"):
|
||||||
|
output.append(f" └── {line}")
|
||||||
|
|
||||||
if submodule_diffs:
|
if submodule_diffs:
|
||||||
tree += "\n└── 📚 Submodules:"
|
output.append("└── 📚 Submodules:")
|
||||||
for sub_path, diff in submodule_diffs.items():
|
for submodule_path, diff in submodule_diffs.items():
|
||||||
sub_name = os.path.basename(sub_path)
|
name = os.path.basename(submodule_path)
|
||||||
tree += f"\n └── 📦 {sub_name}\n"
|
output.append(f" └── 📦 {name}")
|
||||||
if diff:
|
for line in diff.split("\n"):
|
||||||
tree += indent_lines(diff, level=3, prefix="└── 📝 Changes:\n")
|
output.append(f" └── {line}")
|
||||||
else:
|
|
||||||
tree += " └── ✓ No changes"
|
|
||||||
return tree
|
|
||||||
|
|
||||||
|
return "\n".join(output)
|
||||||
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}')}"
|
|
||||||
|
|
||||||
|
|
||||||
async def push_code():
|
async def push_code():
|
||||||
git_user, git_pass, project_path = get_git_credentials()
|
git_user, git_pass, project_path = get_git_credentials()
|
||||||
os.chdir(project_path)
|
os.chdir(project_path)
|
||||||
print(f"Changed to project path: {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(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"git",
|
"git",
|
||||||
"remote",
|
"remote",
|
||||||
@ -251,14 +231,10 @@ async def push_code():
|
|||||||
git_repo = stdout.decode().strip().replace(".git", "").replace("https://", "")
|
git_repo = stdout.decode().strip().replace(".git", "").replace("https://", "")
|
||||||
remote_url = f"https://{git_user}:{git_pass}@{git_repo}"
|
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()
|
await commit_and_push_submodules()
|
||||||
|
|
||||||
# Generate and commit main project
|
|
||||||
commit_msg = gen_commit(project_path)
|
commit_msg = gen_commit(project_path)
|
||||||
|
commit[project_path] = commit_msg
|
||||||
print(f"Generated commit message:\n{commit_msg}\n")
|
print(f"Generated commit message:\n{commit_msg}\n")
|
||||||
|
|
||||||
commands = [
|
commands = [
|
||||||
@ -273,25 +249,19 @@ async def push_code():
|
|||||||
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
||||||
)
|
)
|
||||||
stdout, stderr = await proc.communicate()
|
stdout, stderr = await proc.communicate()
|
||||||
if stdout:
|
if proc.returncode != 0:
|
||||||
print(stdout.decode())
|
print(f"Command failed: {cmd}")
|
||||||
if stderr:
|
print(stderr.decode())
|
||||||
print(stderr.decode())
|
elif stderr:
|
||||||
|
# Not an error; just info output
|
||||||
|
print(stderr.decode())
|
||||||
|
|
||||||
# Prepare submodule diff tree
|
# Format and print the tree output
|
||||||
submodule_diffs = {}
|
tree_output = format_tree_output(project_path, submodule_diffs, commit)
|
||||||
if await has_submodules():
|
print("\nProject Status:")
|
||||||
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:")
|
|
||||||
print(tree_output)
|
print(tree_output)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(push_code())
|
asyncio.run(push_code())
|
||||||
|
Loading…
Reference in New Issue
Block a user