GitPython不签出就可以从指定的提交中获取文件吗
我想用GitPython从指定的commit复制文件。现在我来到这里:GitPython不签出就可以从指定的提交中获取文件吗,python,gitpython,Python,Gitpython,我想用GitPython从指定的commit复制文件。现在我来到这里: 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() 它起作用了。但是checkout会更改HEAD并更改文件。是否可以在不签出的情况下从指定的提交中复制文件或读取文件?确实为您提供了一个流,但需要注意的是:如果
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()
它起作用了。但是checkout
会更改HEAD
并更改文件。是否可以在不签出的情况下从指定的提交中复制文件或读取文件?确实为您提供了一个流,但需要注意的是:如果您习惯使用和或.readlines()
读取流,请不要在此处尝试。选择普通的.read()
如果您不想要尾随的换行符,也可以直接委托给git show,如:
我建议您使用(它在内部使用GitPython)。它更易于使用:
for commit in RepositoryMining("path_to_repo", single="commitHASH").traverse_commits():
for modified_file in commit.modifications:
# do whatever you want with the source code
print(modified_file.source_code)
应该可以使用从任何提交中获取blob并从中读取,例如git.Repo().commit(commit_HEX_SHA).tree['subdir/somefile.ext'].data\u stream
。
git.Repo().git.show('{}:{}'.format(COMMIT_HEX_SHA, 'subdir/somefile.ext'))
for commit in RepositoryMining("path_to_repo", single="commitHASH").traverse_commits():
for modified_file in commit.modifications:
# do whatever you want with the source code
print(modified_file.source_code)