""" id: git_tools title: Git Operations (unsafe) author: admin description: Clone, commit, push, checkout, and more. version: 0.1.0 license: Proprietary """ import subprocess import os def _run(cwd, cmd): p = subprocess.run( ["bash", "-lc", cmd], capture_output=True, text=True, cwd=cwd or None ) return { "cwd": cwd, "cmd": cmd, "stdout": p.stdout, "stderr": p.stderr, "exit_code": p.returncode, } class Tools: def clone(self, url: str, dest: str) -> dict: """git clone .""" return _run(None, f"git clone {url} {dest}") def status(self, repo: str) -> dict: """git -C status.""" return _run(repo, "git status") def checkout(self, repo: str, branch: str) -> dict: """git checkout .""" return _run(repo, f"git checkout {branch}") def create_branch(self, repo: str, branch: str) -> dict: """git checkout -b .""" return _run(repo, f"git checkout -b {branch}") def commit(self, repo: str, message: str, add_all: bool = True) -> dict: """git add (optional) and git commit -m.""" cmd = ( ("git add -A && " if add_all else "") + f"git commit -m {os.popen('printf %q "' + message + '"').read().strip()}" ) return _run(repo, cmd) def push(self, repo: str, remote: str = "origin", branch: str = "") -> dict: """git push [remote] [branch].""" return _run(repo, f"git push {remote} {branch}".strip()) def pull(self, repo: str, remote: str = "origin", branch: str = "") -> dict: """git pull [remote] [branch].""" return _run(repo, f"git pull {remote} {branch}".strip())