fitzzftw.patch.container.DiffCodeFile

class DiffCodeFile[source]

Bases: object

Stateful 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:

hunks

Returns a list of all Hunk objects associated with this file (ro).

orig_header

The original file header (---) (ro).

new_header

The new file header (+++) (rw).

addedlines

Total number of added lines (+) across all hunks (ro).

deletedlines

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_path

Generates 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.

property addedlines: int

Total number of added lines (+) across all hunks (ro).

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:

list[FileLine]

property deletedlines: int

Total number of deleted lines (-) across all hunks (ro).

get_source_path(strip=0)[source]

Determine the source file path based on the header and strip level.

Parameters:

strip (int) – Number of path components to remove from the start.

Returns:

A Path object for the source file.

Return type:

Path

get_target_path(strip=0)[source]

Determine the source file path based on the header and strip level.

Parameters:

strip (int) – Number of path components to remove from the start.

Returns:

A Path object for the source file.

Return type:

Path

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.

property orig_header: HeadLine

The original file header (—) (ro).

Returns:

The HeadLine object representing the source file state.

update_line_counts(lines_added, lines_deleleted)[source]

Increment the file-wide line counters. Usually called by child Hunk objects.

Parameters:
  • lines_added (int) – Number of ‘+’ lines to add.

  • lines_deleted – Number of ‘-’ lines to add.