Skip to content

Support for line offsets in patch hunks #84

Description

@stevenengler

In situations where you don't want to diff an entire file (for example you want to ignore some header), you could diff only a subset of the lines. But then the line numbers in the generated patch will be wrong since they will be relative to the start of that subset.

Would you be open to an API that supports setting line offsets for the original and modified text? Similar to DiffOptions::set_original_filename() and DiffOptions::set_modified_filename(), I think this could be added with a DiffOptions::set_original_line_offset() and DiffOptions::set_modified_line_offset(). These would add some integer offset to the generated Hunks.

An alternative API could be to add a Patch::hunks_mut() and the ability to update the old range and new range of each Hunk. Maybe a Hunk::new_range_add_offset(i64) and Hunk::old_range_add_offset(i64). This would also be useful for converting back after reading/parsing a patch.

Some workarounds are:

  1. Add blank lines to the original and modified text to cause diffy to generate the correct line numbers. But this isn't great when you want to skip a lot of lines.
  2. Manually parse the generated patch text and replace the line numbers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions