GitPython gets a tree and blob by sha

I am using GitPython with an open repository, and I am trying to get a specific git object by its step. If I used git directly, I would just do it

git ls-tree sha_of_tree git show sha_of_blob 

Since I am using GitPython and I want to get a specific tree, I am doing the following.

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

And bring it back

 <git.Tree "b466a6098a0287ac568ef0ad783ae2c35d86362b"> 

Now I have a tree object, but I can’t access its attributes, such as path, name, drops, 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 you print the following, then it works

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

Another part of my question is how to get blob object with GitPython. I noticed that only the object tree has blobs attributes, so in order to get blob by sha, I must first find out which tree it belongs to, find this blob, and then call the data_stream method. I could just do

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

but first, I would like to know that this is the only way to do this.

+7
source share
3 answers

In general, a tree has children who have drops and more trees. Drops are files that are direct children of this tree, and other trees are directories that are children of this tree.

Access files directly under this tree:

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

Access directories directly under this tree:

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

How to look at drops in subdirectories:

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

Allows you to get the path to the first blob earlier:

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

Hopefully this will give you a better idea of ​​how to navigate this data structure and how to access the attributes of these objects.

+3
source

Try the following:

  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 
+3
source

I searched for this because I had the same problem and I found a solution:

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

I feel that there must be a way to link to Blob through the repo, but I could not find it.

+1
source

Source: https://habr.com/ru/post/916437/


All Articles