ローカル ファイルシステム内の git リポジトリへのパスと、リポジトリ内のpath_to_my_repository
ファイルへのパスがあるとしますpath_to_file
。
特定の日付リストについて、 Python から特定のブランチにある対応するバージョンのファイルを取得するにはどうすればよいですか(つまり、ファイルをメモリにロードします)。
このシェルコマンドは、あなたが望むことをするはずです:
git show "<branch>@{<timestamp>}:<path/to/file>"
例えば:
git show "master@{yesterday}:some/file/in/repo"
git show "master@{2014-01-01 00:00:00}:another/file"
これはに出力されSTDOUT
ます。
これを任意のディレクトリから実行する-C
には、リポジトリのルートを指すオプションを使用できます。
git -C path_to_my_repository show "master@{2014-05-05 12:00:00}:path_to_file"
このようなモジュール、またはそれに近いものを使用して、subprocess
Python からこれを実行できます。
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()
または、他の方法を使用してシェル コマンドを呼び出します。また、 libgit2やその他の多くのツールも使用できるはずです。