How to compute diff stats (insertions, deletions) in Python

Parses a git diff text and counts the number of added and removed lines to produce insertion and deletion stats.

Easy Python 3.9+ Aug 9, 2026 Git + Python 15 views 0 copies

Python code

39 lines
Python 3.9+
import re
from collections import Counter


def parse_diff(diff_text):
    insertions = 0
    deletions = 0
    for line in diff_text.splitlines():
        if line.startswith("+") and not line.startswith("+++"):
            insertions += 1
        elif line.startswith("-") and not line.startswith("---"):
            deletions += 1
    return insertions, deletions


def diff_stats(diff_text):
    ins, dels = parse_diff(diff_text)
    return {
        "insertions": ins,
        "deletions": dels,
        "total_changes": ins + dels,
    }


if __name__ == "__main__":
    sample_diff = """diff --git a/example.txt b/example.txt
--- a/example.txt
+++ b/example.txt
@@ -1,4 +1,4 @@
 Hello world
-old line removed
+new line added
+another insertion
 unchanged line
"""
    stats = diff_stats(sample_diff)
    print(f"Insertions: {stats['insertions']}")
    print(f"Deletions: {stats['deletions']}")
    print(f"Total changes: {stats['total_changes']}")

Output

stdout
Insertions: 2
Deletions: 1
Total changes: 3

How it works

This function reads a diff string line by line. Lines starting with + are counted as insertions, - as deletions. The not line.startswith("+++") guard skips the diff header lines like +++ b/example.txt. The --- guard similarly skips the original file header. Splitting by splitlines() handles both \n and \r\n line endings, making it robust across platforms. Counting raw lines gives a quick approximation of the git diff --stat output, though it doesn't account for moved or context lines.

Common mistakes

  • Not skipping the `+++` and `---` header lines, which corrupts the counts
  • Forgetting that blank lines in the diff start with a space and should not be counted
  • Ignoring binary file diffs where `Binary files differ` appears instead of `+`/`-` lines

Variations

  1. Use `subprocess.run` with `git diff --stat` or `git diff --numstat` to let Git compute stats
  2. Parse `git diff --numstat` output which gives tab-separated counts per file

Real-world use cases

  • Build a CI bot that comments merge-request size (insertions vs deletions) on every pull request.
  • Write a pre-commit hook that blocks changes touching more than N lines in critical files.
  • Summarize weekly developer activity by aggregating diff stats across a repo's commit history.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Git + Python

Related tutorials and quizzes for this topic.