The history of pygit2 blob

I am trying to make the equivalent of git log filename in a git repository using pygit2. The documentation explains how to make git log as follows:

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

Do you have any ideas?

thanks

EDIT:

I now have something similar, more or less accurate:

 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 compare it 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) 
+7
source share
2 answers

I would recommend that you simply use the git command-line interface, which can provide well-formatted output that is very easy to parse with Python. For example, to get the author’s 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 complete list of format specifiers that you can pass to - pretty , check out the git log documentation: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

+1
source

Another solution, lazily giving file revisions from a given commit. Because it is recursive, it can break down if the story is too big.

 def revisions(commit, file, last=None): try: entry = commit.tree[file] except KeyError: return if entry != last: yield entry last = entry for parent in commit.parents: for rev in revisions(parent, file, last): yield rev 
0
source

All Articles