reviewboard.diffviewer.diffutils¶
-
CHUNK_RANGE_RE
= <_sre.SRE_Pattern object>[source]¶ A regex for matching a diff chunk header.
New in version 3.0.18.
-
convert_to_unicode
(s, encoding_list)[source]¶ Returns the passed string as a unicode object.
If conversion to unicode fails, we try the user-specified encoding, which defaults to ISO 8859-15. This can be overridden by users inside the repository configuration, which gives users repository-level control over file encodings.
Ideally, we’d like to have per-file encodings, but this is hard. The best we can do now is a comma-separated list of things to try.
Returns the encoding type which was used and the decoded unicode object.
-
split_line_endings
(data)[source]¶ Splits a string into lines while preserving all non-CRLF characters.
Unlike the string’s splitlines(), this will only split on the following character sequences: n, r, rn, and rrn.
This is needed to prevent the sort of issues encountered with Unicode strings when calling splitlines(), which is that form feed characters would be split. patch and diff accept form feed characters as valid characters in diffs, and doesn’t treat them as newlines, but splitlines() will treat it as a newline anyway.
-
patch
(diff, orig_file, filename, request=None)[source]¶ Apply a diff to a file.
This delegates out to
patch
because noone except Larry Wall knows how to patch.Parameters: - diff (bytes) – The contents of the diff to apply.
- orig_file (bytes) – The contents of the original file.
- filename (unicode) – The name of the file being patched.
- request (django.http.HttpRequest, optional) – The HTTP request, for use in logging.
Returns: The contents of the patched file.
Return type: bytes
Raises: reviewboard.diffutils.errors.PatchError
– An error occurred when trying to apply the patch.
-
get_original_file
(filediff, request, encoding_list)[source]¶ Get a file either from the cache or the SCM, applying the parent diff if it exists.
SCM exceptions are passed back to the caller.
-
get_patched_file
(source_data, filediff, request=None)[source]¶ Return the patched version of a file.
This will normalize the patch, applying any changes needed for the repository, and then patch the provided data with the patch contents.
Parameters: - source_data (bytes) – The file contents to patch.
- filediff (reviewboard.diffviewer.models.filediff.FileDiff) – The FileDiff representing the patch.
- request (django.http.HttpClient, optional) – The HTTP request from the client.
Returns: The patched file contents.
Return type: bytes
-
get_filenames_match_patterns
(patterns, filenames)[source]¶ Return whether any of the filenames match any of the patterns.
This is used to compare a list of filenames to a list of
patterns
. The patterns are case-sensitive.Parameters: - patterns (list of unicode) – The list of patterns to match against.
- filename (list of unicode) – The list of filenames.
Returns: True
if any filenames match any patterns.False
if none match.Return type:
-
get_matched_interdiff_files
(tool, filediffs, interfilediffs)[source]¶ Generate pairs of matched files for display in interdiffs.
This compares a list of filediffs and a list of interfilediffs, attempting to best match up the files in both for display in the diff viewer.
This will prioritize matches that share a common source filename, destination filename, and new/deleted state. Failing that, matches that share a common source filename are paired off.
Any entries in
interfilediffs` that don't have any match in ``filediffs
are considered new changes in the interdiff, and any entries infilediffs
that don’t have entries ininterfilediffs
are considered reverted changes.Parameters: - tool (reviewboard.scmtools.core.SCMTool) – The tool used for all these diffs.
- filediffs (list of reviewboard.diffviewer.models.FileDiff) – The list of filediffs on the left-hand side of the diff range.
- interfilediffs (list of reviewboard.diffviewer.models.FileDiff) – The list of filediffs on the right-hand side of the diff range.
Yields: tuple – A paired off filediff match. This is a tuple containing two entries, each a
FileDiff
orNone
.
-
get_diff_files
(diffset, filediff=None, interdiffset=None, interfilediff=None, request=None, filename_patterns=None)[source]¶ Return a list of files that will be displayed in a diff.
This will go through the given diffset/interdiffset, or a given filediff within that diffset, and generate the list of files that will be displayed. This file list will contain a bunch of metadata on the files, such as the index, original/modified names, revisions, associated filediffs/diffsets, and so on.
This can be used along with
populate_diff_chunks()
to build a full list containing all diff chunks used for rendering a side-by-side diff.Parameters: - diffset (reviewboard.diffviewer.models.DiffSet) – The diffset containing the files to return.
- filediff (reviewboard.diffviewer.models.FileDiff, optional) – A specific file in the diff to return information for.
- interdiffset (reviewboard.diffviewer.models.DiffSet, optional) – A second diffset used for an interdiff range.
- interfilediff (reviewboard.diffviewer.models.FileDiff, optional) – A second specific file in
interdiffset
used to return information for. This should be provided iffilediff
andinterdiffset
are both provided. If it’sNone
in this case, then the diff will be shown as reverted for this file. - filename_patterns (list of unicode, optional) – A list of filenames or
patterns
used to limit the results. Each of these will be matched against the original and modified file of diffs and interdiffs.
Returns: A list of dictionaries containing information on the files to show in the diff, in the order in which they would be shown.
Return type: list of dict
-
populate_diff_chunks
(files, enable_syntax_highlighting=True, request=None)[source]¶ Populates a list of diff files with chunk data.
This accepts a list of files (generated by get_diff_files) and generates diff chunk data for each file in the list. The chunk data is stored in the file state.
-
get_file_from_filediff
(context, filediff, interfilediff)[source]¶ Return the files that corresponds to the filediff/interfilediff.
This is primarily intended for use with templates. It takes a RequestContext for looking up the user and for caching file lists, in order to improve performance and reduce lookup times for files that have already been fetched.
This function returns either exactly one file or
None
.
-
get_last_line_number_in_diff
(context, filediff, interfilediff)[source]¶ Determine the last virtual line number in the filediff/interfilediff.
This returns the virtual line number to be used in expandable diff fragments.
-
get_last_header_before_line
(context, filediff, interfilediff, target_line)[source]¶ Get the last header that occurs before the given line.
This returns a dictionary of
left
header andright
header. Each header is eitherNone
or a dictionary with the following fields:Field Description line
Virtual line number (union of the original and patched files) text
The header text
-
get_file_chunks_in_range
(context, filediff, interfilediff, first_line, num_lines)[source]¶ Generate the chunks within a range of lines in the specified filediff.
This is primarily intended for use with templates. It takes a RequestContext for looking up the user and for caching file lists, in order to improve performance and reduce lookup times for files that have already been fetched.
See
get_chunks_in_range()
for information on the returned state of the chunks.
-
get_chunks_in_range
(chunks, first_line, num_lines)[source]¶ Generate the chunks within a range of lines of a larger list of chunks.
This takes a list of chunks, computes a subset of those chunks from the line ranges provided, and generates a new set of those chunks.
Each returned chunk is a dictionary with the following fields:
Variable Description change
The change type (“equal”, “replace”, “insert”, “delete”) numlines
The number of lines in the chunk. lines
The list of lines in the chunk. meta
A dictionary containing metadata on the chunk Each line in the list of lines is an array with the following data:
Index Description 0 Virtual line number (union of the original and patched files) 1 Real line number in the original file 2 HTML markup of the original file 3 Changed regions of the original line (for “replace” chunks) 4 Real line number in the patched file 5 HTML markup of the patched file 6 Changed regions of the patched line (for “replace” chunks) 7 True if line consists of only whitespace changes
-
get_line_changed_regions
(oldline, newline)[source]¶ Returns regions of changes between two similar lines.
-
get_sorted_filediffs
(filediffs, key=None)[source]¶ Sorts a list of filediffs.
The list of filediffs will be sorted first by their base paths in ascending order.
Within a base path, they’ll be sorted by base name (minus the extension) in ascending order.
If two files have the same base path and base name, we’ll sort by the extension in descending order. This will make
*.h
sort ahead of*.c
/*.cpp
, for example.If the list being passed in is actually not a list of FileDiffs, it must provide a callable
key
parameter that will return a FileDiff for the given entry in the list. This will only be called once per item.
-
get_displayed_diff_line_ranges
(chunks, first_vlinenum, last_vlinenum)[source]¶ Return the displayed line ranges based on virtual line numbers.
This takes the virtual line numbers (the index in the side-by-side diff lines) and returns the human-readable line numbers, the chunks they’re in, and mapped virtual line numbers.
A virtual line range may start or end in a chunk not containing displayed line numbers (such as an “original” range starting/ending in an “insert” chunk). The resulting displayed line ranges will exclude these chunks.
Parameters: Returns: A tuple of displayed line range information, containing 2 items.
Each item will either be a dictionary of information, or
None
if there aren’t any displayed lines to show.The dictionary contains the following keys:
display_range
:A tuple containing the displayed line range.
virtual_range
:A tuple containing the virtual line range that
display_range
maps to.chunk_range
:A tuple containing the beginning/ending chunks that
display_range
maps to.
Return type: tuple
Raises: ValueError
– The range provided was invalid.
-
get_diff_data_chunks_info
(diff)[source]¶ Return information on each chunk in a diff.
This will scan through a unified diff file, looking for each chunk in the diff and returning information on their ranges and lines of context. This can be used to generate statistics on diffs and help map changed regions in diffs to lines of source files.
New in version 3.0.18.
Parameters: diff (bytes) – The diff data to scan. Returns: A list of chunk information dictionaries. Each entry has an orig
and ``modified` dictionary containing the following keys:chunk_start
(int
):- The starting line number of the chunk shown in the diff, including any lines of context. This is 0-based.
chunk_len
(int
):- The length of the chunk shown in the diff, including any lines of context.
changes_start
(int
):- The starting line number of a range of changes shown in a chunk in the diff. This is after any lines of context and is 0-based.
changes_len
(int
):- The length of the changes shown in a chunk in the diff, excluding any lines of context.
pre_lines_of_context
(int
):- The number of lines of context before any changes in a chunk. If the chunk doesn’t have any changes, this will contain all lines of context otherwise shown around changes in the other region in this entry.
post_lines_of_context
(int
):- The number of lines of context after any changes in a chunk. If the chunk doesn’t have any changes, this will be 0.
Return type: list of dict