Pygithub: [Example] Full example of commit multiple file

Created on 30 Jul 2020  路  5Comments  路  Source: PyGithub/PyGithub

I am moving from gitlab to github.
We use https://github.com/python-gitlab/python-gitlab as the library
To make a commit for multiple files we use exactly this example: https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

I find create_git_commit would do the same thing in github but lack of docs.
Could someone help to provide an example of creating those Tree etc.. staffs ?

Thanks very much.

stale

Most helpful comment

Here is a full example abstracted from my real usage:
I get a lot hints from this https://github.com/PyGithub/PyGithub/issues/863#issuecomment-517927446

To commit to master branch

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)

All 5 comments

well, finally I end up with the full process... hopefully will share later...

Here is a full example abstracted from my real usage:
I get a lot hints from this https://github.com/PyGithub/PyGithub/issues/863#issuecomment-517927446

To commit to master branch

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)

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I wanted to do something similar (i.e. commit multiple files at once), but this time in a branch. Below is working code (let me know if it can be optimised though)

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 @xpdable 
# 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 @xpdable 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")

Any advice on how to delete files as part of such multiple file commits?

Some posts talk about creating new trees with references to paths with null sha's, or modifying existing trees incl. ideas about looping and finding all references to the deleted blobs?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

psychemedia picture psychemedia  路  5Comments

PeterJCLaw picture PeterJCLaw  路  6Comments

hren-ron picture hren-ron  路  6Comments

rthill91 picture rthill91  路  4Comments

jacquev6 picture jacquev6  路  3Comments