fitzzftw.patch.container.DiffCodeFile¶
- class DiffCodeFile[source]¶
Bases:
objectStateful container for a single file’s modifications within a patch.
This class serves as the central assembly point for a file-level patch. It ensures that only valid HeadLine objects are used for headers. It manages a collection of Hunks, provides indexed and iterative access, and aggregates line-level statistics (added/deleted) from its child hunks.
- Attributes:
hunks (list[Hunk]): The collection of change blocks for this file.
Public Data Attributes:
Returns a list of all Hunk objects associated with this file (ro).
The original file header (---) (ro).
The new file header (+++) (rw).
Total number of added lines (+) across all hunks (ro).
Total number of deleted lines (-) across all hunks (ro).
Public Methods:
__init__(orig_header)Initializes a new DiffCodeFile with a mandatory original file header.
__getitem__(index)Enables indexed access to the stored hunks.
__len__()Provides the total count of hunks.
__iter__()Returns an independent iterator over the hunks.
add_hunk(hunk)Appends a Hunk to the internal collection and establishes the parent-child link for statistics.
apply(options)Apply hunks to the file content and return the resulting lines.
get_source_path([strip])Determine the source file path based on the header and strip level.
get_target_path([strip])Determine the source file path based on the header and strip level.
update_line_counts(lines_added, lines_deleleted)Increment the file-wide line counters.
__repr__()Return repr(self).
Private Data Attributes:
_temp_pathGenerates a unique path for the staging file (ro).
Private Methods:
_read_file(path)Read a file and convert its lines into FileLine objects.
_write_to_staging(lines)Write the patched lines to a temporary file in the staging area.
- __init__(orig_header)[source]¶
Initializes a new DiffCodeFile with a mandatory original file header.
- Parameters:
orig_header (HeadLine) – The ‘—’ header line object.
- Raises:
TypeError – If orig_header is not a HeadLine instance.
PatchParseError – If the header is not an original (—) header.
- add_hunk(hunk)[source]¶
Appends a Hunk to the internal collection and establishes the parent-child link for statistics.
- Parameters:
hunk (Hunk) – The Hunk object to add.
- apply(options)[source]¶
Apply hunks to the file content and return the resulting lines.
This method is purely logical and does not perform any write operations.
- Parameters:
options (DiffCodeOptions) – Command line arguments for comparison.
- Returns:
A new list of FileLine objects representing the patched state.
- Raises:
FtwPatchError – If any hunk fails to apply.
- Return type:
- get_source_path(strip=0)[source]¶
Determine the source file path based on the header and strip level.
- get_target_path(strip=0)[source]¶
Determine the source file path based on the header and strip level.
- property hunks: list[Hunk]¶
Returns a list of all Hunk objects associated with this file (ro).
- Returns:
A list containing specialized Hunk containers.
- property new_header: HeadLine | None¶
The new file header (+++) (rw).
- Parameters:
value – The HeadLine object to set as the target file state.
- Raises:
TypeError – If the value is not a HeadLine instance (Setter).
- Returns:
The HeadLine object representing the target file state or None.