Get All Revisions for a specific file in gitpython
Asked Answered
G

2

8

I am using gitpython library for performing git operations, retrieve git info from python code. I want to retrieve all revisions for a specific file. But couldn't find a specific reference for this on the docs.

Can anybody give some clue on which function will help in this regard? Thanks.

Ghazi answered 2/3, 2015 at 6:9 Comment(1)
I don't think so, as the referred-to question doesn't involve gitpython at all.Oleneolenka
F
9

A follow-on, to read each file:

import git
repo = git.Repo()
path = "file_you_are_looking_for"

revlist = (
    (commit, (commit.tree / path).data_stream.read())
    for commit in repo.iter_commits(paths=path)
)

for commit, filecontents in revlist:
    ...
Foreland answered 27/10, 2015 at 17:28 Comment(0)
O
6

There is no such function, but it is easily implemented:

import git
repo = git.Repo()
path = "dir/file_you_are_looking_for"

commits_touching_path = list(repo.iter_commits(paths=path))

Performance will be moderate even if multiple paths are involved. Benchmarks and more code about that can be found in an issue on github.

Oleneolenka answered 2/3, 2015 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.