【问题标题】:GitPython get tree and blob object by shaGitPython通过sha获取树和blob对象
【发布时间】:2018-10-17 22:56:53
【问题描述】:

我正在使用带有裸存储库的 GitPython,并且我正在尝试通过其 SHA 获取特定的 git 对象。如果我直接使用git,我会这样做

git ls-tree sha_of_tree
git show sha_of_blob

由于我使用的是 GitPython,并且我想获得一棵特定的树,因此我执行以下操作:

repo = Repo("path_to_my_repo")
repo.tree("b466a6098a0287ac568ef0ad783ae2c35d86362b")

然后把它拿回来

<git.Tree "b466a6098a0287ac568ef0ad783ae2c35d86362b">

现在我有一个树对象,但我无法访问它的属性,如路径、名称、blob 等。

repo.tree("b466a6098a0287ac568ef0ad783ae2c35d86362b").path
Traceback (most recent call last):

File "<stdin>", line 1, in <module>
File "c:\Python27\lib\site-packages\gitdb\util.py", line 238, in __getattr__
self._set_cache_(attr)
File "c:\Python27\lib\site-packages\git\objects\tree.py", line 147, in _set_cache_
super(Tree, self)._set_cache_(attr)
File "c:\Python27\lib\site-packages\git\objects\base.py", line 157, in _set_cache_
raise AttributeError( "path and mode attributes must have been set during %s object creation" % type(self).__name__ )
AttributeError: path and mode attributes must have been set during Tree object creation

但如果我输入以下内容,它会起作用

repo.tree().trees[0].path

我的问题的另一部分是如何使用 GitPython 获取 blob 对象。我注意到唯一的对象树有属性 blob,所以为了通过 SHA 获取 blob,我必须 (a) 首先知道它属于哪个树,(b) 找到这个 blob,然后 (c) 调用 @987654327 @ 方法。 我可以这样做

repo.git.execute("git show blob_sha")

但我想首先知道这是执行此操作的唯一方法。

【问题讨论】:

    标签: git gitpython


    【解决方案1】:

    试试这个:

       def read_file_from_branch(self, repo, branch, path, charset='ascii'):
                '''
                return the contents of a file in a branch, without checking out the
                branch
                '''
                if branch in repo.heads:
                    blob = (repo.heads[branch].commit.tree / path)
                    if blob:
                        data = blob.data_stream.read()
                        if charset:
                            return data.decode(charset)
                        return data
                return None
    

    【讨论】:

    • 请对您的代码添加更多解释。仅代码的答案不是很有帮助。谢谢。
    【解决方案2】:

    一般来说,一棵树的子节点是 blob 和更多的树。 Blob 是作为该树的直接子级的文件,而其他树是作为该树的直接子级的目录。

    直接访问该树下的文件:

    repo.tree().blobs # returns a list of blobs
    

    访问该树正下方的目录:

    repo.tree().trees # returns a list of trees
    

    查看子目录中的 blob 怎么样:

    for t in repo.tree().trees:
        print t.blobs
    

    让我们从之前获取第一个 blob 的路径:

    repo.tree().blobs[0].path # gives the relative path
    repo.tree().blobs[0].abspath # gives the absolute path
    

    希望这能让您更好地了解如何导航此数据结构以及如何访问这些对象的属性。

    【讨论】:

      【解决方案3】:

      我一直在寻找这个,因为我遇到了同样的问题,我找到了解决方案:

      >>> import binascii
      >>> id_to_find = repo.head.commit.tree['README'].hexsha  # For example
      >>> id_to_find
      "aee35f14ee5515ee98d546a170be60690576db4b"
      >>> git.objects.blob.Blob(repo, binascii.a2b_hex(id_to_find))
      <git.Blob "aee35f14ee5515ee98d546a170be60690576db4b">
      

      我觉得应该有一种方法可以通过 repo 引用 Blob,但我找不到它。

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 1970-01-01
        • 2015-11-04
        • 2019-03-03
        • 1970-01-01
        • 2021-08-28
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多