Merger

Class
implements MergerInterface
Final: Yes

Merges .gitattributes content with generated export-ignore rules.

Description

This class preserves existing custom entries while adding missing export-ignore rules for known candidate paths, deduplicates semantically equivalent entries, and sorts export-ignore rules with directories before files.

Table of Contents

Interfaces

MergerInterface

Merges export-ignore entries with existing .gitattributes content.

Methods

merge()

Merges generated export-ignore entries with existing .gitattributes content.

 : string
extractExportIgnorePathSpec()

Extracts the path spec from a simple export-ignore line.

 : string|null
generatedDirectoryLookup()

Builds a lookup table of generated directory candidates.

 : array<string, true>
isLiteralPathSpec()

Determines whether a path spec is a literal path and not a glob pattern.

 : bool
keepInExportLookup()

Builds a lookup table for paths that MUST stay in the exported archive.

 : array<string, true>
normalizeLine()

Normalizes a .gitattributes line for deterministic comparison and output.

 : string
normalizePathKey()

Normalizes a path spec for deduplication and keep-in-export matching.

 : string
normalizePathSpec()

Normalizes a gitattributes path spec for sorting.

 : string
parseExistingLines()

Parses the raw .gitattributes content into trimmed non-empty lines.

 : array<int, string>
sortKey()

Builds the natural sort key for a path spec.

 : string
Methods

merge()

Public

Merges generated export-ignore entries with existing .gitattributes content.

public merge( string  $existingContent, array<int, string>  $exportIgnoreEntries[, array<int, string>  $keepInExportPaths = []]) : string

Description

This method:

  1. Preserves custom user-defined entries in their original order
  2. Adds missing generated export-ignore entries for existing paths
  3. Deduplicates entries using normalized path comparison
  4. Sorts export-ignore entries with directories before files
Parameters
$existingContent : string

Description

The raw .gitattributes content currently stored.

$exportIgnoreEntries : array<int, string>

Description

the export-ignore entries to manage

$keepInExportPaths : array<int, string> = []

Description

the paths that MUST remain exported

Return values
string

Description

The merged .gitattributes content

extractExportIgnorePathSpec()

Private

Extracts the path spec from a simple export-ignore line.

private extractExportIgnorePathSpec( string  $line) : string|null
Parameters
$line : string

Description

the line to inspect

Return values
string|null

Description

the extracted path spec when the line is a simple export-ignore rule

generatedDirectoryLookup()

Private

Builds a lookup table of generated directory candidates.

private generatedDirectoryLookup( array<int, string>  $exportIgnoreEntries) : array<string, true>
Parameters
$exportIgnoreEntries : array<int, string>

Description

the generated export-ignore path list

Return values
array<string, true>

Description

the normalized directory lookup

isLiteralPathSpec()

Private

Determines whether a path spec is a literal path and not a glob pattern.

private isLiteralPathSpec( string  $pathSpec) : bool
Parameters
$pathSpec : string

Description

the normalized path spec to inspect

Return values
bool

Description

true when the path spec is a literal path

keepInExportLookup()

Private

Builds a lookup table for paths that MUST stay in the exported archive.

private keepInExportLookup( array<int, string>  $keepInExportPaths) : array<string, true>
Parameters
$keepInExportPaths : array<int, string>

Description

the configured keep-in-export paths

Return values
array<string, true>

Description

the normalized path lookup

normalizeLine()

Private

Normalizes a .gitattributes line for deterministic comparison and output.

private normalizeLine( string  $line) : string
Parameters
$line : string

Description

the raw line to normalize

Return values
string

Description

the normalized line

normalizePathKey()

Private

Normalizes a path spec for deduplication and keep-in-export matching.

private normalizePathKey( string  $pathSpec) : string

Description

Literal root paths are compared without leading slash differences, while pattern-based specs preserve their original anchoring semantics.

Parameters
$pathSpec : string

Description

the raw path spec to normalize

Return values
string

Description

the normalized deduplication key

normalizePathSpec()

Private

Normalizes a gitattributes path spec for sorting.

private normalizePathSpec( string  $pathSpec) : string
Parameters
$pathSpec : string

Description

the raw path spec to normalize

Return values
string

Description

the normalized path spec

parseExistingLines()

Private

Parses the raw .gitattributes content into trimmed non-empty lines.

private parseExistingLines( string  $content) : array<int, string>
Parameters
$content : string

Description

The full .gitattributes content.

Return values
array<int, string>

Description

the non-empty lines from the file

sortKey()

Private

Builds the natural sort key for a path spec.

private sortKey( string  $pathSpec) : string
Parameters
$pathSpec : string

Description

the raw path spec to normalize for sorting

Return values
string

Description

the natural sort key