[英]How to make an initial commit using pygit2?
在某些情況下,我正在開發一個 Python 模塊,該模塊是從我用來管理 GitLab 實例的當前意大利面條代碼創建的,以發布和評分大學課程的作業。 工作可以在這里找到。 我的應用程序要求我下載一個存儲庫作為“模板”作業發布給所有學生,所以我需要為我課程中的所有學生使用相同的文件樹和一個新的 git 存儲庫(因此需要一遍又一遍地創建初始提交再次)。
這里的技巧,我在任何地方都找不到記錄(在這篇文章中似乎缺少 pygit2 的文檔),是將create_commit
的第一個參數設置為"HEAD"
,而不是例如"refs/heads/master"
,如這個例子。 也許這對於 git 向導來說是顯而易見的,但我花了一秒鍾才意識到。
import pygit2 as git
template_proj_location = "/file/path/to/the/template"
# 'proj' is a python-gitlab object for the GitLab project this repo needs to get pushed to
repo = git.init_repository(template_proj_location, initial_head='master', origin = proj.ssh_url_to_repo)
(index := repo.index).add_all()
index.write()
tree = index.write_tree()
me = git.Signature("My Name", "myemail@email.domain.edu")
repo.create_commit("HEAD", me, me, "commit msg", tree, [])
# Now to push the repo to the new location
_, ref = repo.resolve_refish(refish=repo.head.name)
remote = repo.remotes["origin"]
remote.push([ref.name], callbacks = my_func_to_get_RemoteCallbacks_obj_for_ssh_auth)
我使用以下內容作為參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.