English 中文(简体)
GitPython 获得 sha 的树和浮点对象
原标题:GitPython get tree and blob object by sha

我使用GitPython 和一个空的仓库, 我试图通过 SHA 获得特定的 Git 对象。 如果我直接使用 Git Git, 我会这样做

git ls-tree sha_of_tree
git show sha_of_blob

既然我用吉特皮森和我想买一棵树 我就做以下工作:

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

把这个拿回来

<git.Tree "b466a6098a0287ac568ef0ad783ae2c35d86362b">

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

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

File "<stdin>", line 1, in <module>
File "c:Python27libsite-packagesgitdbutil.py", line 238, in __getattr__
self._set_cache_(attr)
File "c:Python27libsite-packagesgitobjects	ree.py", line 147, in _set_cache_
super(Tree, self)._set_cache_(attr)
File "c:Python27libsite-packagesgitobjectsase.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

The other part of my question is how to get a blob object with GitPython. I noticed that the only object tree has attribute blobs, so in order to get blob by SHA, I have to (a) first know which tree it belongs to, (b) find this blob, and then (c) call the data_stream method. I could just do

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

但我想首先知道,这是这样做的唯一办法。

问题回答

尝试此 :

   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

一般而言,一棵树有孩子,这些孩子是小树和更多的树,这些小树是树上直接的孩子,其他树是指导那棵树的孩子的目录。

直接访问树下的文件 :

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

直接访问此树下方的目录 :

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

看看子目录中的浮肿如何:

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

让我们从前面获得第一个浮点的路径 :

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

希望这能让您更好地了解 如何浏览这个数据结构 以及如何访问这些天体的属性。

我之所以寻找这个是因为我也有同样的问题, 我找到了一个解决办法:

>>> 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">

我觉得应该有办法通过回购来引用Blob, 但我找不到它。





相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

热门标签