-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgremote
executable file
·93 lines (75 loc) · 2.37 KB
/
gremote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from typing import Any
import webbrowser
import subprocess
import sys
import re
def main(args=None):
try:
if len(args) >= 1:
if args[0].startswith('-'):
switch(args[0], *args[1:])
exit(0)
elif args[-1].startswith('-'):
switch(args[-1], *args[:-1])
exit(0)
remote = args[0] if len(args) >= 1 else "origin"
url = subprocess.check_output(
["git", "remote", "get-url", remote]).decode("utf-8").strip()
# parse the url for hostname and path
git_url = re.sub(r"^"
r"(?:https?://|git@)"
r"([^:/]*)"
r"(?::|/)"
r"(.*)"
r"(\.git)?"
r"$", r"https://\1/\2", url)
print(f"Opening {remote} git remote: {git_url}")
webbrowser.open(git_url, new=0, autoraise=True)
exit(0)
except KeyboardInterrupt:
print("Aborted")
exit(0)
except subprocess.CalledProcessError:
print("No remote found")
exit(1)
except Exception as e:
print(e)
exit(1)
def switch(s: str, *args: Any) -> None:
""" Map the switch to the corresponding function and execute it. """
# map switches to functions
# if no function is found, print help
switches = {
"-h": help,
"--help": help,
}
if s in switches:
switches[s](*args)
else:
raise Exception(f"Unknown switch: {s}. Use -h or --help for help.")
def help(*args: Any, **kwargs: Any) -> None:
print("""
==========================================================
gremote - open git remote url in the browser
==========================================================
Usage:
python gremote [options] [remote]
Options:
-h, --help
Print this help message and exit.
Description:
This script opens the git remote url in a browser.
If no remote is provided, the `origin` remote is used.
Examples:
python gremote
Open the `origin` remote url.
python gremote dev
Open the `dev` remote url.
python gremote -h
Print this help message and exit.
""")
if __name__ == '__main__':
# sys.argv[0] is the name of the script
# sys.argv[1:] is the list of arguments
main(sys.argv[1:])