`git describe` 与 gitlab python API
Posted
技术标签:
【中文标题】`git describe` 与 gitlab python API【英文标题】:`git describe` with gitlab python API 【发布时间】:2021-08-30 06:28:15 【问题描述】:是否可以通过gitlab python API 获得与git describe --tags
等效的内容?
我想在 gitlab 运行器上浅克隆我的 repo 以获得性能,但仍将最后一个标签作为 conan builds 的版本信息。
【问题讨论】:
这里有同样的问题。试图弄清楚这一点。但还没有成功。我不想仅通过指定提交 ID 来计算没有结帐的在线版本。如果我遍历 commit.parent_ids,它会随着时间的推移变得太长。我唯一需要的是计算到正则表达式中下一个标签的距离。 【参考方案1】:这里有同样的问题。我需要计算从提交 ID 到某个标签(项目/版本)的距离。那里有多个项目,因此可以有多种不同的方式。
现在有了解决方案:
例子:
import gitlab
import pprint
from argparse import Namespace
import datetime
# helper to find all tags - limit to your needs
def find_tags(tags):
result = []
for tag in tags.list(all=True, as_list=False):
result.append(tag)
return result
_fast_cache = "page": 0
# gets a commit in an fast manner
def get_commit(project, commit_id, **kwargs):
global _fast_cache
if kwargs.get("direct", True):
commit = project.commits.get(commit_id)
_fast_cache.update(commit.id: Namespace(title=commit.title, id=commit.id, parent_ids=commit.parent_ids,
dateref=datetime.datetime.fromisoformat(commit.committed_date)))
return _fast_cache[commit_id]
while commit_id not in _fast_cache:
count = len(_fast_cache.keys())
current_page = _fast_cache["page"] + 1
for commit in project.commits.list(page=current_page, ref_name=kwargs.get("branch", commit_id),
per_page=100,
**kwargs):
if commit.id not in _fast_cache:
_fast_cache.update(
commit.id: Namespace(title=commit.title, id=commit.id, parent_ids=commit.parent_ids,
dateref=datetime.datetime.fromisoformat(
commit.committed_date)))
_fast_cache["page"] = current_page
if len(_fast_cache.keys()) == count:
return None
return _fast_cache[commit_id]
def walk_project(project, HEAD, tag, **kwargs):
def as_commit(c, direct=False):
if isinstance(c, str):
_commit = get_commit(project, c, direct=direct, **kwargs)
if _commit is None:
return None
return _commit
return c
seen_map =
depth = 0
HEAD = as_commit(HEAD)
lookup = 0: HEAD.id
TAG = as_commit(tag.target, direct=True)
if TAG.dateref > HEAD.dateref:
return None
while len(lookup.keys()) > 0:
key_item = sorted(lookup.keys()).pop(-1)
current = lookup[key_item]
del lookup[key_item]
if current not in seen_map:
data = as_commit(current)
if data is None:
# print("Nothing found?")
return None
seen_map[data.id] = dict(depth=depth, parents=data.parent_ids)
for x in data.parent_ids:
d = as_commit(x)
lookup[d.dateref] = d.id
if data.id in tag.target:
return tag.name + "-" + str(depth) + "-g" + HEAD.id[:10]
# abort in case that too many commits must be traversed
if depth >= 1000:
return None
# debug output
print(f"[depth][data.id] data.title")
depth += 1
return None
def main():
gl = gitlab.Gitlab("https://gitlabserver.com", private_token="mytoken")
project = gl.projects.get("pretty-tagged-project")
print(project.name)
current_head = project.commits.get("a commit to start ")
print(current_head.attributes)
print(current_head.parent_ids)
tags_names = find_tags(project.tags)
pprint.pprint(tags_names)
for tag_name in tags_names:
version_number = walk_project(project, current_head.id, tag_name, branch=current_head.id)
print(tag_name.name, version_number)
if __name__ == '__main__':
main()
【讨论】:
是的。但是缺少计算和匹配标签的部分......我没有得到那个。它也可能有助于问题的开始。 我尝试了start here,但还懒得继续。最好是来自 gitlab 甚至 git 的官方解决方案。以上是关于`git describe` 与 gitlab python API的主要内容,如果未能解决你的问题,请参考以下文章