GitPython get tree and blob object by sha

前端 未结 3 960
礼貌的吻别
礼貌的吻别 2021-01-12 10:06

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 s         


        
3条回答
  •  时光取名叫无心
    2021-01-12 10:22

    In general, a tree has children which are blobs and more trees. The blobs are files that are direct children of that tree and the other trees are directories that are direction children of that tree.

    Accessing the files directly below that tree:

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

    Accessing the directories directly below that tree:

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

    How about looking at the blobs in the subdirectories:

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

    Let's get the path of the first blob from earlier:

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

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

提交回复
热议问题