Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Yields logic

Author Dave Jarvis <email>
Date 2026-02-18 12:35:17 GMT-0800
Commit a4c7a60d944d52bc930d6ea1849a4f9011f907f7
Parent 2ba9fea
git/GitDiff.php
}
- public function diff( string $oldSha, string $newSha ): array {
+ public function diff( string $oldSha, string $newSha ): Generator {
$oldTree = $oldSha !== '' ? $this->getTreeHash( $oldSha ) : '';
$newTree = $newSha !== '' ? $this->getTreeHash( $newSha ) : '';
- return $this->diffTrees( $oldTree, $newTree );
+ yield from $this->diffTrees( $oldTree, $newTree );
}
- public function compare( string $commitHash ): array {
+ public function compare( string $commitHash ): Generator {
$commitData = $this->git->read( $commitHash );
$parentHash = '';
- $result = [];
if( preg_match( '/^parent ([0-9a-f]{40})/m', $commitData, $m ) ) {
$parentHash = $m[1];
}
$newTree = $this->getTreeHash( $commitHash );
$oldTree = $parentHash !== '' ? $this->getTreeHash( $parentHash ) : '';
- return $this->diffTrees( $oldTree, $newTree );
+ yield from $this->diffTrees( $oldTree, $newTree );
}
string $newTreeSha,
string $path = ''
- ): array {
- $changes = [];
-
+ ): Generator {
if( $oldTreeSha !== $newTreeSha ) {
$oldEntries = $oldTreeSha !== ''
if( !$old && $new ) {
- $sub = $new['is_dir']
- ? $this->diffTrees( '', $new['sha'], $currentPath )
- : [$this->createChange( 'A', $currentPath, '', $new['sha'] )];
- $changes = array_merge( $changes, $sub );
+ if( $new['is_dir'] ) {
+ yield from $this->diffTrees( '', $new['sha'], $currentPath );
+ } else {
+ yield $this->createChange( 'A', $currentPath, '', $new['sha'] );
+ }
} elseif( !$new && $old ) {
- $sub = $old['is_dir']
- ? $this->diffTrees( $old['sha'], '', $currentPath )
- : [$this->createChange( 'D', $currentPath, $old['sha'], '' )];
- $changes = array_merge( $changes, $sub );
+ if( $old['is_dir'] ) {
+ yield from $this->diffTrees( $old['sha'], '', $currentPath );
+ } else {
+ yield $this->createChange( 'D', $currentPath, $old['sha'], '' );
+ }
} elseif( $old && $new && $old['sha'] !== $new['sha'] ) {
if( $old['is_dir'] && $new['is_dir'] ) {
- $changes = array_merge(
- $changes,
- $this->diffTrees( $old['sha'], $new['sha'], $currentPath )
+ yield from $this->diffTrees(
+ $old['sha'],
+ $new['sha'],
+ $currentPath
);
} elseif( !$old['is_dir'] && !$new['is_dir'] ) {
- $changes[] = $this->createChange(
+ yield $this->createChange(
'M',
$currentPath,
$old['sha'],
$new['sha']
);
}
}
}
}
-
- return $changes;
}
$currO = $start + 1;
$currN = $start + 1;
-
- // Note: Implicit context is handled by the gap logic in formatDiffOutput
- // We only reconstruct the sliced portion + operations here
foreach( $ops as $op ) {
private function formatDiffOutput( array $stream ): array {
- // This logic needs to handle the fact that we only passed the slice
- // But since the viewer likely doesn't show the full unchanged file
- // for huge diffs, returning just the changed chunk is acceptable
- // or we'd need to re-pass the original counts to pad the line numbers.
- // For now, this returns the calculated patch.
return $stream;
}
Delta 20 lines added, 30 lines removed, 10-line decrease