Skip to main content

Patience Diff Algorithm - An implementation of Bram Cohen's diff algorithm

Project description

Patience Diff

A complete Python port of Bram Cohen's algorithm for computing efficient differences.

Features

  • Patience Sorting Algorithm: O(n log n)
  • Semantic Cleanup: Removes spurious matches
  • Multiple Output Formats: Blocks, hunks with context
  • Myers Fallback: Simple diff for edge cases
  • Type Hints: Full annotations for IDE support
  • Well Tested: Comprehensive test suite

Quick Start

make install  # Install in dev mode
make test     # Run tests

License

GNU General Public License v2 or later

Original algorithm and OCaml implementation by Bram Cohen
Copyright (C) 2005, 2006 Canonical Ltd
Python port by Shaun Joe

API Reference

Core Functions

  • get_matching_blocks(transform, prev_array, next_array, big_enough=1) - Find matching blocks
  • get_hunks(transform, prev_array, next_array, context=-1, big_enough=1) - Generate hunks
  • matches(prev_array, next_array) - Get matching index pairs
  • match_ratio(prev_array, next_array) - Calculate similarity ratio

Data Types

  • Hunk - Contiguous diff region with ranges
  • MatchingBlock - Matching subsequence (prev_start, next_start, length)
  • Range types: Same, Prev, Next, Replace

Project Structure

src/                      # Python package (11 modules)
├── patience_diff.py      # Core algorithm (700+ lines)
├── plain_diff.py         # Myers fallback
├── hunk.py               # Hunk data structures
├── hunks.py              # Hunk utilities
├── matching_block.py     # Matching block type
├── range.py              # Range types
├── move_id.py            # Move identifiers
├── move_kind.py          # Move classification
├── patience_diff_intf.py # Interface definitions
├── patience_diff_lib.py  # Library exports
└── __init__.py           # Package initialization

test/                     # Test suite
├── test_limit_context.py
├── test_merge_diff.py
└── test_plain_diff_cutoff.py

Makefile                  # Build automation
setup.py                  # Package setup
pyproject.toml            # Build configuration
README.md

Testing

All tests passing:

$ make test
test/test_limit_context.py PASSED
test/test_merge_diff.py PASSED
test/test_plain_diff_cutoff.py PASSED

============================  3 passed  ============================

Algorithm

The Patience Diff algorithm works in several steps:

  1. Identify unique elements (appearing exactly once in both sequences)
  2. Build longest increasing subsequence (LIS) of unique matches using patience sorting
  3. Recursively find matches in gaps between LIS elements
  4. Remove spurious small matches ("semantic cleanup")
  5. Merge adjacent blocks separated by identical elements
  6. Convert matching blocks to hunks with Same/Prev/Next/Replace ranges

References

  • http://bazaar-vcs.org - Original implementation
  • Eugene W. Myers: "An O(ND) Difference Algorithm and Its Variations"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patience_diff_algo-1.0.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patience_diff_algo-1.0.0-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file patience_diff_algo-1.0.0.tar.gz.

File metadata

  • Download URL: patience_diff_algo-1.0.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for patience_diff_algo-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a800036c043df6852a65c30c307226f7a118e685edd85b761302b0abf1aae166
MD5 569e7c16bf6434b0e753a0a54b20893e
BLAKE2b-256 15a465fd07fdb4f1388b626beba8a9a98bb5986e589349bddc1fbaa2a52acbd1

See more details on using hashes here.

File details

Details for the file patience_diff_algo-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for patience_diff_algo-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35058442a73fd3704789fde8b457555e2884df35557604094528611389a5426b
MD5 e33704ba6f89c614f0b113279da645c3
BLAKE2b-256 de07eaa4410bbc3bf84336beec2992fc8583c400dc45b847a3188cdac79fcd7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page