Categories
Python

Fetching changed files (diff) between two Git commits in Python

First we import the git module, create a Repo object with the physical path to the git repo. We call the iter_commits() method to get a iterator or generator expression which we pass to list() for converting it to a list item.

To compare one commit to another we use the diff() method of a commit object and pass another commit object as the parameter.

The returned list has all the changed files. Each of this item has two blobs – a_blob or b_blob. a_blob is the blob in the first commit, b_blob is the blob in the last commit – in the selected range.

Each of this blob objects have different properties – name, path, data_stream etc. We’re interested only in the path. We should use both a_blob and b_blob because in case of a renamed file, there are two changes – one is deleted, one is added. a_blob will point to the earlier file, b_blob will be the latest one.

Finally, we print the list of changed files. Easy, is it not? 🙂

Categories
Python

Fetching Remote Git Repo with Python: In a few lines of codes

In my old posts, I have always made rants about how cool and comfy Python is. Here’s a tiny code snippet that shows how I fetch remote Git repos with Python and the awesome GitPython library:

PS: Installing GitPython is just a breeze on *Nix with: