GitPython Is it possible to get a file from a specified commit without checking

I want to copy files from the specified commit using GitPython. Now I come here:

import git
git = git.Git(REPO_PATH)
git.checkout(COMMIT_HEX_SHA)
fo = open(REPO_PATH + "/foo.txt", "r")
str = fo.read(10);
fo.close()

It works. But checkoutmodifies HEADand modifies files. Is it possible to copy files or read files from a specified commit without checkout?

+5
source share
1 answer

Byron's comment does give you a stream, but a word of caution: if you are used to using withor .readlines()reading streams, do not try them here. Go easy .read().

git.Repo().commit(COMMIT_HEX_SHA).tree['subdir/somefile.ext'].data_stream.read()

, git show :

git.Repo().git.show('{}:{}'.format(COMMIT_HEX_SHA, 'subdir/somefile.ext'))
0

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


All Articles