Update from updated-inputs-2025-05-24-01-03
This commit is contained in:
@@ -5,20 +5,18 @@ from dataclasses import dataclass
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
REMOTE_TYPES = [
|
REMOTE_TYPES = {
|
||||||
{
|
"github": {
|
||||||
# github
|
|
||||||
"match": re.compile(r'git@github.com:(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
"match": re.compile(r'git@github.com:(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
||||||
"format-branch": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/",
|
"format-branch": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/",
|
||||||
"format-commit": lambda g: f"https://github.com/{g.username}/{g.project}/commit/{g.commit}/",
|
"format-commit": lambda g: f"https://github.com/{g.username}/{g.project}/commit/{g.commit}/",
|
||||||
},
|
},
|
||||||
{
|
"gitea": {
|
||||||
# gitea
|
|
||||||
"match": re.compile(r'(?P<gituser>[\w\.-]+)@(?P<host>[\w\.-]+):(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
"match": re.compile(r'(?P<gituser>[\w\.-]+)@(?P<host>[\w\.-]+):(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
||||||
"format-branch": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/",
|
"format-branch": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/",
|
||||||
"format-commit": lambda g: f"https://{g.host}/{g.username}/{g.project}/commit/{g.commit}/",
|
"format-commit": lambda g: f"https://{g.host}/{g.username}/{g.project}/commit/{g.commit}/",
|
||||||
},
|
},
|
||||||
]
|
}
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FormatArgs:
|
class FormatArgs:
|
||||||
@@ -29,6 +27,11 @@ class FormatArgs:
|
|||||||
commit: str = None
|
commit: str = None
|
||||||
branch: str = None
|
branch: str = None
|
||||||
|
|
||||||
|
def is_git_repo():
|
||||||
|
s = subprocess.run(["git", "rev-parse"], capture_output=True, text=True)
|
||||||
|
|
||||||
|
return s.returncode == 0
|
||||||
|
|
||||||
def get_remote_branch():
|
def get_remote_branch():
|
||||||
s = subprocess.run(["git", "status", "--porcelain", "-uno", "-b", "--no-ahead-behind"], capture_output=True, text=True)
|
s = subprocess.run(["git", "status", "--porcelain", "-uno", "-b", "--no-ahead-behind"], capture_output=True, text=True)
|
||||||
|
|
||||||
@@ -68,19 +71,36 @@ def main():
|
|||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument("--branch", dest="display_branch", action='store_true', help="Display link to branch, instead to commit")
|
parser.add_argument("--branch", dest="display_branch", action='store_true', help="Display link to branch, instead to commit")
|
||||||
|
parser.add_argument("--remote-type", dest="remote_type", choices=REMOTE_TYPES.keys(), help="Specify remote type")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if not is_git_repo():
|
||||||
|
print("Not a git repo")
|
||||||
|
|
||||||
|
exit(1)
|
||||||
|
|
||||||
r = get_remote_branch()
|
r = get_remote_branch()
|
||||||
|
|
||||||
remote_url = get_remote_url(r["remote"])
|
remote_url = get_remote_url(r["remote"])
|
||||||
|
|
||||||
for remote_type in REMOTE_TYPES:
|
selected_remote_types = REMOTE_TYPES
|
||||||
|
|
||||||
|
if args.remote_type is not None:
|
||||||
|
selected_remote_types = {
|
||||||
|
args.remote_type: REMOTE_TYPES[args.remote_type],
|
||||||
|
}
|
||||||
|
|
||||||
|
remote_type_found = False
|
||||||
|
|
||||||
|
for remote_type_name, remote_type in selected_remote_types.items():
|
||||||
m = remote_type["match"].match(remote_url)
|
m = remote_type["match"].match(remote_url)
|
||||||
|
|
||||||
if m is None:
|
if m is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
remote_type_found = True
|
||||||
|
|
||||||
g = FormatArgs(**m.groupdict())
|
g = FormatArgs(**m.groupdict())
|
||||||
|
|
||||||
if args.display_branch:
|
if args.display_branch:
|
||||||
@@ -92,6 +112,10 @@ def main():
|
|||||||
print(remote_type["format-commit"](g))
|
print(remote_type["format-commit"](g))
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if not remote_type_found:
|
||||||
|
print("No remote type matched")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user