Skip to content

Instantly share code, notes, and snippets.

@codejake
Created November 16, 2023 18:04
Show Gist options
  • Select an option

  • Save codejake/f839601af7cb031a2184bc9f0867b128 to your computer and use it in GitHub Desktop.

Select an option

Save codejake/f839601af7cb031a2184bc9f0867b128 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# A simple script that checks the current directory for a github remote
# and if found, opens the remote repo in a browser window.
# Works on macOS, at least. Can be adapted for Windows & Linux.
import subprocess
import sys
try:
# Execute 'git remote -v' and capture the output
result = subprocess.run(
["git", "remote", "-v"], capture_output=True, text=True, check=True
)
# Find the line containing 'github.com'
repo_line = next(line for line in result.stdout.split("\n") if "github.com" in line)
# Extract the repository name
repo = repo_line.split("\t")[1].split(":")[1].split(".git")[0]
# Open the GitHub repository in a browser
subprocess.run(["open", f"https://github.com/{repo}"], check=True)
except Exception:
# Error handling
print("No GitHub remote found", file=sys.stderr)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment