使用Python操作GitHub Wiki仓库
有没有办法通过编程的方式(使用像 PyGithub
、GitPython
或 dulwich
这样的库)直接把文件加载到 MyRepo.wiki.git
这个仓库里?当然是用 Python 来做。
我可以很轻松地用 PyGithub
把文件上传到 MyRepo.git
仓库,但不幸的是,这个库没有提供与 MyRepo.wiki.git
仓库交互的接口或方法。
这是我如何把文件上传到 MyRepo.git
仓库的方式:
github_repo = github_account.get_user().get_repo('MyRepo')
head_ref = gh_repo.get_git_ref("heads/%s" % github_branch)
latest_commit = gh_repo.get_git_commit(head_ref.object.sha)
base_tree = latest_commit.tree
new_tree = gh_repo.create_git_tree(
[github.InputGitTreeElement(
path="test.txt",
mode='100755' if github_executable else '100644',
type='blob',
content="test"
)],
base_tree)
new_commit = gh_repo.create_git_commit(
message="test commit message",
parents=[latest_commit],
tree=new_tree)
head_ref.edit(sha=new_commit.sha, force=False)
那么,我该如何 以 MyRepo.wiki.git
仓库的方式来做 同样的事情 呢?如果你能提供一个使用 PyGithub 库的例子,那就太好了。
附言:我可以用 Gollum API 来做到这一点吗?
再附言:有没有人用过任何 Python 库来处理 *.wiki.git
的?我不太相信 :(
最后再说一句:如果我说得不够清楚:我并不想以任何方式创建一个本地仓库。我想要的只是动态修改仓库结构 - 就像我之前的例子那样。但这是针对 *.wiki.git
仓库。
谢谢!
1 个回答
你不能通过 GitHub 的网页 API 访问 GitHub 的 wiki,而 PyGithub 正是完全依赖这个 API。不过,你可以把 GitPython 指向 wiki 的 git 地址。之后,你就可以像访问其他仓库一样访问这个 git 仓库里的文件。
编辑
正如你提到的,如果你不想创建一个本地的 git 仓库克隆,我建议你这样做:
查看这段代码:https://github.com/github/gollum/blob/master/lib/gollum/frontend/app.rb,它定义了所有可能的外部 HTTP 接口。
目前还没有一个很好的 Python 封装库。不过如果你自己做一个(部分)的话,我建议使用一个 REST 客户端库,像这里提到的那样:https://stackoverflow.com/questions/2176561/which-is-the-best-python-library-to-make-rest-request-like-put-get-delete-pos
如果你现在觉得你的限制可以改变:
这个 文档 提供了一个很好的教程,涵盖了所有需要的内容,只要你对 git 有一点了解。简单来说就是:
import git
repo = git.Repo.clone_from("git@github.com:user/project.wiki.git", "some-path")
repo.index.add(["your-new-file"])
repo.index.commit("your message to the world")
repo.remotes.origin.push()