尝试解析标签时dulwich的NotCommitError

时间:2013-09-24 15:28:50

标签: python git dulwich

我正在与dulwich合作开展一个项目,我需要通过提交ID有时克隆存储库,有时通过标记,有时通过分支名称来克隆存储库。我在使用标签的情况下遇到了麻烦,它似乎适用于某些存储库,但不适用于其他存储库。

这是我写的“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

两个引用都是标记,因此我不确定我做错了什么,或者为什么代码在两个存储库中的行为都不同。非常感谢任何帮助解决这个问题!

1 个答案:

答案 0 :(得分:2)

refs / tags / 0.6.0是带注释的标签。这意味着它的ref指向Tag对象(然后它具有对提交对象的引用),而不是直接指向Commit对象。

在这一行:

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]
相关问题