Search code examples
pythongitgitpython

HOW-TO: git show hexsha:directorypath/file


Thank you for supporting this awesome module (GitPython) that I have been working with the last couple of weeks.

I tried to find in the documentation the equivalent in GitPython of how to make the call to "git show"

git show <hexsha>:<directory>/<file>

even with interacting with git directly

git_ = irepo.git

Could you please enlighten me on how to manage and make the above "git show" call given that hexsha,directory,file are known ?


Solution

  • As GitPython doesn't wrap the show subcommand, one will indeed have to use the git command wrapper directly.

    The call git show <hexsha>:<directory>/<file> would look like this in git-python.

    import git
    r = git.Repo(path_to_repo)
    res = r.git.show("%s:%s" % (hexsha, file_path))
    

    res will be a string containing output produced by git show, which you will have to parse yourself.

    More information on how to use git directly can be found in the official documentation.