python-3.x - Python git 包获取标签并提交

标签 python-3.x git gitpython

我想在我的 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/

相关文章:

ruby-on-rails - Git 在 Rails 项目上忽略了我的 .gitignore

python - 如何在 GitPython 中创建 Git 拉取请求

python - 当有东西要获取/拉取时与没有东西时从 gitPython 返回的 FetchInfo 对象有什么区别?

python - 如何在python中将字符串值转换为int值

python - 如何旋转 matplotlib 条形图?

python - 如何将二维列表写入txt文件?

git - 只推送一个文件到 GIT

python - 如何使用正则表达式删除制表符和换行符

Gitlab-ci.yml 创建 merge 请求

python - 列出自上次使用 GitPython 提交以来已更改的文件