Search code examples
pythongitdiffpull-requestgitpython

Using gitpython to diff changes from a pull request


I'm trying to write a script that will be triggered by a pull request github webhook. When this script is triggered, it needs to find out which files were modified from master to newBranch. End of day, I'll read through those files, verify that no breaking changes were made, and then write up why it should/shouldn't be merged and send it off to a human to confirm.

I'm using gitpython (or, rather, trying to), but the documentation doesn't really go over the PR use case.

My code looks like this so far, but I'm not convinced that I'm doing things correctly:

repo = git.Repo('.')
repo.config_reader()
for d in repo.index.diff(repo.remotes.origin.refs.master.commit):
  print d
  print d.diff

I guess I'm not sure what the index is in a PR, so I'm not convinced that this is diffing the right things. When I try it out in a test repo, I don't see any diffs unless I'm manually fixing merge conflicts, so I suspect I'm not doing things correctly.

Can you please either help me with the code, or suggest a workflow that I could use to test this script? I'm getting all muddled up the more I look at this.


Solution

  • This is not the right approach.

    PRs are not a git concept; they are a Github one. You can't use gitpython for this. You will have to use Github's own API, for which there are several third-party libraries.