From 00338214103d73b72dbb1ea3db62e49ce532b6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se> Date: Sun, 4 Oct 2020 00:00:25 +0200 Subject: [PATCH] Git-open better error handling. --- git-open | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/git-open b/git-open index 1683aac..22c21ad 100755 --- a/git-open +++ b/git-open @@ -17,7 +17,8 @@ def gitconf(field): return popen("git config " + field) 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): if url[0:4] == "http": @@ -25,7 +26,8 @@ def to_http(url): if url[0:4] == "git@": base, path = url[4:].split(":") 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): subprocess.run(["xdg-open", item]) @@ -34,10 +36,10 @@ def err(s): print("\x1b[0;31mError\x1b[m " + s) def warn(s): - print("\x1b[0;32mWarn\x1b[m " + s) + print("\x1b[0;33mWarn\x1b[m " + s) def info(s): - print("\x1b[0;33mInfo\x1b[m " + s) + print("\x1b[0;32mInfo\x1b[m " + s) def main(args): out = popen("git remote") @@ -49,23 +51,17 @@ def main(args): if not 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: - if remote: - 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]))) + err("All remotes failed") if __name__ == "__main__": main(sys.argv) -- GitLab