问题描述
我在一个裸仓库中使用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。树b466a6098a0287ac568ef0ad783ae2c35d86362b>
现在我有了一个树对象,但是我无法访问其路径,名称,Blob等属性
repo.tree(b466a6098a0287ac568ef0ad783ae2c35d86362b)。path
Traceback(最近一次调用最后一次):
在< module>中的文件< stdin>,第1行
文件c:\Python27\lib\site-packages\gitdb\util.py,行238,在__getattr__
self._set_cache_(attr)
文件 c:\Python27\lib\site-packages\git\objects\tree.py,第147行,在_set_cache_
super(Tree,self)._ set_cache_(attr)
文件c:\Python27\lib\site-packages\git\objects\base.py,行157,在_set_cache_
raise AttributeError(path and mode attributes must have been set during during %s对象创建%type(self).__ name__)
AttributeError:必须在创建Tree对象期间设置路径和模式属性
但是,如果输入以下内容,它可以工作
repo.tree()。trees [0 ] .path
我的问题的另一部分是如何使用GitPython获取blob对象。我注意到只有对象树具有属性blob,所以为了通过sha获得blob,我必须首先知道它属于哪个树,找到这个blob然后调用data_stream方法。
我可以做
$ b $ $ p $ repo.git.execute(git show blob_sha)
code>
但我首先想知道这是执行此操作的唯一方法。
解决方案
试试这个:
def read_file_from_branch(self,repo,branch,path,charset ='ascii'):
'''
返回分支中文件的内容,而不检出
分支
'''
如果在repo.heads中分支:
blob =(repo.heads [branch] .commit.tree / path)
如果blob:
数据= blob.data_stream.read()
如果字符集:
返回data.decode(字符集)
返回数据
返回无
I'm using GitPython with a bare repository and I'm trying to get specific git object by its sha. If I used git directly, I would just do this
git ls-tree sha_of_tree
git show sha_of_blob
Since I'm using GitPython and I want to get specific tree, I do the following.
repo = Repo("path_to_my_repo")
repo.tree("b466a6098a0287ac568ef0ad783ae2c35d86362b")
And get this back
<git.Tree "b466a6098a0287ac568ef0ad783ae2c35d86362b">
Now I have a tree object but I cannot access its atributes like path,name,blobs, etc
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
But if type the following it works
repo.tree().trees[0].path
The other part of my question is how to get blob object with GitPython. I noticed that only object tree has attribute blobs so in order to get blob by sha, I have to first know which tree it belongs to, find this blob and then call data_stream method.I could just do
repo.git.execute("git show blob_sha")
but I would like to know first that this is the only way to do this.
解决方案
Try this:
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
这篇关于GitPython通过sha获取树和blob对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!