| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #!/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)
|