7

git log filenamepygit2を使用して、gitベアリポジトリで同等のことをしようとしています。git logドキュメントでは、次のような方法のみを説明しています。

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)
4

2 に答える 2

1

git のコマンドライン インターフェイスを使用することをお勧めします。これは、Python を使用して解析するのが非常に簡単な適切にフォーマットされた出力を提供できます。たとえば、特定のファイルの作成者名、ログ メッセージ、およびコミット ハッシュを取得するには、次のようにします。

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

--prettyに渡すことができるフォーマット指定子の完全なリストについては、 git logのドキュメントを参照してください: https://www.kernel.org/pub/software/scm/git/docs/git-log. html

于 2013-08-06T08:56:47.197 に答える