Python在没有本地工作目录的Github远程仓库上更新文件

时间:2016-10-28 15:06:49

标签: python github github-api

这是关于在没有本地工作目录(Python push files to Github remote repo without local working directory)的情况下推送到远程仓库的问题的后续问题。我想知道如果文件已经存在于远程仓库上,我只想用同名的修改文件更新它? (例如,相当于在Github网站上,上传遥控器上已存在的文件的修改版本)

编辑:我们提出了一个解决方案:

public static double max(Object[] objects, Measurer m)
{
    double largest = 0;
    for (int i=0; i<objects.length; i++)
    {
        if(m.measure(objects[i]) > largest)
        {
            largest = m.measure(objects[i]);
        }
    }
    return largest;
}

但是,虽然这在一台计算机上成功运行,但它在另一台计算机上引发了错误(具体而言,第一行将获得AttributeError)。这是因为github3的潜在版本不同吗?

1 个答案:

答案 0 :(得分:1)

似乎很清楚,在github3版本0.9.6下,到目前为止你将获得pip install github3.pyhttps://github3py.readthedocs.io/en/master/#installation),这将有效(对远程仓库进行更新而不用任何本地工作目录):

def update_to_git(username,password,path,account,repo,message):
    files_to_upload = [path]
    gh = github3.login(username=username, password=password)
    repository = gh.repository(account, repo)
    for file_info in files_to_upload:
        with open(file_info, 'rb') as fd:
            contents = fd.read()
        contents_object = repository.contents(file_info)
        contents_object.update(message,contents)

但是,如果你有github3版本1.0.0a4,这将无法正常工作。具体来说,您将获得AttributeError行的contents_object = repository.contents(file_info),这可能是由于github3中的实现发生了变化。

相关问题