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:

Categories
Bangla Python Series

āĻŦāĻžāĻ‚āϞāĻžā§Ÿ āĻĒāĻžāχāĻĨāύ – āĻĢāĻžāĻ‚āĻļāύ

āφāĻŽāĻžāĻĻ⧇āϰ āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽā§‡āϰ āϝ⧇ āĻ…āĻ‚āĻļāϗ⧁āϞ⧋ āĻŦāĻžāϰ āĻŦāĻžāϰ āφāϏ⧇ āϏ⧇āϗ⧁āϞ⧋āϕ⧇ āφāĻŽāϰāĻž āĻĒ⧁āύāϰāĻžā§Ÿ āĻŦā§āϝāĻŦāĻšāĻžāϰāϝ⧋āĻ—ā§āϝ āĻāĻ•āĻ• (reusable unit) āĻšāĻŋāϏ⧇āĻŦ⧇ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āϏāĻžāĻšāĻžāĻ¯ā§āϝ⧇ āĨ¤ āĻ—āύāĻŋāϤ⧇ āϝ⧇āĻŽāύ āĻĻ⧇āϖ⧇āĻ›āĻŋ āϕ⧋āύ āĻĢāĻžāĻ‚āĻļāύ āĻāĻ•āϟāĻŋ āχāύāĻĒ⧁āϟ āύāĻŋā§Ÿā§‡ āϏ⧇āϟāĻžāϰ āωāĻĒāϰ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āϧāϰāύ⧇āϰ āĻŽā§āϝāĻžāĻĨ āĻ•āϰ⧇ āφāωāϟāĻĒ⧁āϟ āĻĻā§‡ā§Ÿ, āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽāĻŋāĻ‚āĻāĻ“ āϏ⧇āχ āĻāĻ•āχ āĻŦā§āϝāĻžāĻĒāĻžāϰ āϘāĻŸā§‡ āĨ¤ āφāĻĒāύāĻŋ āĻāĻ• āĻŦāĻž āĻāĻ•āĻžāϧāĻŋāĻ• āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāϟāĻžāϰ āĻĒāĻžāϏ āĻ•āϰāĻŦ⧇āύ āĻāĻ•āϟāĻŋ āĻĢāĻžāĻ‚āĻļāύ⧇, āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ āĻĒā§āϰāϏ⧇āϏ āĻ•āϰ⧇ āφāĻĒāύāĻžāϕ⧇ āφāωāϟāĻĒ⧁āϟ “āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰāĻŦ⧇” āĨ¤ āϤāĻŦ⧇ āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽāĻŋāĻ‚ āĻāϰ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ āϏāĻŦāϏāĻŽā§Ÿ āϝ⧇ āχāύāĻĒ⧁āϟ āĻĨāĻžāĻ•āϤ⧇ āĻšāĻŦ⧇ āĻŦāĻž āφāωāϟāĻĒ⧁āϟ āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇ āĻāĻŽāύ āϕ⧋āύ āĻ•āĻĨāĻž āύ⧇āχ āĨ¤

āĻāĻ•āϟāĻŋ āĻĢāĻžāĻ‚āĻļāύ āφāϏāϞ⧇ āĻ•āĻŋāϛ⧁ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āĻŸā§‡āϰ āϏāĻ‚āĻ•āϞāύ āĨ¤ āϝāĻ–āύāχ āϕ⧋āύ āĻĢāĻžāĻ‚āĻļāύ āĻ•āϞ āĻ•āϰāĻž āĻšā§Ÿ āϤāĻ–āύ āĻāχ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āĻ­āĻŋāϤāϰ⧇ āĻĨāĻžāĻ•āĻž āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϗ⧁āϞ⧋ āĻāĻ•ā§āϏāĻŋāĻ•āĻŋāωāϟ āĻ•āϰāĻž āĻšā§Ÿ āĨ¤ āĻĒāĻžāχāĻĨāύ⧇ āφāĻŽāϰāĻž āĻĢāĻžāĻ‚āĻļāύ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ def āĻ•āĻŋ-āĻ“ā§ŸāĻžāĻ°ā§āĻĄāϟāĻŋ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŋ āĨ¤ āφāϏ⧁āύ āĻĻ⧇āϖ⧇ āύ⧇āχ āĻāĻ•āϟāĻŋ āĻĢāĻžāĻ‚āĻļāύ:

āĻĒā§āϰāĻĨāĻŽā§‡ āφāĻŽāϰāĻž def āĻ•āĻŋ-āĻ“ā§ŸāĻžāĻ°ā§āĻĄāϟāĻŋ āϞāĻŋāϖ⧇āĻ›āĻŋ āĨ¤ āϤāĻžāϰāĻĒāĻžāϰ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āύāĻžāĻŽ – “hello”, āĻāĻŦāĻ‚ āϤāĻžāϰāĻĒāϰ (). āϝāĻĻāĻŋ āφāĻŽāϰāĻž āĻĢāĻžāĻ‚āĻļāύāϟāĻŋāϤ⧇ āϕ⧋āύ āχāύāĻĒ⧁āϟ āĻĻāĻŋāϤ⧇ āϚāĻžāχ āϏ⧇āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāϟāĻžāϰāϗ⧁āϞ⧋ āĻāχ () āĻāϰ āĻŽāĻ§ā§āϝ⧇ āĻ•āĻŽāĻž āĻĻāĻŋā§Ÿā§‡ āφāϞāĻžāĻĻāĻž āĻ•āϰ⧇ āϞāĻŋāĻ–āϤ⧇ āĻšāĻŦ⧇ āĨ¤ āφāĻŽāϰāĻž āĻĻ⧇āĻ–āϤ⧇ āĻĒāĻžāĻšā§āĻ›āĻŋ, āĻāχ āĻĢāĻžāĻ‚āĻļāύ⧇ āφāĻŽāϰāĻž āϕ⧋āύ āχāύāĻĒ⧁āϟ āĻĻāĻŋāĻšā§āĻ›āĻŋ āύāĻž āĨ¤ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋ “Hello world!” āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāĻŦ⧇ āĨ¤ āϏāĻŋ-āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽāĻŋāĻ‚ āĻāϰ āϏāĻžāĻĨ⧇ āĻŽāĻŋāϞ āϰ⧇āϖ⧇ (āĻāĻŦāĻ‚ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āĻŸā§‡āϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻĻ⧇āĻ–āĻžāύ⧋āϰ āϜāĻ¨ā§āϝ) āφāĻŽāϰāĻž 0 āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰāĻ›āĻŋ āĨ¤ āφāϏāϞ⧇ āĻāχ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āĻŸā§‡āϰ āϕ⧋āύ āĻĻāϰāĻ•āĻžāϰ āĻ›āĻŋāϞ āύāĻž āĨ¤

āĻāĻŦāĻžāϰ āφāϏ⧁āύ āĻĻ⧇āĻ–āĻž āϝāĻžāĻ• āĻĒāĻžāχāĻĨāύ⧇ āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āφāĻŽāϰāĻž āĻĢāĻžāĻ‚āĻļāύ āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāϟāĻžāϰ āĻĒāĻžāϏ āĻ•āϰāĻŦ āĨ¤

āĻāχ āĻĢāĻžāĻ‚āĻļāύāϟāĻŋāϕ⧇ āĻ•āϞ āĻ•āϰ⧁āύ āĻāĻ­āĻžāĻŦ⧇: sayHello(“maSnun”)

āĻāĻŦāĻžāϰ http://learnpythonthehardway.org/book/ex19.html āĻāχ āĻĒ⧇āχāϜ āĻĨ⧇āϕ⧇ āĻĢāĻžāĻ‚āĻļāύ āύāĻŋā§Ÿā§‡ āĻ•āĻŋāϛ⧁ āϘāĻžāϟāĻžāϘāĻžāϟāĻŋ āĻ•āϰ⧁āύ āĨ¤ āχāύāĻļāĻž āφāĻ˛ā§āϞāĻžāĻš, āύ⧇āĻ•ā§āϏāϟ āĻĒā§‹āĻ¸ā§āĻŸā§‡ āĻĒāĻžāχāĻĨāύ⧇ āĻĢāĻžāĻ‚āĻļāύ⧇āϰ āφāϰ⧋ āĻ•āĻŋāϛ⧁ āĻĻāĻŋāĻ• āύāĻŋā§Ÿā§‡ āφāϞ⧋āϚāύāĻž āĻ•āϰāĻžāϰ āĻšā§‡āĻˇā§āϟāĻž āĻ•āϰāĻŦ āĨ¤