Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygit2 blob history

I'm trying to do the equivalent of git log filename in a git bare repository using pygit2. The documentation only explains how to do a git log like this:

from pygit2 import GIT_SORT_TIME
for commit in repo.walk(oid, GIT_SORT_TIME):
    print(commit.hex)

Do you have any idea?

Thanks

EDIT:

I'm having something like this at the moment, more or less precise:

from pygit2 import GIT_SORT_TIME, Repository


repo = Repository('/path/to/repo')

def iter_commits(name):
    last_commit = None
    last_oid = None

    # loops through all the commits
    for commit in repo.walk(repo.head.oid, GIT_SORT_TIME):

        # checks if the file exists
        if name in commit.tree:
            # has it changed since last commit?
            # let's compare it's sha with the previous found sha
            oid = commit.tree[name].oid
            has_changed = (oid != last_oid and last_oid)

            if has_changed:
                yield last_commit

            last_oid = oid
        else:
            last_oid = None

        last_commit = commit

    if last_oid:
        yield last_commit


for commit in iter_commits("AUTHORS"):
    print(commit.message, commit.author.name, commit.commit_time)
like image 265
user1415785 Avatar asked Nov 08 '12 16:11

user1415785


1 Answers

I would recommend you to just use git's command-line interface, which can provide nicely formatted output that is really easy to parse using Python. For example, to get the author name, log message and commit hashes for a given file:

import subprocess
subprocess.check_output(['git','log','--pretty="%H,%cn%n----%B----"','some_git_file.py'])

For a full list of format specifiers that you can pass to --pretty, have a look at the documentation of git log: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

like image 144
ThePhysicist Avatar answered Oct 26 '22 14:10

ThePhysicist