首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gitpython解析git日志

使用gitpython解析git日志
EN

Stack Overflow用户
提问于 2019-04-13 00:57:40
回答 2查看 7.5K关注 0票数 2

在python中,我希望获得git存储库中文件的所有提交的日志,并解析日志中的信息(散列、作者姓名、作者邮件、作者日期、提交者姓名、提交者邮件、提交日期和提交消息)。目前,我可以使用gitpython或者通过子进程调用shell命令来获取原始的git日志。

使用gitpython:

代码语言:javascript
复制
g=git.Git(path)
loginfo=g.log("--pretty=fuller",'--follow',"<filename>")

使用子过程调用:

代码语言:javascript
复制
lines = subprocess.check_output(
        ['git', 'log','--follow',"--pretty=fuller"," 
         <filename"],stderr=subprocess.STDOUT)

然而,在那之后,我想解析原始日志,但我无法在gitpython中找到合适的库/方法来实现这一点。此外,我还希望日期以python日期时间格式进行解析。你能帮上忙吗?

EN

回答 2

Stack Overflow用户

发布于 2019-04-13 05:30:10

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

代码语言:javascript
复制
import git
repo = git.Repo("/home/user/.emacs.d")
commits = list(repo.iter_commits("master", max_count=5))

然后你就可以确定gitpython提供了什么类型的数据:

代码语言:javascript
复制
dir(commits[0])

其中一些是:

  • author
  • committed_datetime
  • hexsha
  • message
  • stats

举个例子:

代码语言:javascript
复制
>>> 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将datetime对象与locale对象一起输出)

如果您想要检查提交是否包含一个文件(如果您想要从该文件获取所有提交,则该文件是可用的)。您可以使用:

代码语言:javascript
复制
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

然后,要从一个文件中获取所有提交:

代码语言:javascript
复制
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‘文件中的所有提交:

代码语言:javascript
复制
initel_file_commits = get_file_commits('init.el')

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

检查函数是否正常工作:

代码语言:javascript
复制
>>> 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}}

希望能有所帮助。

票数 6
EN

Stack Overflow用户

发布于 2019-04-26 17:28:59

您可以考虑使用PyDriller,这是一个围绕GitPython的包装器,可以使这些事情变得更容易。有一个名为get_commits_modified_file()的函数

代码语言:javascript
复制
gr = Git("repo_path")
commits = gr.get_commits_modified_file("filepath") # this return a list of commits hash

然后您可以执行以下操作:

代码语言:javascript
复制
for commit in Repository("repo_path", only_commits=commits).traverse_commits():
    # your code here
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55656434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档