pygit2
pygit2 copied to clipboard
Show last commits for Tree or Blob
Hello. I pretty sure that my problem is extremely simple or solved many times, but I can't find the right answer. Is there any possibility to get the last commit for particular tree or blob?
I tried to use something very similar, but this approach have a bug. It shows few extra commits, which are not appeared in git log and anyway, I think there's should be little bit clearer way. Is it?
right now ,it's no see this #200
Hm. It's strange, but it's seems that bug was not in my approach. I talking about extra commits. Results are always differs from what I get via git log. And it differs with different sort argument. I thought it affects only order?
can you provide some example code?
For example, just like in #200
def walk_for_files(repo, file_name, sort=0):
walker = repo.walk(repo.head.target.hex, sort) # change for dev-version
c0 = walker.next()
for c1 in walker:
files_in_diff = ((x.old_file_path, x.new_file_path) for x in c1.tree.diff(c0.tree))
if file_name in itertools.chain(*files_in_diff):
yield c0
c0 = c1
for c in walk_for_files(r, '.venv', 2):
print c.message
merge fix menu projects plugin
for c in walk_for_files(r, '.venv', 4):
print c.message
multiple actions show more button projects plugin add action model
# git log .venv
projects plugin init project
I tried sort options until "18", all of them gave me vaious results, but no one gave only "projects plugin" ("init project" won't printed by algorythm). libgit2 installed from master branch, pygit2 too.
Libgit2 diff methods support a pathspec option, but pygit2 does not support it yet. It would be quite easy to add support for it, maybe you want to try.
There is a related libgit2 issue, https://github.com/libgit2/libgit2/issues/495
Hi, I am just wondering if this is implemented? I do wish to inquiry a blob object's last commit in order to pull parent commit to show changes...
libgit2 issue https://github.com/libgit2/libgit2/issues/3041
Hello! My solution is
def _find_last_commit(repo: Repository, file_path: str, oid: str):
walker = repo.walk(oid, GIT_SORT_TIME)
first_commit = next(walker)
while True:
second_commit = next(walker)
first_file = first_commit.tree[file_path]
if file_path not in second_commit.tree:
return first_commit
second_file = second_commit.tree[file_path]
if first_file.id != second_file.id:
return first_commit
first_commit = second_commit
Diff calculation between two commits very expensive