使用GitPython解析Git日志

3
在Python中,我想获取git存储库中文件的所有提交日志,并解析日志中的信息(哈希、作者名称、作者邮件、作者日期、提交者名称、提交者邮件、提交日期和提交消息)。目前,我可以使用gitpython或通过subprocess调用shell命令来获取原始的git日志。
使用gitpython:
g=git.Git(path)
loginfo=g.log("--pretty=fuller",'--follow',"<filename>")

使用子进程调用:

lines = subprocess.check_output(
        ['git', 'log','--follow',"--pretty=fuller"," 
         <filename"],stderr=subprocess.STDOUT)

然而,之后我想解析原始日志,但是在gitpython中找不到合适的库或方法。同时,我也希望日期能够以Python datetime格式解析。您能帮忙吗?

2个回答

7

您可以使用以下命令获取所有存储库提交记录:

import git
repo = git.Repo("/home/user/.emacs.d")
commits = list(repo.iter_commits("master", max_count=5))

然后你可以自行确定gitpython提供了哪些类型的数据:

dir(commits[0])

其中一些包括:

  • 作者
  • 提交时间
  • 十六进制SHA值
  • 消息
  • 状态

举个例子:

>>> commits[0].author
<git.Actor "azzamsa <foo@bar.com>">

>>> commits[0].hexsha
'fe4326e94eca2e651bf0081bee02172fedaf0b90'

>>> commits[0].message
'Add ocaml mode\n'

>>> commits[0].committed_datetime
datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=<git.objects.util.tzoffset object at 0x7fb4fcd01790>)

(committed_datetime输出带有locale对象的datetime对象)

如果您想检查提交是否包含文件(如果您想获取该文件的所有提交则可用),可以使用以下代码:

def is_exists(filename, sha):
    """Check if a file in current commit exist."""
    files = repo.git.show("--pretty=", "--name-only", sha)
    if filename in files:
        return True

然后获取一个文件中的所有提交:

def get_file_commits(filename):
    file_commits = []
    for commit in commits:
        if is_exists(filename, commit.hexsha):
            file_commits.append(commit)

    return file_commits

举个例子,我想从“init.el”文件中获取所有提交记录:

initel_file_commits = get_file_commits('init.el')

>>> initel_file_commits
[<git.Commit "fe4326e94eca2e651bf0081bee02172fedaf0b90">, <git.Commit
"e4f39891fb484a95ea76e8e07244b908e732e7b3">]

请确认该函数已正确运行:

>>> initel_file_commits[0].stats.files
{'init.el': {'insertions': 1, 'deletions': 0, 'lines': 1}, 'modules/aza-ocaml.el': {'insertions': 28, 'deletions': 0, 'lines': 28}}

>>> initel_file_commits[1].stats.files
{'init.el': {'insertions': 1, 'deletions': 0, 'lines': 1}, 'modules/aza-calfw.el': {'insertions': 65, 'deletions': 0, 'lines': 65}, 'modules/aza-home.el': {'insertions': 0, 'deletions': 57, 'lines': 57}}

希望可以帮到你。

3

您可以考虑使用PyDriller,这是一个对GitPython进行包装的工具,使这些操作更加简便。其中有一个函数名为get_commits_modified_file()

gr = Git("repo_path")
commits = gr.get_commits_modified_file("filepath") # this return a list of commits hash

然后您可以执行:

for commit in Repository("repo_path", only_commits=commits).traverse_commits():
    # your code here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接