1

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?

2 Answers 2

3

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.

2

This solution uses 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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.