gpt4 book ai didi

python - GitPython 标签排序

转载 作者:太空狗 更新时间:2023-10-29 19:35:54 24 4
gpt4 key购买 nike

我正在尝试使用 GitPython 库获取存储库中的最新标签。通常我是这样做的:

repo = Repo(project_root)
last_tag = str(repo.tags[-1])

但是一旦 1.10 版发布,我总是得到 1.9 ;( 我知道这与输出 git tag -l 列出相同的顺序有关。所以它将是 1.1, 1.10, 1.2, ..., 1.9

问题是如何使用GitPython获取最新的标签? (我知道 git tag -l | sort -V 并且我知道如何不使用 repo 对象来解决这个问题。但也许有人知道我在这个 lib 中获取排序标签列表时缺少什么)

自定义排序功能也是一个选项,但我仍然想知道是否有办法使用 GitPython 来实现?

最佳答案

GitPython 中repo.tags 返回的IterableList 对象扩展了list Python 类,这意味着你可以按照你的方式对其进行排序想。要获取最新创建的标签,您只需执行以下操作:

import git
repo = git.Repo('path/to/repo')
tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)
latest_tag = tags[-1]

关于python - GitPython 标签排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25056478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com