Pygithub: [مثال] مثال كامل لارتكاب ملف متعدد

تم إنشاؤها على ٣٠ يوليو ٢٠٢٠  ·  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 ستفعل الشيء نفسه في جيثب لكن تفتقر إلى المستندات.
هل يمكن لشخص أن يساعد في تقديم مثال على إنشاء تلك الشجرة وما إلى ذلك .. الموظفين؟

شكرا جزيلا.

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")

أي نصيحة حول كيفية حذف الملفات كجزء من مثل هذه الملفات المتعددة؟

تتحدث بعض المنشورات عن إنشاء أشجار جديدة مع إشارات إلى مسارات ذات قيم خالية ، أو تعديل الأشجار الموجودة بما في ذلك. أفكار حول التكرار والعثور على جميع الإشارات إلى النقط المحذوفة؟

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات

القضايا ذات الصلة

rthill91 picture rthill91  ·  4تعليقات

nixoz2k7 picture nixoz2k7  ·  7تعليقات

surajjacob picture surajjacob  ·  4تعليقات

BBI-YggyKing picture BBI-YggyKing  ·  5تعليقات

jacquev6 picture jacquev6  ·  3تعليقات