1

使用pygit2包如何在新存储库上创建初始提交?

我不断收到一条错误消息,提示找不到“refs/heads/master”。

4

1 回答 1

0

在某些情况下,我正在开发一个 Python 模块,该模块是从我用来管理 GitLab 实例的当前意大利面条代码创建的,以便为大学课程发布和评分作业。工作可以在这里找到。我的应用程序要求我下载一个存储库作为“模板”作业发布给所有学生,所以我需要为我课程中的所有学生使用相同的文件树和一个新的 git 存储库(因此需要一遍又一遍地创建初始提交再次)。

这里的技巧,我在任何地方都找不到文档(截至这篇文章似乎缺少 pygit2 的文档),是将第一个参数设置为create_committo"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)

我使用以下内容作为参考:

于 2022-01-25T02:06:52.027 回答