pygithub get_contents thorws 错误 UnknownObjectException

问题描述

我正在 Windows 上编写一个 python 脚本,它将以用户名作为输入,并下载他的存储库的主分支代码。我正在使用 pygithub 模块。我正在使用 github 模块的 get_contents 方法下载代码。 在下面的 code_snippet 中,python 执行 get_content 方法时会抛出 UnkNownObjectException

我传递的 Server_path 是 "./Gitrepo"。 下面是代码片段:

Server_path='Path where I want to save the code'
sha=branch
repository is git repo

def download_directory(repository,sha,server_path):
    """
    Download all contents at server_path with commit tag sha in
    the repository.
    """
    if os.path.exists(server_path):
        shutil.rmtree(server_path)

    os.makedirs(server_path)
    contents = repository.get_contents(path=server_path,ref=sha)
    

    for content in contents:
        #print("content.path %s" % content.path)
        if content.type == 'dir':
            print("content.path %s" % content.path)
            download_directory(repository,content.path)
        else:
            try:
                path = content.path
                file_content = repository.get_contents(path,ref=sha)
                file_data = base64.b64decode(file_content.content)
                file_out = open(content.name,"wb+")
                file_out.write(file_data)
                file_out.close()
            except (GithubException,IOError) as exc:
                logging.error('Error processing %s: %s',content.path,exc)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)