2012-11-08 5 views
7

Ich versuche, das Äquivalent von git log filename in einem git bare-Repository mit pygit2 zu tun. Die Dokumentation nur erklärt, wie ein git log wie dies zu tun:pygit2 Blob Geschichte

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

Haben Sie eine Idee?

Dank

EDIT:

ich so etwas wie dies im Moment habe, mehr oder weniger präzise:

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) 

Antwort

1

Ich würde Ihnen empfehlen, nur Befehlszeile verwenden git der Schnittstelle , die eine schön formatierte Ausgabe bereitstellen kann, die mit Python sehr einfach zu analysieren ist. Um zum Beispiel den Namen des Autors zu erhalten, melden Nachricht und begehen Hashes für eine bestimmte Datei:

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

Für eine vollständige Liste der Formatbezeich, die Sie --pretty passieren kann, haben einen Blick auf die Dokumentation von git log: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

0

Eine andere Lösung, liefert träge Überarbeitungen einer Datei von einem bestimmten Commit. Da es rekursiv ist, kann es brechen, wenn der Verlauf zu groß ist.

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