| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #!/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()
- commit = oneliner('git', 'rev-parse', 'HEAD')
- local_branch = oneliner('git', 'name-rev', '--name-only', 'HEAD')
- tag = None
- if local_branch.startswith('tags/'):
- tag = local_branch[5:]
- if tag.endswith('^0'):
- tag = tag[:-2]
- remote_url = oneliner('git', 'config', 'remote.origin.url')
- else:
- tracking_branch = oneliner('git', 'config', 'branch.%s.merge' % local_branch)
- if tracking_branch.startswith('refs/heads/'):
- tracking_branch = tracking_branch[len('refs/heads/'):]
- else:
- raise Exception("can't handle " + tracking_branch)
- tracking_remote = oneliner('git', 'config', 'branch.%s.remote' % local_branch)
- remote_url = oneliner('git', 'config', 'remote.%s.url' % tracking_remote)
- relpath = oneliner('git', 'rev-parse', '--show-prefix')[:-1]
- 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.startswith('github.com-'):
- hostname = 'github.com'
- obj_type = 'tree'
- if len(sys.argv) == 2:
- obj_type = 'blob'
- repo_url = urllib.parse.urlunparse(('https', hostname, parsed.path, '', '', ''))
- def url_for(treeish):
- dir_url = '%s/%s/%s' % (repo_url, obj_type, treeish)
- if relpath:
- dir_url = '%s/%s' % (dir_url, relpath)
- if len(sys.argv) == 2:
- return '%s/%s' % (dir_url, sys.argv[1])
- else:
- return dir_url
- if tag is not None:
- print(url_for(tag))
- else:
- print(url_for(tracking_branch))
- print(url_for(commit))
|