简体   繁体   English

Python git 包获取标签并提交

[英]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.

相关问题 如何使用 python 代码获取 python 项目的 git 提交 hash 项目? - How to get git commit hash of python project using python code? Python Git 和 Git-Lab 创建项目并推送代码/提交 - Python Git and Git-Lab create project and push code/commit git commit error:python 2和python 3.7之间的版本不一致 - git commit error: version inconsistency between python 2 and python 3.7 我是否需要将dist文件夹提交到git才能使用git中的pip安装包? - Do I need to commit the dist folder to git in order to be able to install the package using pip from git? `ModuleNotFoundError` 当从 Git 导入 Python Package - `ModuleNotFoundError` When Importing Python Package from Git 在Python中,通过指定脚本的git commit hash来运行脚本的版本 - In Python, run a version of a script by specifying its git commit hash python 脚本打印来自特定 git 分支的最后提交消息 - python script to print the last commit message from specific git branch 使用git + ssh进行Pip安装不适用于python软件包 - Pip install using git+ssh not working for python package 如何从私有 git 仓库在 python 中创建轮子 package - how to create wheel package in python from the private git repo Python:测试和加载同一包的不同 git 提交 - Python: testing & loading different git commits of same package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM