──────────────────────────────────────

63. Maintain a Python Tool · Read a Diff and Find Its Commit

Use a diff and the local Git history to explain what changed.

Lesson files · lessons/maintain-a-tool/read-the-change

Before you start
- Loop through lines of text.
- Use a string slice to read the first character.

By the end
- Recognise file headers and count changed lines inside a Git diff.
- Use a local commit and its diff to explain a fix to the next maintainer.

Where you can use it
Show a teammate the change that fixed a report and point to the exact commit they can inspect.

Git keeps snapshots of the work in a folder. A commit records a snapshot with a message. After a first commit, use git status --short to see changed paths and git diff -- room_report.py to inspect edits in that file before staging them with git add.

A diff first names the old and new paths. An @@ line marks the start of a block of changed code. Within that block, a line beginning with a minus was removed and a line beginning with a plus was added. Studio provides saved diff text so you can read the same format.

Use git log --oneline to find a commit’s short ID, then git show followed by that ID to see what the commit changed. The stock report practice folder in the workbook gives you the actual commands to initialise a repository, review a fix and share its commit.

Predict
Which behaviour does the added line introduce in this diff?
- The returned name has its surrounding whitespace removed.
- The function now groups all names into one label.

Worked example (example.py)

lines = ["-    return name", "+    return name.strip()"]
for line in lines:
    if line.startswith("+"):
        print(line[1:].strip())


Example result
return name.strip()

Read one file’s Git diff from Program input. Count lines that start with a plus or minus inside its change blocks. Start counting after the first @@ header. Print Added: and Removed: with their counts. In your notes, name the file and explain what the old and new lines do.

Starter program (starter.py)

import sys

for line in sys.stdin:
    if line.startswith("@@"):
        print("Found a change block.")


Starter input
diff --git a/room_report.py b/room_report.py
--- a/room_report.py
+++ b/room_report.py
@@ -1,2 +1,3 @@
 def label(name):
-    return name
+    clean_name = name.strip()
+    return clean_name

Hint
Set a flag when a line starts with @@. Count plus and minus lines only after that flag is set.

Case 1. Try the sample
Input
diff --git a/room_report.py b/room_report.py
--- a/room_report.py
+++ b/room_report.py
@@ -1,2 +1,3 @@
 def label(name):
-    return name
+    clean_name = name.strip()
+    return clean_name
Expected output
Added: 2
Removed: 1

Case 2. Add one blank code line
Input
diff --git a/test_room_report.py b/test_room_report.py
--- a/test_room_report.py
+++ b/test_room_report.py
@@ -1,2 +1,3 @@
 import unittest
+
 from room_report import room_totals
Expected output
Added: 1
Removed: 0

Case 3. No changes
Input
(No input.)
Expected output
Added: 0
Removed: 0

Stretch
Use the workbook README.txt to make the practice folder and save its baseline commit. Run git log --oneline and git show HEAD to inspect the baseline. Keep using this folder in the repair lesson.

Reflect
Which Git command would you give a partner to see an already committed fix, and what would you ask them to check in it?

Check your prediction
The returned name has its surrounding whitespace removed. The added line applies strip() to the room name.
