GitPython:获取当前标签(分离头)

gue*_*tli 8 python git gitpython

我使用库gitpython

如果本地git位于签出的标记上,我想获取标记的名称.

repo=git.Repo(repo_dir)
repo.tag # --> tags. But which is the current?
Run Code Online (Sandbox Code Playgroud)

在命令行上,git工具知道它.例

user@host> git status
HEAD detached at release/1.2.3
Run Code Online (Sandbox Code Playgroud)

我想通过gitpython获取字符串"release/1.2.3".

And*_*eyT 8

您可以遍历标记并将每个标记提交与当前头部提交进行比较:

next((tag for tag in repo.tags if tag.commit == repo.head.commit), None)
Run Code Online (Sandbox Code Playgroud)

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及为什么它会解决问题的解释。大多数只包含代码(即使它正在工作)的答案通常不会帮助 OP 理解他们的问题。 (2认同)

Wol*_*olf 5

看起来你可以通过GitCmd调用得到你想要的东西describe.

g = Git(git_dir)
rval = g.describe()
Run Code Online (Sandbox Code Playgroud)

我没有看到任何直接访问此信息的方法.

  • 或者使用[`repo.git.describe()`](https://github.com/gitpython-developers/GitPython/issues/136). (5认同)