2012-11-08 14 views
7

Sto cercando di fare l'equivalente di git log filename in un repository git bare utilizzando pygit2. La documentazione spiega solo come fare un git log come questo:pygit2 cronologia blob

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

Avete qualche idea?

Grazie

EDIT:

Sto avendo qualcosa di simile al momento, più o meno preciso:

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) 

risposta

1

vorrei si consiglia di utilizzare solo l'interfaccia della riga di comando di git , che può fornire un output ben formattato che è davvero facile da analizzare usando Python. Ad esempio, per ottenere il nome dell'autore, registrare il messaggio e si impegnano hash per un dato file:

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

Per un elenco completo di identificatori di formato che è possibile passare a --pretty, dare un'occhiata alla documentazione di log git: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

0

Un'altra soluzione, pigramente produce revisioni di un file da un dato commit. Dal momento che è ricorsivo, potrebbe rompersi se la cronologia è troppo grande.

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