diff --git a/git-open b/git-open index 31457e82e4caf6eaa3d7e16fb44f460380673d8f..30f36280390e077815dad40b19b4a18d8acb8381 100755 --- a/git-open +++ b/git-open @@ -32,21 +32,24 @@ configuration = { def popen(str): p = subprocess.Popen(str.split(" "), - universal_newlines=True, - stdout=subprocess.PIPE) + 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)) return popen(f'git remote get-url {remote_name}').strip() + def to_http(url): for pattern in configuration['patterns']: if match := re.match(pattern['i'], url): @@ -69,23 +72,29 @@ def to_http(url): return "https://" + base + "/" + path raise Exception("URL doesn't start with either 'http' or 'git@'") + 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;33mWarn\x1b[m " + s) + def info(s): print("\x1b[0;32mInfo\x1b[m " + s) + def open_remote(remote): url = to_http(remote_url(remote)) xdg_open(url) info(f'opening {url}') + def main(args): out = popen("git remote") remotes = out.strip().split("\n") @@ -94,7 +103,7 @@ def main(args): do = open_remote if args.dry_run: - do = lambda x: print(to_http(remote_url(x))) + do = lambda x: print(to_http(remote_url(x))) # noqa: E731 if not remotes: err("No remotes") @@ -117,6 +126,7 @@ def main(args): else: err("All remotes failed") + def load_configuration(): global configuration # TODO possibly add ~/.config/git/open.yaml to list @@ -129,10 +139,12 @@ def load_configuration(): except FileNotFoundError: pass + if __name__ == "__main__": # Note that argparse gives `--help' and `-h', but git "eats" # `--help'. `-h' does however work. - parser = argparse.ArgumentParser(description='open git remotes in web browser') + parser = argparse.ArgumentParser( + description='open git remotes in web browser') parser.add_argument('-n', '--dry-run', action='store_true', dest='dry_run') parser.add_argument('remote', action='store', nargs='?') args = parser.parse_args() @@ -141,4 +153,3 @@ if __name__ == "__main__": load_configuration() main(args) -