2012-11-08 47 views
7

मैं pygit2 का उपयोग कर गिट नंगे भंडार में git log filename के बराबर करने की कोशिश कर रहा हूं। प्रलेखन केवल बताते हैं कि कैसे एक git log इस तरह करना है:pygit2 ब्लॉब इतिहास

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

आप किसी भी विचार है?

धन्यवाद

संपादित करें:

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) 

उत्तर

1

मैं सिर्फ Git के कमांड लाइन इंटरफेस का उपयोग करने की सलाह देंगे:

मैं इस समय कुछ इस तरह, कम या ज्यादा सटीक हो रही है , जो अच्छी तरह से स्वरूपित आउटपुट प्रदान कर सकता है जो पाइथन का उपयोग करके पार्स करना वास्तव में आसान है। उदाहरण के लिए, लेखक का नाम प्राप्त करने के लिए, लॉग संदेश और प्रतिबद्ध किसी दिए गए फ़ाइल के लिए हैश:

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

प्रारूप विनिर्देशक की पूरी सूची के लिए है कि आप --pretty को पारित कर सकते हैं, प्रलेखन पर एक नजर है गिट लॉग: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

0

एक और समाधान, आलसी रूप से किसी दिए गए प्रतिबद्धता से फ़ाइल के संशोधन उत्पन्न करता है। चूंकि यह रिकर्सिव है, अगर इतिहास बहुत बड़ा हो तो यह टूट सकता है।

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