fitzzftw.patch.container.Hunk

class Hunk[source]

Bases: object

Container for a single change block within a file.

This class stores the coordinate information from the hunk header and the actual content lines. It ensures that only valid HunkHeadLine objects are used as headers. It tracks added and deleted lines and reports them to a parent DiffCodeFile if associated.

Public Data Attributes:

parent

The parent DiffCodeFile (via weakref proxy) (rw).

lines

Returns a list of all HunkLine objects within this hunk (ro).

old_start

The starting line number in the original file (ro).

new_start

The starting line number in the new file (ro).

addedlines

Total number of added lines (+) in this hunk (ro).

deletedlines

Total number of deleted lines (-) in this hunk (ro).

Public Methods:

__init__(header[, parent])

Initializes a new Hunk with coordinate metadata.

add_line(line)

Adds a single content line to the hunk and updates statistics.

apply(lines, options)

Apply the hunk's changes to a list of FileLine objects.

__getitem__(index)

Provides access to hunk lines by their position.

__len__()

Returns the count of lines in this hunk.

__iter__()

Provides an iterator for the hunk's lines.

__repr__()

Return repr(self).

Private Methods:

_compare_context(expected, actual, options)

Compare hunk context against file content using specialized properties.


__init__(header, parent=None)[source]

Initializes a new Hunk with coordinate metadata.

Parameters:
  • header (HunkHeadLine) – The ‘@@’ coordinate header line object.

  • parent (DiffCodeFile | None) – Optional parent DiffCodeFile for statistics aggregation.

Raises:

TypeError – If header is not a HunkHeadLine instance.

add_line(line)[source]

Adds a single content line to the hunk and updates statistics.

Parameters:

line (HunkLine) – The HunkLine object to append.

property addedlines: int

Total number of added lines (+) in this hunk (ro).

Returns:

Total number of added lines.

apply(lines, options)[source]

Apply the hunk’s changes to a list of FileLine objects.

This method validates the context of the target lines against the expected hunk context. If the validation passes (considering whitespace options), it performs the replacement/insertion and returns the new state of the lines.

Parameters:
  • lines (list[FileLine]) – Current file content as a list of FileLine objects.

  • options (HunkCompareOptions) – Command line arguments for whitespace and comparison.

Raises:

FtwPatchError – If the context check fails or the index is out of bounds.

Returns:

A modified list of FileLine objects.

Return type:

list[FileLine]

property deletedlines: int

Total number of deleted lines (-) in this hunk (ro).

Returns:

Total number of deleted lines.

property lines: list[HunkLine]

Returns a list of all HunkLine objects within this hunk (ro).

Returns:

A list containing specialized HunkLine objects.

property new_start: int

The starting line number in the new file (ro).

Returns:

The 1-based start line index for the target state.

property old_start: int

The starting line number in the original file (ro).

Returns:

The 1-based start line index from the header.

property parent: DiffCodeFile | None

The parent DiffCodeFile (via weakref proxy) (rw).

This can only be set once to maintain statistical integrity. If the hunk already contains lines, they are reported to the new parent.

Note: Accessing this property may raise a ReferenceError if the referenced object has been garbage collected.

Parameters:

value ('DiffCodeFile') – The DiffCodeFile instance to link.