Created
November 16, 2023 18:04
-
-
Save codejake/f839601af7cb031a2184bc9f0867b128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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