import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from urllib.parse import urlparse
TLDR_REPO = "tldr-pages/tldr"
AI_TRAILER = "Assisted-by: Gemini 3.5 Flash"
def run(cmd, *, check=True, cwd=None):
result = subprocess.run(cmd, capture_output=True, text=True, cwd=cwd)
if check and result.returncode != 0:
print(f"Command failed: {' '.join(str(c) for c in cmd)}", file=sys.stderr)
if result.stdout:
print(result.stdout, file=sys.stderr)
if result.stderr:
print(result.stderr, file=sys.stderr)
sys.exit(result.returncode)
return result
def main():
if not shutil.which("gh"):
print("Error: 'gh' CLI tool is required but not found in PATH.", file=sys.stderr)
sys.exit(1)
root_dir = Path(__file__).resolve().parent.parent
owner = "l1a"
try:
url_check = subprocess.run(["git", "remote", "get-url", "origin"], capture_output=True, text=True, cwd=str(root_dir))
url = url_check.stdout.strip()
parsed = urlparse(url)
if parsed.scheme in ("https", "http") and parsed.netloc in ("github.com", "www.github.com"):
path_parts = parsed.path.strip("/").split("/")
if path_parts and path_parts[0]:
owner = path_parts[0]
elif url.startswith("git@github.com:"):
remainder = url[len("git@github.com:"):]
path_parts = remainder.split("/")
if path_parts and path_parts[0]:
owner = path_parts[0]
except Exception:
pass
local_page = root_dir / "docs" / "retch.md"
if not local_page.exists():
print(f"Error: Local tldr page not found at {local_page}", file=sys.stderr)
sys.exit(1)
print("Forking and cloning upstream tldr-pages/tldr...")
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
run(["gh", "repo", "fork", TLDR_REPO, "--clone", "--", str(tmp_path)])
target_dir = tmp_path / "pages" / "common"
if not target_dir.exists():
print(f"Error: Target directory {target_dir} not found in cloned tldr repo.", file=sys.stderr)
sys.exit(1)
target_file = target_dir / "retch.md"
is_update = target_file.exists()
shutil.copy(local_page, target_file)
branch_name = "add-retch-page" if not is_update else "update-retch-page"
run(["git", "checkout", "-b", branch_name], cwd=tmp_path)
run(["git", "add", "pages/common/retch.md"], cwd=tmp_path)
action = "add" if not is_update else "update"
commit_msg = f"retch: {action} page\n\n{AI_TRAILER}"
run(["git", "commit", "-m", commit_msg], cwd=tmp_path)
print("Pushing branch to your fork...")
run(["git", "push", "--force", "origin", branch_name], cwd=tmp_path)
print("Opening Pull Request on tldr-pages/tldr...")
pr_title = f"retch: {action} page"
pr_body = (
f"This PR {action}s the tldr-page entry for the `retch` system information fetcher.\n\n"
f"Command repository: https://github.com/l1a/retch\n\n"
f"Assisted-by: Gemini 3.5 Flash"
)
pr_cmd = [
"gh", "pr", "create",
"--repo", TLDR_REPO,
"--title", pr_title,
"--body", pr_body,
"--head", f"{owner}:{branch_name}"
]
pr_result = run(pr_cmd, cwd=tmp_path)
print("\nSuccess! Pull Request created upstream:")
print(pr_result.stdout.strip())
if __name__ == "__main__":
main()