diff --git a/scripts/interpreter/gen_commit.py b/scripts/interpreter/gen_commit.py index 08a1e45..e281f46 100755 --- a/scripts/interpreter/gen_commit.py +++ b/scripts/interpreter/gen_commit.py @@ -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())