Created
April 2, 2012 20:04
-
-
Save waylan/2286857 to your computer and use it in GitHub Desktop.
Parsing github repo urls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urlparse | |
formats = [ | |
'git@github.com:org-or-user/project.git', # ssh read/write | |
'git://github.com/org-or-user/project.git', # ssh read | |
'https://username@github.com/org-or-user/project.git', # http read/write | |
'https://github.com/org-or-user/project.git' # http read | |
] | |
for format in formats: | |
r = urlparse.urlparse(format) | |
print ' URL:', format | |
if r.scheme == 'git' or (r.scheme == 'https' and r.username is None): | |
print 'Sorry, you do not have write access to this Repository.' | |
elif r.scheme == 'https' and r.hostname == 'github.com': | |
gh_user = r.username | |
gh_org, gh_project = r.path.lstrip('/').rstrip('.git').split('/', 1) | |
print 'GH_USER:', gh_user | |
print 'GH_ORG:', gh_org | |
print 'GH_PROJECT:', gh_project | |
elif format.startswith('git@github.com:'): | |
gh_org, gh_project = format[15:].lstrip('/').rstrip('.git').split('/', 1) | |
print 'GH_ORG:', gh_org | |
print 'GH_PROJECT:', gh_project | |
else: | |
print 'Your Repo does not appear to be hosted on github.com.' | |
print # blank line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment