raylu 8 anni fa
parent
commit
2db6409b40
1 ha cambiato i file con 40 aggiunte e 0 eliminazioni
  1. 40 0
      git-url

+ 40 - 0
git-url

@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+import re
+import subprocess
+import sys
+import urllib.parse
+
+def oneliner(*args):
+	return subprocess.check_output(args, universal_newlines=True).strip()
+
+local_branch = oneliner('git', 'name-rev', '--name-only', 'HEAD')
+tracking_remote = oneliner('git', 'config', 'branch.%s.remote' % local_branch)
+tracking_branch = oneliner('git', 'config', 'branch.%s.merge' % local_branch)
+remote_url = oneliner('git', 'config', 'remote.%s.url' % tracking_remote)
+relpath = oneliner('git', 'rev-parse', '--show-prefix')[:-1]
+
+if tracking_branch.startswith('refs/heads/'):
+	tracking_branch = tracking_branch[len('refs/heads/'):]
+else:
+	raise Exception("can't handle " + tracking_branch)
+
+remote_url = re.sub(r'^(.+@.+):(.+)$', r'ssh://\1/\2', remote_url)
+if remote_url.endswith('.git'):
+	remote_url = remote_url[:-len('.git')]
+parsed = urllib.parse.urlparse(remote_url)
+hostname = parsed.hostname
+if hostname == 'github.com-private':
+	hostname = 'github.com'
+
+if hostname == 'github.com':
+	repo_url = urllib.parse.urlunparse(('https', hostname, parsed.path, '', '', ''))
+	dir_url = '%s/tree/%s' % (repo_url, tracking_branch)
+	if relpath:
+		dir_url = '%s/%s' % (dir_url, relpath)
+	if len(sys.argv) == 2:
+		print('%s/%s' % (dir_url, sys.argv[1]))
+	else:
+		print(dir_url)
+else:
+	raise Exception("can't handle " + parsed.hostname)