我想在我的 Python 代码中打印 git commit
和标签。
如何使用 git
包执行此操作?
当我去我的 Bitbucket 时,我看到
tag: 73-2-g46b9856
commit checksum: 46b9856
如何从 git
包中检索此信息?
我已完成以下操作:
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
最佳答案
所以我假设您已经在 sha
变量中获得了所需的校验和。
此时,有一篇文章介绍了如何获取标签并在此链接中查找与该 sha
关联的特定标签:Get tags of a commit
# Example code for clarity
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
tagmap = {}
for t in repo.tags:
tagmap.setdefault(repo.commit(t), []).append(t)
tags = tagmap[repo.commit(sha)] # Warning: Your latest commit might not have a tag associated with it so this will throw an error right now.
print(tags)
关于python-3.x - Python git 包获取标签并提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60249768/