git.py 893 B

12345678910111213141516171819202122232425262728293031
  1. """
  2. Git command execution utility.
  3. Single source of truth for running git commands across all Trellis scripts.
  4. """
  5. from __future__ import annotations
  6. import subprocess
  7. from pathlib import Path
  8. def run_git(args: list[str], cwd: Path | None = None) -> tuple[int, str, str]:
  9. """Run a git command and return (returncode, stdout, stderr).
  10. Uses UTF-8 encoding with -c i18n.logOutputEncoding=UTF-8 to ensure
  11. consistent output across all platforms (Windows, macOS, Linux).
  12. """
  13. try:
  14. git_args = ["git", "-c", "i18n.logOutputEncoding=UTF-8"] + args
  15. result = subprocess.run(
  16. git_args,
  17. cwd=cwd,
  18. capture_output=True,
  19. text=True,
  20. encoding="utf-8",
  21. errors="replace",
  22. )
  23. return result.returncode, result.stdout, result.stderr
  24. except Exception as e:
  25. return 1, "", str(e)