2012-11-08 1 views
7

나는 pygit2를 사용하여 자식 벌거지 저장소에서 git log filename과 동일한 작업을 수행하려고합니다.pygit2 blob history

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

당신이 어떤 생각을 가지고 있습니까 : 문서는 어떻게 이런 git log을 할 설명?

감사

편집 :

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

난 그냥 자식의 명령 줄 인터페이스를 사용하도록 권장합니다 :

나는 더 많거나 적은 정확한 순간에 이런 식으로 뭔가가 있어요 이 함수는 파이썬을 사용하여 구문 분석하기 쉽도록 멋지게 형식화 된 출력을 제공 할 수 있습니다. 예를 들어, 저자 이름을 가져 메시지를 기록하고 커밋 특정 파일의 해시합니다 : 당신이 --pretty에 전달할 수있는

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

형식 지정자의 전체 목록을 문서를 보라 git log : 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 
관련 문제