[英]Python git package get tag and commit
I would like to print a git commit
and the tag in my Python code.我想在我的 Python 代码中打印一个git commit
和标签。 How can I do this using git
package?我如何使用git
包来做到这一点?
When I am going to my Bitbucket I see当我去我的 Bitbucket 我看到
tag: 73-2-g46b9856
commit checksum: 46b9856
How can I retrieve this info from git
package?如何从git
包中检索此信息?
I have done the following:我做了以下工作:
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
So I assume you already have the checksum you want in the sha
variable.所以我假设你已经在sha
变量中有你想要的校验和。
At this point, there's a post for how to get the tags and looking for a specific tag associated with that sha
in this link: Get tags of a commit在这一点上,有一篇关于如何获取标签并在此链接中查找与该sha
关联的特定标签的帖子: 获取提交的标签
# 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)
Here is what solved my issue:这是解决我的问题的方法:
repo = git.Repo(search_parent_directories = True)
sha = repo.head.object.hexsha
commit_chksum = repo.git.rev_parse(sha, short = 7)
tag = subprocess.check_output(["git", "describe", "--always"]).strip().decode()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.