Search code examples
pythongitgitpython

Retrieving versions of a file


Say I have a path to a git repository in the local filesystem: path_to_my_repository, and a path to a file in the repository path_to_file.

For a given list of dates, how can I get the corresponding version of the file on a particular branch from Python (i.e. loading the file in memory).


Solution

  • This shell command should do what you want:

    git show "<branch>@{<timestamp>}:<path/to/file>"
    

    For example:

    git show "master@{yesterday}:some/file/in/repo"
    git show "master@{2014-01-01 00:00:00}:another/file"
    

    This prints to STDOUT.

    To run this from an arbitrary directory, you can use the -C option to point to your repository root:

    git -C path_to_my_repository show "master@{2014-05-05 12:00:00}:path_to_file"
    

    You can run this from Python using the subprocess module like this, or something close to it:

    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()
    

    or using any other method to call a shell command. You should also be able to use libgit2, or a number of other tools.