【问题标题】:Diff commit messages of two branches with gitpython使用 gitpython 区分两个分支的提交消息
【发布时间】:2017-02-12 17:26:14
【问题描述】:

在工作中,我们有一个工作流程,其中每个分支都按日期“命名”。在一周中,至少有一次,最新的分支被推送到生产环境。我们现在需要的是生产中最新分支与通过 gitpython 的新分支之间更改的摘要/提交消息。

我试图做的事情:

import git

g = git.Git("pathToRepo")
r = git.Repo("pathToRepo")
g.pull() # get latest

b1commits = r.git.log("branch1")
b2commits = r.git.log("branch2")

这为我提供了两个分支的所有提交历史记录,但我不知道如何比较它们以获得最新的提交消息。

这可以在 gitPython 中实现吗?还是有更好的解决方案?

【问题讨论】:

    标签: python git-commit git-log gitpython


    【解决方案1】:

    我想通了:

    import git
    
    g = git.Git(repoPath+repoName)
    g.pull()
    commitMessages = g.log('%s..%s' % (oldBranch, newBranch), '--pretty=format:%ad %an - %s', '--abbrev-commit')
    

    通读 Git 文档,我发现我可以用这种语法 B1..B2 比较两个分支。我对 gitpython 进行了同样的尝试,它成功了,其他参数用于自定义格式。

    【讨论】:

      【解决方案2】:

      此解决方案使用GitPython

      import git
      
      def get_commit_from_range(start_commit, end_commit):
          repo = git.Repo('path')
          commit_range = f"{start_commit}...{end_commit}"
          result = repo.iter_commits(commit_range)
          for commit in result:
             print(commit.message)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-14
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        相关资源
        最近更新 更多