0

ローカル ファイルシステム内の git リポジトリへのパスと、リポジトリ内のpath_to_my_repositoryファイルへのパスがあるとしますpath_to_file

特定の日付リストについて、 Python から特定のブランチにある対応するバージョンのファイルを取得するにはどうすればよいですか(つまり、ファイルをメモリにロードします)。

4

1 に答える 1

2

このシェルコマンドは、あなたが望むことをするはずです:

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やその他の多くのツールも使用できるはずです。

于 2014-07-17T21:17:38.003 に答える