Search code examples
pythongit-commitgit-loggitpython

Diff commit messages of two branches with gitpython


At work, we have a workflow where each branch is "named" by date. During the week, at least once, the latest branch gets pushed to production. What we require now is the summary/commit messages of the changes between the latest branch in production vs the new branch via gitpython.

What I have tried to do:

import git

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

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

This give me all of the commit history from both branches but I can't figure out how to compare them to just get the newest commit messages.

Is this possible to do in gitPython? Or is there a better solution?


Solution

  • I figured it out:

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

    Reading through the Git documentation I found that I can compare two branches with this syntax B1..B2. I tried the same with gitpython and it worked, the other parameters are there for a custom format.