Created
December 2, 2025 02:04
-
-
Save leliel12/86ffcf1f047d36404f85a1773702784f 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 | |
| """ | |
| Git Open - Extract and display Git remote URLs, open in browser | |
| Author: Juan BC | |
| Email: [email protected] | |
| License: BSD-3-Clause | |
| Installation: | |
| 1. Copy this script to a directory in your PATH (e.g., ~/.local/bin/) | |
| $ cp git-open ~/.local/bin/ | |
| 2. Make it executable: | |
| $ chmod +x ~/.local/bin/git-open | |
| 3. Ensure ~/.local/bin is in your PATH (add to ~/.bashrc or ~/.zshrc if needed): | |
| export PATH="$HOME/.local/bin:$PATH" | |
| 4. Now you can use it as a git subcommand: | |
| $ git open | |
| Requirements: | |
| - Python 3.6+ | |
| - Git repository | |
| """ | |
| import subprocess | |
| import sys | |
| import re | |
| import webbrowser | |
| def is_git_repo(): | |
| """Check if the current directory is a Git repository""" | |
| try: | |
| subprocess.run( | |
| ["git", "rev-parse", "--git-dir"], | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| ) | |
| return True | |
| except subprocess.CalledProcessError: | |
| return False | |
| def get_remote_url(remote_name="origin"): | |
| """ | |
| Get the URL of the specified remote | |
| Args: | |
| remote_name: Name of the remote (default 'origin') | |
| Returns: | |
| Remote URL or None if it doesn't exist | |
| """ | |
| try: | |
| result = subprocess.run( | |
| ["git", "remote", "get-url", remote_name], | |
| check=True, | |
| capture_output=True, | |
| text=True, | |
| ) | |
| return result.stdout.strip() | |
| except subprocess.CalledProcessError: | |
| return None | |
| def get_all_remotes(): | |
| """Get all configured remotes""" | |
| try: | |
| result = subprocess.run( | |
| ["git", "remote", "-v"], check=True, capture_output=True, text=True | |
| ) | |
| return result.stdout.strip() | |
| except subprocess.CalledProcessError: | |
| return None | |
| def format_url(url): | |
| """ | |
| Format the URL for readable display | |
| Convert SSH URLs to HTTPS format when possible | |
| """ | |
| # Convert SSH format to HTTPS for GitHub, GitLab, Bitbucket | |
| ssh_pattern = r"git@([^:]+):(.+?)(?:\.git)?$" | |
| match = re.match(ssh_pattern, url) | |
| if match: | |
| host = match.group(1) | |
| path = match.group(2) | |
| https_url = f"https://{host}/{path}" | |
| return url, https_url | |
| return url, None | |
| def main(): | |
| """Main function""" | |
| # Check if we are in a Git repository | |
| if not is_git_repo(): | |
| print("❌ Error: No estás en un repositorio Git", file=sys.stderr) | |
| sys.exit(1) | |
| print("🔍 Searching Git repository remotes...\n") | |
| # Get 'origin' remote URL | |
| origin_url = get_remote_url("origin") | |
| if origin_url: | |
| print("📍 Remote 'origin':") | |
| print(f" {origin_url}") | |
| # Display HTTPS version if SSH | |
| original, https = format_url(origin_url) | |
| if https and https != original: | |
| print(f" HTTPS: {https}") | |
| print() | |
| # Display all remotes | |
| all_remotes = get_all_remotes() | |
| if all_remotes: | |
| print("📋 All configured remotes:") | |
| print("-" * 60) | |
| for line in all_remotes.split("\n"): | |
| if line.strip(): | |
| # Format the output | |
| parts = line.split() | |
| if len(parts) >= 2: | |
| name = parts[0] | |
| url = parts[1] | |
| operation = parts[2] if len(parts) > 2 else "" | |
| print(f" {name:12} {url:40} {operation}") | |
| print("-" * 60) | |
| webbrowser.open(https) | |
| else: | |
| print("⚠️ No configured remotes found") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment