public
Last active

A quick way to find all the forks of a particular github project.

  • Download Gist
find-forks.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#!/usr/bin/env python
 
import os
import urllib2
import json
import subprocess
 
user=None
repo=None
 
github_cmd="git config --get remote.github.url".split(" ")
origin_cmd="git config --get remote.origin.url".split(" ")
# we want to determine the user / repository name
# my convention is that my own projects use 'github' as the remote
# origin when I created it. And, obviously, a clone will use 'origin'.
# so try them each
try:
github = subprocess.check_output(github_cmd).strip()
user, repo = github.split('/')[-2:]
user = user.lstrip('git@github.com:')
repo = repo.rstrip('.git')
except subprocess.CalledProcessError:
pass # ok, so no remote called 'github', let's try origin
 
if user is None and repo is None:
try:
origin = subprocess.check_output(origin_cmd).strip()
user, repo = origin.split('/')[-2:]
repo = repo.rstrip('.git')
except subprocess.CalledProcessError:
print("Could not determine user or repo.")
os.exit(-1)
 
github_url='https://api.github.com/repos/%s/%s/forks'
resp = urllib2.urlopen(github_url % (user, repo))
if resp.code == 200:
content = resp.read()
data = json.loads(content)
for remote in data:
remote_add_cmd="git remote add %s %s" % (remote['owner']['login'], remote['clone_url'])
print remote_add_cmd
subprocess.call(remote_add_cmd.split(" "))
fetch_all_cmd="git fetch --all"
print fetch_all_cmd
subprocess.call(fetch_all_cmd.split(" "))

when I clone this repo https://github.com/muhku/calendar-ui.git - it fails. couldn't work it out I'm afraid.

but this url definitely works
https://api.github.com/repos/muhku/calendar-ui/

File "./find-forks.py", line 35, in
resp = urllib2.urlopen(github_url % (user, repo))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.