trac.versioncontrol.diff – Utilities for generation of diffs

Synopsis

get_filtered_hunks, get_hunks are low-level wrappers for Python’s difflib.SequenceMatcher, and they generate groups of opcodes corresponding to diff “hunks”.

get_change_extent is a low-level utility used when marking intra-lines differences.

diff_blocks is used at a higher-level to fill the template data needed by the “diff_div.html” template.

unified_diff is also a higher-level function returning differences following the unified diff file format.

Finally, get_diff_options is an utility for retrieving user diff preferences from a Request.

Function Reference

trac.versioncontrol.diff.get_change_extent(str1, str2)

Determines the extent of differences between two strings.

Returns a pair containing the offset at which the changes start, and the negative offset at which the changes end.

If the two strings have neither a common prefix nor a common suffix, (0, 0) is returned.

trac.versioncontrol.diff.get_filtered_hunks(fromlines, tolines, context=None, ignore_blank_lines=False, ignore_case=False, ignore_space_changes=False)

Retrieve differences in the form of difflib.SequenceMatcher opcodes, grouped according to the context and ignore_* parameters.

Parameters:
  • fromlines – list of lines corresponding to the old content
  • tolines – list of lines corresponding to the new content
  • ignore_blank_lines – differences about empty lines only are ignored
  • ignore_case – upper case / lower case only differences are ignored
  • ignore_space_changes – differences in amount of spaces are ignored
  • context – the number of “equal” lines kept for representing the context of the change
Returns:

generator of grouped difflib.SequenceMatcher opcodes

If none of the ignore_* parameters is True, there’s nothing to filter out the results will come straight from the SequenceMatcher.

trac.versioncontrol.diff.get_hunks(fromlines, tolines, context=None)

Generator yielding grouped opcodes describing differences .

See get_filtered_hunks for the parameter descriptions.

trac.versioncontrol.diff.diff_blocks(fromlines, tolines, context=None, tabwidth=8, ignore_blank_lines=0, ignore_case=0, ignore_space_changes=0)

Return an array that is adequate for adding to the data dictionary

See get_filtered_hunks for the parameter descriptions.

See also the diff_div.html template.

trac.versioncontrol.diff.unified_diff(fromlines, tolines, context=None, ignore_blank_lines=0, ignore_case=0, ignore_space_changes=0)

Generator producing lines corresponding to a textual diff.

See get_filtered_hunks for the parameter descriptions.

trac.versioncontrol.diff.get_diff_options(req)

Retrieve user preferences for diffs.

Returns:(style, options, data) triple.
style
can be 'inline' or 'sidebyside',
options
a sequence of “diff” flags,
data
the style and options information represented as key/value pairs in dictionaries, for example:
{'style': u'sidebyside',
 'options': {'contextall': 1, 'contextlines': 2,
             'ignorecase': 0,  'ignoreblanklines': 0,
             'ignorewhitespace': 1}}
trac.versioncontrol.diff.filter_ignorable_lines(hunks, fromlines, tolines, context, ignore_blank_lines, ignore_case, ignore_space_changes)

Detect line changes that should be ignored and emits them as tagged as “equal”, possibly joined with the preceding and/or following “equal” block.

See get_filtered_hunks for the parameter descriptions.