当试图解析标签时,dulwich并不害怕

2024-04-19 07:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在和dulwich合作一个项目,在这个项目中,我有时需要通过提交ID、标记、分支名称来克隆存储库。我对tag case有问题,它似乎适用于某些存储库,但不适用于其他存储库。你知道吗

下面是我编写的“clone”助手函数:

from dulwich import index
from dulwich.client import get_transport_and_path
from dulwich.repo import Repo


def clone(repo_url, ref, folder):
    is_commit = False
    if not ref.startswith('refs/'):
        is_commit = True
    rep = Repo.init(folder)
    client, relative_path = get_transport_and_path(repo_url)

    remote_refs = client.fetch(relative_path, rep)
    for k, v in remote_refs.iteritems():
        try:
            rep.refs.add_if_new(k, v)
        except:
            pass

    if ref.startswith('refs/tags'):
        ref = rep.ref(ref)
        is_commit = True

    if is_commit:
        rep['HEAD'] = rep.commit(ref)
    else:
        rep['HEAD'] = remote_refs[ref]
    indexfile = rep.index_path()
    tree = rep["HEAD"].tree
    index.build_index_from_tree(rep.path, indexfile, rep.object_store, tree)
    return rep, folder

奇怪的是,我能做到

 clone('git://github.com/dotcloud/docker-py', 'refs/tags/0.2.0', '/tmp/a')

但是

clone('git://github.com/dotcloud/docker-registry', 'refs/tags/0.6.0', '/tmp/b')

失败于

NotCommitError: object debd567e95df51f8ac91d0bb69ca35037d957ee6
type commit
[...]
 is not a commit

这两个引用都是标记,所以我不确定我做错了什么,也不知道为什么代码在两个存储库上的行为不同。如果你能帮我解决这个问题,我将不胜感激!你知道吗


Tags: pathfromimportclientreftreeindexif
1条回答
网友
1楼 · 发布于 2024-04-19 07:16:11

refs/tags/0.6.0是带注释的标记。这意味着它的ref指向一个标记对象(该对象随后有一个对提交对象的引用),而不是直接指向提交对象。你知道吗

在此行中:

if is_commit:
     rep['HEAD'] = rep.commit(ref)
 else:
     rep['HEAD'] = remote_refs[ref]

你可能只想做一些事情,比如:

if isinstance(rep[ref], Tag):
     rep['HEAD'] = rep[ref].object[1]
else:
     rep['HEAD'] = rep[ref]

相关问题 更多 >