我使用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")
但我想首先知道,这是这样做的唯一办法。