pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

Show last commits for Tree or Blob

Open chuwy opened this issue 12 years ago • 8 comments

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?

chuwy avatar May 12 '13 23:05 chuwy

right now ,it's no see this #200

suziwen avatar May 13 '13 00:05 suziwen

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?

chuwy avatar May 13 '13 02:05 chuwy

can you provide some example code?

cholin avatar May 13 '13 08:05 cholin

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.

chuwy avatar May 13 '13 14:05 chuwy

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

jdavid avatar May 13 '13 14:05 jdavid

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...

SLiNv avatar Nov 15 '17 16:11 SLiNv

libgit2 issue https://github.com/libgit2/libgit2/issues/3041

jdavid avatar Oct 04 '20 08:10 jdavid

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

goDeni avatar Nov 14 '23 07:11 goDeni