2012-11-08 41 views
7

Pygit2 kullanarak bir git çıplak deposunda git log filename eşdeğerini yapmaya çalışıyorum.pygit2 blob history

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

Eğer bir fikrin var mı: dokümantasyon sadece nasıl böyle bir git log yapmak açıklıyor?

Teşekkür

DÜZENLEME:

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) 

cevap

1

Sadece seyahatseverlerin Git komut satırı arayüzü kullanmak için tavsiye ederim:

Ben, az ya da çok kesin bir anda böyle bir şey yaşıyorum Python kullanarak ayrıştırılması gerçekten kolay olan güzel biçimlendirilmiş çıktı sağlayabilir. Örneğin, yazar adı olsun mesajı log ve taahhüt verilen bir dosya için karmaları için: Eğer --pretty geçebilir

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

biçim belirteçleri tam listesi için, dokümantasyon de bakabilirsiniz git günlüğü: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

0

Başka bir çözüm, tembel bir şekilde, belirli bir işlemden bir dosyanın düzeltmelerini verir. Özyineli olduğu için tarih çok büyükse kırılabilir.

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