-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
repo.py
executable file
·80 lines (64 loc) · 1.77 KB
/
repo.py
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
#!/usr/bin/env python3
"""
Open the website for this repo (or upstream).
"""
from __future__ import annotations
import argparse
import os
import git # pip install GitPython
from termcolor import cprint # pip install termcolor
def clean_url(url: str) -> str:
# git@github.com:user/repo.git
# ->
# https://github.com/user/repo.git
if url.startswith("git@"):
url = "https://" + url.removeprefix("git@").replace(":", "/")
# https://github.com/user/repo.git
# ->
# https://github.com:user/repo
url = url.removesuffix(".git")
return url
def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-u",
"--upstream",
action="store_true",
help="Open the upstream instead (if there is one)",
)
parser.add_argument(
"tab",
nargs="?",
help="Open the named tab",
)
parser.add_argument(
"-n",
"--dry-run",
action="store_true",
help="Show but don't open webpages",
)
args = parser.parse_args()
# Find the user/repo of the Git origin
git_repo = git.Repo(".")
url = list(git_repo.remotes.origin.urls)[0]
if args.upstream:
try:
url = list(git_repo.remotes.upstream.urls)[0]
except AttributeError: # 'IterableList' object has no attribute 'upstream'
cprint("No upstream, opening origin", "yellow")
print(url)
url = clean_url(url)
if args.tab:
if "gitlab" in url:
url += "/-"
url += "/" + args.tab
cmd = "open " + url
print(cmd)
if not args.dry_run:
os.system(cmd)
if __name__ == "__main__":
main()
# End of file