Syllabus Lesson 111 of 141 · Invent your tools
Invent your tools

A diff risk summariser

Why it matters Reviewers skim. A one-line summary at the top of a pull request ("touches a migration, weakens tests") tells them where to slow down, and it is the same every time, unlike asking a chat to "review this diff".

Your team writes down its review rules and wants a tool that applies them to a unified diff (the output of git diff). Each file in the diff is a section that starts with a diff --git a/OLD b/NEW line. Header lines follow (index, ---, +++, new file mode, deleted file mode, rename from/to, similarity index, Binary files ... differ). After the first @@ line of a section, every line is content: + added, - removed, a space for context.

Real diffs are messier than the textbook one. A pure rename has no ---/+++ lines and no content. A binary file has neither. A deleted file's +++ line says /dev/null. And a removed SQL comment, -- old note, appears in the diff as --- old note.

Your turn

Write risk_summary(diff). A file's path is NEW from its diff --git line, or OLD if the section has a deleted file mode line. Paths have no spaces. Return a dict from category to the sorted list of paths in it (leave out empty categories):

  • schema: a directory in the path is named migrations, or the file name ends in .sql.
  • secrets: an added line contains password, secret, api_key or token, in any case.
  • tests-weakened: a test file (name starts with test_, or a directory is named tests) that is deleted, or has more removed than added lines containing assert.
  • dependencies: the file name is requirements.txt, pyproject.toml, package.json or package-lock.json.
  • config: the file name ends in .yml, .yaml, .ini or .env, or is settings.py.
Trap to beat: the tests use renames, binary files, deleted and new files, and SQL comments, as described above.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.