GitPython get tree and blob object by sha

前端 未结 3 977
礼貌的吻别
礼貌的吻别 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:18

    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
    

提交回复
热议问题