Skip to content
Snippets Groups Projects
Commit 00338214 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Git-open better error handling.

parent 5d1b083d
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,8 @@ def gitconf(field): ...@@ -17,7 +17,8 @@ def gitconf(field):
return popen("git config " + field) return popen("git config " + field)
def remote_url(remote_name): def remote_url(remote_name):
return gitconf("remote.{}.url".format(remote_name)) # return gitconf("remote.{}.url".format(remote_name))
return popen(f'git remote get-url {remote_name}').strip()
def to_http(url): def to_http(url):
if url[0:4] == "http": if url[0:4] == "http":
...@@ -25,7 +26,8 @@ def to_http(url): ...@@ -25,7 +26,8 @@ def to_http(url):
if url[0:4] == "git@": if url[0:4] == "git@":
base, path = url[4:].split(":") base, path = url[4:].split(":")
return "https://" + base + "/" + path return "https://" + base + "/" + path
return "http://www.nicememe.website" raise Exception("URL doesn't start with either 'http' or 'git@'")
# return "http://www.nicememe.website"
def xdg_open(item): def xdg_open(item):
subprocess.run(["xdg-open", item]) subprocess.run(["xdg-open", item])
...@@ -34,10 +36,10 @@ def err(s): ...@@ -34,10 +36,10 @@ def err(s):
print("\x1b[0;31mError\x1b[m " + s) print("\x1b[0;31mError\x1b[m " + s)
def warn(s): def warn(s):
print("\x1b[0;32mWarn\x1b[m " + s) print("\x1b[0;33mWarn\x1b[m " + s)
def info(s): def info(s):
print("\x1b[0;33mInfo\x1b[m " + s) print("\x1b[0;32mInfo\x1b[m " + s)
def main(args): def main(args):
out = popen("git remote") out = popen("git remote")
...@@ -49,23 +51,17 @@ def main(args): ...@@ -49,23 +51,17 @@ def main(args):
if not remotes: if not remotes:
err("No remotes") err("No remotes")
if len(remotes) == 1:
if remote and remotes[0] != remote:
err("No remote with name " + remote)
return
xdg_open(to_http(remote_url(remotes[0]))) for remote in remotes:
try:
url = to_http(remote_url(remote))
xdg_open(url)
info(f'opening {url}')
break
except Exception as e:
warn(str(e))
else: else:
if remote: err("All remotes failed")
if remote in remotes:
xdg_open(to_http(remote_url(remote)))
return
else:
err("No remote with name " + remote)
return
info("Multiple remotes found, defaulting to " + remotes[0])
xdg_open(to_http(remote_url(remotes[0])))
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) main(sys.argv)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment