Python 检索文件的版本
假设我在本地文件系统中有一个git存储库的路径:Python 检索文件的版本,python,git,gitpython,Python,Git,Gitpython,假设我在本地文件系统中有一个git存储库的路径:path\u to\u my\u repository,以及存储库中一个文件的路径path\u to\u file 对于给定的日期列表,如何从Python中获取特定分支上文件的相应版本(即,将文件加载到内存中)。此shell命令应满足您的要求: git show "<branch>@{<timestamp>}:<path/to/file>" 这将打印到STDOUT 要从任意目录运行此操作,可以使用-C选项指向存
path\u to\u my\u repository
,以及存储库中一个文件的路径path\u to\u file
对于给定的日期列表,如何从Python中获取特定分支上文件的相应版本(即,将文件加载到内存中)。此shell命令应满足您的要求:
git show "<branch>@{<timestamp>}:<path/to/file>"
这将打印到STDOUT
要从任意目录运行此操作,可以使用-C
选项指向存储库根目录:
git -C path_to_my_repository show "master@{2014-05-05 12:00:00}:path_to_file"
您可以使用以下命令从Python运行此命令:
或者使用任何其他方法调用shell命令。你也应该能够,或者其他一些工具。谢谢,我无法复制这个,即使是在shell中:
git-C/Users/josh/code/reps/my_repo show“master@{2014-05-01 12:00:00}:/Users/josh/code/reps/my_repo/learning/main/config.xml”致命:路径/Users/josh/code/reps/my_repo/learning/main/config.xml存在于磁盘上,但不是在“master”{2014-05-01 12:00:00}
中。但是,我肯定知道该文件在该日期存在。@user815423426,路径应该是存储库中的相对路径。例如,在您的例子中,我相信您希望使用learning/main/config.xml
。
git -C path_to_my_repository show "master@{2014-05-05 12:00:00}:path_to_file"
from subprocess import Popen, PIPE
p = Popen(['git', '-C', path_to_my_repository, 'show',
'master@{' + date_string + '}:' + path_to_file],
stdin=PIPE, stdout=PIPE, stderr=PIPE)
# Content will be in `output`
output, err = p.communicate()