#!/usr/bin/env python3

import sys
import subprocess

from tig_helpers import get_site, get_github_user_and_repo, get_origin_url

def main():
	commit     = sys.argv[1]
	origin_url = get_origin_url()
	site       = get_site(origin_url)
	if site == "github.com":
		user, repo = get_github_user_and_repo(origin_url)
		url        = "https://github.com/%s/%s/commit/%s" % (user, repo, commit)
	elif site == "kernel.ubuntu.com":
		repo_path  = origin_url.replace("git://kernel.ubuntu.com/", "", 1)
		url        = "http://kernel.ubuntu.com/git/%s/commit/?id=%s" % (repo_path, commit)
	elif site == "anonscm.debian.org":
		repo_path  = origin_url.replace("https://anonscm.debian.org/git/", "", 1)
		url        = "https://anonscm.debian.org/git/%s/commit/?id=%s" % (repo_path, commit)
	elif site == "chromium.googlesource.com":
		repo_path  = origin_url.replace("https://chromium.googlesource.com/", "", 1).rstrip("/")
		url        = "https://chromium.googlesource.com/%s/+/%s" % (repo_path, commit)

	subprocess.call(["xdg-open", url])

if __name__ == "__main__":
	main()
