Skip to content

Instantly share code, notes, and snippets.

@waylan
Created April 2, 2012 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylan/2286857 to your computer and use it in GitHub Desktop.
Save waylan/2286857 to your computer and use it in GitHub Desktop.
Parsing github repo urls.
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