Skip to content
Snippets Groups Projects
Commit 857c83bb authored by Hugo Hornquist's avatar Hugo Hornquist
Browse files

Add git-open.

parents
No related branches found
No related tags found
No related merge requests found
git-open 0 → 100644
#!/usr/bin/env python3
import sys
import subprocess
def popen(str):
p = subprocess.Popen(str.split(" "),
universal_newlines=True,
stdout=subprocess.PIPE)
out, err = p.communicate()
p.wait()
if p.returncode != 0:
sys.exit(1)
return out
def gitconf(field):
return popen("git config " + field)
def remote_url(remote_name):
return gitconf("remote.{}.url".format(remote_name))
def to_http(url):
if url[0:4] == "http":
return url
if url[0:4] == "git@":
base, path = url[4:].split(":")
return "https://" + base + "/" + path
return "http://www.nicememe.website"
def xdg_open(item):
subprocess.run(["xdg-open", item])
def err(s):
print("\x1b[0;31mError\x1b[m " + s)
def warn(s):
print("\x1b[0;32mWarn\x1b[m " + s)
def info(s):
print("\x1b[0;33mInfo\x1b[m " + s)
def main(args):
out = popen("git remote")
remotes = out.strip().split("\n")
remote = False
if len(args) == 2:
remote = sys.argv[1]
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])))
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])))
if __name__ == "__main__":
main(sys.argv)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment