我正在从 gitlab 迁移到 github。
我们使用https://github.com/python-gitlab/python-gitlab作为库
为了提交多个文件,我们使用了这个例子: https ://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html
我发现 create_git_commit 会在 github 中做同样的事情,但缺少文档。
有人可以帮忙提供一个创建这些树等的例子。员工吗?
非常感谢。
好吧,最后我结束了整个过程......希望稍后会分享......
这是从我的实际用法中抽象出来的完整示例:
我从这个https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446 得到了很多提示
提交到主分支
import github
g = github.Github(base_url="https://git.mycompany.com/api/v3",login_or_token="mytoken")
repo = g.get_repo('myorg/myrepo')
# you can read file content into blob, here just use str for example
blob1 = repo.create_git_blob("file-content", "utf-8")
element1 = github.InputGitTreeElement(path="path-in-repo.ext", mode='100644', type='blob', sha=blob1.sha)
blob2 = repo.create_git_blob("file-content2", "utf-8")
element2 = github.InputGitTreeElement(path="folder/path-in-repo.ext", mode='100644', type='blob', sha=blob2.sha)
head_sha = repo.get_branch('master').commit.sha
base_tree = repo.get_git_tree(sha=head_sha)
tree = repo.create_git_tree([element1, element2], base_tree)
parent = repo.get_git_commit(sha=head_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
master_refs = self.github_repo.get_git_ref('heads/master')
master_ref.edit(sha=commit.sha)
此问题已自动标记为过时,因为它最近没有活动。 如果没有进一步的活动,它将被关闭。 感谢你的贡献。
我想做类似的事情(即一次提交多个文件),但这次是在一个分支中。 下面是工作代码(让我知道它是否可以优化)
import github
g = github.Github("mytoken")
repo = g.get_repo('myorg/myrepo')
blob1 = repo.create_git_blob("file-content", "utf-8")
element1 = github.InputGitTreeElement(path="path-in-repo.ext", mode='100644', type='blob', sha=blob1.sha)
blob2 = repo.create_git_blob("file-content2", "utf-8")
element2 = github.InputGitTreeElement(path="folder/path-in-repo.ext", mode='100644', type='blob', sha=blob2.sha)
head_sha = repo.get_branch('master').commit.sha
# everything above is identical to <strong i="6">@xpdable</strong>
# but here I create the branch
branch = repo.create_git_ref(ref=f"refs/heads/branch-name", sha=head_sha)
branch_sha = repo.get_branch("branch-name").commit.sha
# similar to <strong i="7">@xpdable</strong> but replacing head_sha with branch_sha
base_tree = repo.get_git_tree(sha=branch_sha)
tree = repo.create_git_tree([configuration_element, stats_element], base_tree)
parent = repo.get_git_commit(sha=branch_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
branch_refs = repo.get_git_ref("heads/branch-name")
branch_refs.edit(sha=commit.sha)
# optional : open a PR with the new branch
repo.create_pull(title="PR title", body="PR body", base="master", head="branch-name")
关于如何删除文件作为此类多个文件提交的一部分的任何建议?
一些帖子讨论了创建新树并引用具有空 sha 的路径,或修改现有树,包括。 关于循环和查找对已删除 blob 的所有引用的想法?
最有用的评论
这是从我的实际用法中抽象出来的完整示例:
我从这个https://github.com/PyGithub/PyGithub/issues/863#issuecomment -517927446 得到了很多提示
提交到主分支