Pygithub: 【例】複数ファイルのコミットの実例

作成日 2020年07月30日  ·  5コメント  ·  ソース: PyGithub/PyGithub

gitlabからgithubに移行しています。
ライブラリとしてhttps://github.com/python-gitlab/python-gitlabを使用します
複数のファイルをコミットするには、次の例を使用します: https ://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

create_git_commitはgithubでも同じことをしますが、ドキュメントがありません。
誰かがそれらのツリーなどのスタッフを作成する例を提供するのを手伝ってもらえますか?

どうもありがとう。

stale

最も参考になるコメント

これが私の実際の使用法から抽象化された完全な例です:
この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)

全てのコメント5件

さて、最終的に私は完全なプロセスで終わります...うまくいけば後で共有します...

これが私の実際の使用法から抽象化された完全な例です:
この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")

そのような複数のファイルコミットの一部としてファイルを削除する方法について何かアドバイスはありますか?

一部の投稿では、null shaを含むパスへの参照を使用して新しいツリーを作成すること、または既存のツリーを変更することについて説明しています。 ループして削除されたblobへのすべての参照を見つけることについてのアイデア?

このページは役に立ちましたか?
0 / 5 - 0 評価