可以将GitHub存储库下载到我的本地计算机

时间:2014-02-18 05:51:15

标签: api github repository github-api

你好朋友我刚刚开始使用GitHub,我只是想知道可以通过使用GitHub Api或Api库(即用于Github api的python库“pygithub3”)将github存储库下载到我的本地计算机上

2 个答案:

答案 0 :(得分:2)

使用github3.py,您可以通过执行以下操作克隆所有存储库(包括分支和私有存储库):

import github3
import subprocess


g = github3.login('username', 'password')
for repo in g.iter_repos(type='all'):
    subprocess.call(['git', 'clone', repo.clone_url])

如果您要克隆任意存储库,可以执行以下操作:

import github3
import subprocess


r = github3.repository('owner', 'repository_name')
subprocess.call(['git', 'clone', repo.clone_url])

pygithub3一年多来没有积极开发。我建议不要使用它,因为它没有维护,并且缺少GitHub从那时起对其API进行的大量添加。

答案 1 :(得分:0)

this Gist所示,最简单的解决方案就是调用git clone。

#!/usr/bin/env python
# Script to clone all the github repos that a user is watching
import requests
import json
import subprocess

# Grab all the URLs of the watched repo
user = 'jharjono'
r = requests.get("http://github.com/api/users/%s/subscriptions" % (user))
repos = json.loads(r.content)
urls = [repo['url'] for repo in repos['repositories']]

# Clone them all
for url in urls:
    cmd = 'git clone ' + url
    pipe = subprocess.Popen(cmd, shell=True)
    pipe.wait()

print "Finished cloning %d watched repos!" % (len(urls))

This gist使用 pygithub3 ,会在找到的repos上调用git clone:

#!/usr/bin/env python

import pygithub3

gh = None


def gather_clone_urls(organization, no_forks=True):
    all_repos = gh.repos.list(user=organization).all()
    for repo in all_repos:

        # Don't print the urls for repos that are forks.
        if no_forks and repo.fork:
            continue

        yield repo.clone_url


if __name__ == '__main__':
    gh = pygithub3.Github()

    clone_urls = gather_clone_urls("gittip")
    for url in clone_urls:
        print url