Dave Jarvis' Repositories

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

Reformats code, minor optimizations

AuthorDave Jarvis <email>
Date2026-02-23 12:33:09 GMT-0800
Commita5c4ec33e2daea2d8fe5f4b6577e0882421d8d58
Parent2cca672
Delta420 lines added, 538 lines removed, 118-line decrease
git/GitPacks.php
int $offset
) use ( &$result ): void {
- $context = $this->createContext(
- $packFile, $offset, 0
- );
- $meta = $this->reader->getEntryMeta(
- $context
+ $result = $this->reader->getEntryMeta(
+ $this->createContext( $packFile, $offset, 0 )
);
-
- $result = $meta;
$result['file'] = $packFile;
$result['offset'] = $offset;
}
);
return $result;
}
- public function peek(
- string $sha,
- int $len = 12
- ): string {
+ public function peek( string $sha, int $len = 12 ): string {
$result = '';
$this->locate(
$sha,
function(
string $packFile,
int $offset
) use ( &$result, $len ): void {
- $context = $this->createContext(
- $packFile, $offset, 0
- );
- $result = $this->reader->read(
- $context,
+ $result = $this->reader->read(
+ $this->createContext( $packFile, $offset, 0 ),
$len,
- function(
- string $baseSha,
- int $cap
- ): string {
+ function( string $baseSha, int $cap ): string {
return $this->peek( $baseSha, $cap );
}
$size = $this->reader->getSize( $context );
- if( $size <= self::MAX_RAM ) {
- $result = $this->reader->read(
- $context,
- 0,
- function(
- string $baseSha,
- int $cap
- ): string {
- return $cap > 0
- ? $this->peek( $baseSha, $cap )
- : $this->read( $baseSha );
- }
- );
- }
+ $result = $size <= self::MAX_RAM
+ ? $this->reader->read(
+ $context,
+ 0,
+ function( string $baseSha, int $cap ): string {
+ return $cap > 0
+ ? $this->peek( $baseSha, $cap )
+ : $this->read( $baseSha );
+ }
+ )
+ : $result;
}
);
$result = false;
- foreach(
- $this->streamGenerator( $sha ) as $chunk
- ) {
+ foreach( $this->streamGenerator( $sha ) as $chunk ) {
$callback( $chunk );
$result = true;
}
return $result;
}
- public function streamGenerator(
- string $sha
- ): Generator {
+ public function streamGenerator( string $sha ): Generator {
yield from $this->streamShaGenerator( $sha, 0 );
}
- public function streamRawCompressed(
- string $sha
- ): Generator {
+ public function streamRawCompressed( string $sha ): Generator {
$found = false;
$file = '';
}
);
-
- if( $found ) {
- $context = $this->createContext(
- $file, $off, 0
- );
- yield from $this->reader->streamRawCompressed(
- $context
- );
- }
+ yield from $found
+ ? $this->reader->streamRawCompressed(
+ $this->createContext( $file, $off, 0 )
+ )
+ : [];
}
- public function streamRawDelta(
- string $sha
- ): Generator {
+ public function streamRawDelta( string $sha ): Generator {
$found = false;
$file = '';
}
);
-
- if( $found ) {
- $context = $this->createContext(
- $file, $off, 0
- );
- yield from $this->reader->streamRawDelta(
- $context
- );
- }
+ yield from $found
+ ? $this->reader->streamRawDelta(
+ $this->createContext( $file, $off, 0 )
+ )
+ : [];
}
}
);
-
- if( $found ) {
- $context = $this->createContext(
- $file, $off, $depth
- );
- yield from $this->reader->streamEntryGenerator(
- $context
- );
- }
+ yield from $found
+ ? $this->reader->streamEntryGenerator(
+ $this->createContext( $file, $off, $depth )
+ )
+ : [];
}
int $offset
) use ( &$result ): void {
- $context = $this->createContext(
- $packFile, $offset, 0
+ $result = $this->reader->getSize(
+ $this->createContext( $packFile, $offset, 0 )
);
- $result = $this->reader->getSize( $context );
}
);
git/PackEntryReader.php
private DeltaDecoder $decoder;
private array $cache;
-
- public function __construct( DeltaDecoder $decoder ) {
- $this->decoder = $decoder;
- $this->cache = [];
- }
-
- public function getEntryMeta( PackContext $context ): array {
- return $context->computeArray(
- function( StreamReader $stream, int $offset ): array {
- $packStream = new GitPackStream( $stream );
- $packStream->seek( $offset );
- $hdr = $packStream->readVarInt();
-
- return [
- 'type' => $hdr['type'],
- 'size' => $hdr['size'],
- 'baseOffset' => $hdr['type'] === 6
- ? $offset - $packStream->readOffsetDelta()
- : 0,
- 'baseSha' => $hdr['type'] === 7
- ? \bin2hex( $packStream->read( 20 ) )
- : ''
- ];
- },
- [ 'type' => 0, 'size' => 0 ]
- );
- }
-
- public function getSize( PackContext $context ): int {
- return $context->computeIntDedicated(
- function( StreamReader $stream, int $offset ): int {
- $packStream = new GitPackStream( $stream );
- $packStream->seek( $offset );
- $hdr = $packStream->readVarInt();
-
- return $hdr['type'] === 6 || $hdr['type'] === 7
- ? $this->decoder->readDeltaTargetSize(
- $stream, $hdr['type']
- )
- : $hdr['size'];
- },
- 0
- );
- }
-
- public function read(
- PackContext $context,
- int $cap,
- callable $readShaBaseFn
- ): string {
- return $context->computeStringDedicated(
- function(
- StreamReader $s,
- int $o
- ) use ( $cap, $readShaBaseFn ): string {
- return $this->readWithStream(
- new GitPackStream( $s ),
- $o,
- $cap,
- $readShaBaseFn
- );
- },
- ''
- );
- }
-
- private function readWithStream(
- GitPackStream $stream,
- int $offset,
- int $cap,
- callable $readShaBaseFn
- ): string {
- $stream->seek( $offset );
- $hdr = $stream->readVarInt();
- $type = $hdr['type'];
-
- $result = isset( $this->cache[$offset] )
- ? ( $cap > 0 && \strlen( $this->cache[$offset] ) > $cap
- ? \substr( $this->cache[$offset], 0, $cap )
- : $this->cache[$offset] )
- : ( $type === 6
- ? $this->readOffsetDeltaContent(
- $stream, $offset, $cap, $readShaBaseFn
- )
- : ( $type === 7
- ? $this->readRefDeltaContent(
- $stream, $cap, $readShaBaseFn
- )
- : $this->inflate( $stream, $cap ) ) );
-
- if( $cap === 0 && !isset( $this->cache[$offset] ) ) {
- $this->cache[$offset] = $result;
-
- if( \count( $this->cache ) > self::MAX_CACHE ) {
- unset(
- $this->cache[\array_key_first( $this->cache )]
- );
- }
- }
-
- return $result;
- }
-
- private function readOffsetDeltaContent(
- GitPackStream $stream,
- int $offset,
- int $cap,
- callable $readShaBaseFn
- ): string {
- $neg = $stream->readOffsetDelta();
- $cur = $stream->tell();
- $bData = $this->readWithStream(
- $stream,
- $offset - $neg,
- $cap,
- $readShaBaseFn
- );
-
- $stream->seek( $cur );
-
- return $this->decoder->apply(
- $bData,
- $this->inflate( $stream ),
- $cap
- );
- }
-
- private function readRefDeltaContent(
- GitPackStream $stream,
- int $cap,
- callable $readShaBaseFn
- ): string {
- $sha = \bin2hex( $stream->read( 20 ) );
- $cur = $stream->tell();
- $bas = $readShaBaseFn( $sha, $cap );
-
- $stream->seek( $cur );
-
- return $this->decoder->apply(
- $bas,
- $this->inflate( $stream ),
- $cap
- );
- }
-
- public function streamRawCompressed(
- PackContext $context
- ): Generator {
- yield from $context->streamGenerator(
- function( StreamReader $stream, int $offset ): Generator {
- $packStream = new GitPackStream( $stream );
- $packStream->seek( $offset );
- $hdr = $packStream->readVarInt();
-
- yield from $hdr['type'] !== 6 && $hdr['type'] !== 7
- ? (new ZlibExtractorStream())->stream( $stream )
- : [];
- }
- );
- }
-
- public function streamRawDelta( PackContext $context ): Generator {
- yield from $context->streamGenerator(
- function( StreamReader $stream, int $offset ): Generator {
- $packStream = new GitPackStream( $stream );
- $packStream->seek( $offset );
- $hdr = $packStream->readVarInt();
-
- if( $hdr['type'] === 6 ) {
- $packStream->readOffsetDelta();
- } elseif( $hdr['type'] === 7 ) {
- $packStream->read( 20 );
- }
-
- yield from (new ZlibExtractorStream())->stream( $stream );
- }
- );
- }
-
- public function streamEntryGenerator(
- PackContext $context
- ): Generator {
- yield from $context->streamGeneratorDedicated(
- function(
- StreamReader $stream,
- int $offset
- ) use ( $context ): Generator {
- $packStream = new GitPackStream( $stream );
- $packStream->seek( $offset );
- $hdr = $packStream->readVarInt();
-
- yield from $hdr['type'] === 6 || $hdr['type'] === 7
- ? $this->streamDeltaObjectGenerator(
- $packStream,
- $context,
- $hdr['type'],
- $offset
- )
- : (new ZlibInflaterStream())->stream( $stream );
- }
- );
- }
-
- private function streamDeltaObjectGenerator(
- GitPackStream $stream,
- PackContext $context,
- int $type,
- int $offset
- ): Generator {
- yield from $context->isWithinDepth( self::MAX_DEPTH )
- ? ( $type === 6
- ? $this->processOffsetDelta(
- $stream, $context, $offset
- )
- : $this->processRefDelta( $stream, $context ) )
- : [];
- }
-
- private function readSizeWithStream(
- GitPackStream $stream,
- int $offset
- ): int {
- $cur = $stream->tell();
- $stream->seek( $offset );
- $hdr = $stream->readVarInt();
-
- $result = isset( $this->cache[$offset] )
- ? \strlen( $this->cache[$offset] )
- : ( $hdr['type'] === 6 || $hdr['type'] === 7
- ? $this->decoder->readDeltaTargetSize(
- $stream, $hdr['type']
- )
- : $hdr['size'] );
-
- if( !isset( $this->cache[$offset] ) ) {
- $stream->seek( $cur );
- }
-
- return $result;
- }
-
- private function processOffsetDelta(
- GitPackStream $stream,
- PackContext $context,
- int $offset
- ): Generator {
- $neg = $stream->readOffsetDelta();
- $cur = $stream->tell();
- $baseOff = $offset - $neg;
-
- $baseSrc = isset( $this->cache[$baseOff] )
- ? $this->cache[$baseOff]
- : ( $this->readSizeWithStream( $stream, $baseOff )
- <= self::MAX_BASE_RAM
- ? $this->readWithStream(
- $stream,
- $baseOff,
- 0,
- function(
- string $sha,
- int $cap
- ) use ( $context ): string {
- return $this->resolveBaseSha(
- $sha, $cap, $context
- );
- }
- )
- : $this->collectBase(
- $this->streamEntryGenerator(
- $context->deriveOffsetContext( $neg )
- )
- ) );
-
- $stream->seek( $cur );
-
- yield from $this->decoder->applyStreamGenerator(
- $stream, $baseSrc
- );
- }
-
- private function processRefDelta(
- GitPackStream $stream,
- PackContext $context
- ): Generator {
- $baseSha = \bin2hex( $stream->read( 20 ) );
- $cur = $stream->tell();
- $size = $context->resolveBaseSize( $baseSha );
-
- $baseSrc = $size <= self::MAX_BASE_RAM
- ? $this->resolveBaseSha( $baseSha, 0, $context )
- : $this->collectBase(
- $context->resolveBaseStream( $baseSha )
- );
-
- $stream->seek( $cur );
-
- yield from $this->decoder->applyStreamGenerator(
- $stream, $baseSrc
- );
- }
-
- private function collectBase(
- iterable $chunks
- ): BufferedReader|string {
- $parts = [];
- $total = 0;
- $tmp = false;
-
- foreach( $chunks as $chunk ) {
- $total += \strlen( $chunk );
-
- if( $tmp instanceof BufferedReader ) {
- $tmp->write( $chunk );
- } elseif( $total > self::MAX_BASE_RAM ) {
- $tmp = new BufferedReader(
- 'php://temp/maxmemory:65536', 'w+b'
- );
-
- foreach( $parts as $part ) {
- $tmp->write( $part );
- }
-
- $tmp->write( $chunk );
- $parts = [];
- } else {
- $parts[] = $chunk;
- }
- }
-
- if( $tmp instanceof BufferedReader ) {
- $tmp->rewind();
- }
-
- return $tmp === false ? \implode( '', $parts ) : $tmp;
- }
-
- private function resolveBaseSha(
- string $sha,
- int $cap,
- PackContext $context
- ): string {
- $chunks = [];
-
- foreach(
- $context->resolveBaseStream( $sha ) as $chunk
- ) {
+ private int $cacheSize;
+
+ public function __construct( DeltaDecoder $decoder ) {
+ $this->decoder = $decoder;
+ $this->cache = [];
+ $this->cacheSize = 0;
+ }
+
+ public function getEntryMeta( PackContext $context ): array {
+ return $context->computeArray(
+ function( StreamReader $stream, int $offset ): array {
+ $packStream = new GitPackStream( $stream );
+ $packStream->seek( $offset );
+ $hdr = $packStream->readVarInt();
+
+ return [
+ 'type' => $hdr['type'],
+ 'size' => $hdr['size'],
+ 'baseOffset' => $hdr['type'] === 6
+ ? $offset - $packStream->readOffsetDelta()
+ : 0,
+ 'baseSha' => $hdr['type'] === 7
+ ? \bin2hex( $packStream->read( 20 ) )
+ : ''
+ ];
+ },
+ [ 'type' => 0, 'size' => 0 ]
+ );
+ }
+
+ public function getSize( PackContext $context ): int {
+ return $context->computeIntDedicated(
+ function( StreamReader $stream, int $offset ): int {
+ $packStream = new GitPackStream( $stream );
+ $packStream->seek( $offset );
+ $hdr = $packStream->readVarInt();
+
+ return $hdr['type'] === 6 || $hdr['type'] === 7
+ ? $this->decoder->readDeltaTargetSize(
+ $stream, $hdr['type']
+ )
+ : $hdr['size'];
+ },
+ 0
+ );
+ }
+
+ public function read(
+ PackContext $context,
+ int $cap,
+ callable $readShaBaseFn
+ ): string {
+ return $context->computeStringDedicated(
+ function(
+ StreamReader $s,
+ int $o
+ ) use ( $cap, $readShaBaseFn ): string {
+ return $this->readWithStream(
+ new GitPackStream( $s ), $o, $cap, $readShaBaseFn
+ );
+ },
+ ''
+ );
+ }
+
+ private function readWithStream(
+ GitPackStream $stream,
+ int $offset,
+ int $cap,
+ callable $readShaBaseFn
+ ): string {
+ $stream->seek( $offset );
+ $hdr = $stream->readVarInt();
+ $type = $hdr['type'];
+
+ $result = isset( $this->cache[$offset] )
+ ? ( $cap > 0 && \strlen( $this->cache[$offset] ) > $cap
+ ? \substr( $this->cache[$offset], 0, $cap )
+ : $this->cache[$offset] )
+ : ( $type === 6
+ ? $this->readOffsetDeltaContent(
+ $stream, $offset, $cap, $readShaBaseFn
+ )
+ : ( $type === 7
+ ? $this->readRefDeltaContent(
+ $stream, $cap, $readShaBaseFn
+ )
+ : $this->inflate( $stream, $cap ) ) );
+
+ if( $cap === 0 && !isset( $this->cache[$offset] ) ) {
+ $this->cache[$offset] = $result;
+ $this->cacheSize++;
+
+ if( $this->cacheSize > self::MAX_CACHE ) {
+ unset( $this->cache[\array_key_first( $this->cache )] );
+ $this->cacheSize--;
+ }
+ }
+
+ return $result;
+ }
+
+ private function readOffsetDeltaContent(
+ GitPackStream $stream,
+ int $offset,
+ int $cap,
+ callable $readShaBaseFn
+ ): string {
+ $neg = $stream->readOffsetDelta();
+ $cur = $stream->tell();
+ $bData = $this->readWithStream(
+ $stream, $offset - $neg, $cap, $readShaBaseFn
+ );
+
+ $stream->seek( $cur );
+
+ return $this->decoder->apply(
+ $bData,
+ $this->inflate( $stream ),
+ $cap
+ );
+ }
+
+ private function readRefDeltaContent(
+ GitPackStream $stream,
+ int $cap,
+ callable $readShaBaseFn
+ ): string {
+ $sha = \bin2hex( $stream->read( 20 ) );
+ $cur = $stream->tell();
+ $bas = $readShaBaseFn( $sha, $cap );
+
+ $stream->seek( $cur );
+
+ return $this->decoder->apply(
+ $bas,
+ $this->inflate( $stream ),
+ $cap
+ );
+ }
+
+ public function streamRawCompressed(
+ PackContext $context
+ ): Generator {
+ yield from $context->streamGenerator(
+ function( StreamReader $stream, int $offset ): Generator {
+ $packStream = new GitPackStream( $stream );
+ $packStream->seek( $offset );
+ $hdr = $packStream->readVarInt();
+
+ yield from $hdr['type'] !== 6 && $hdr['type'] !== 7
+ ? (new ZlibExtractorStream())->stream( $stream )
+ : [];
+ }
+ );
+ }
+
+ public function streamRawDelta( PackContext $context ): Generator {
+ yield from $context->streamGenerator(
+ function( StreamReader $stream, int $offset ): Generator {
+ $packStream = new GitPackStream( $stream );
+ $packStream->seek( $offset );
+ $hdr = $packStream->readVarInt();
+
+ if( $hdr['type'] === 6 ) {
+ $packStream->readOffsetDelta();
+ } elseif( $hdr['type'] === 7 ) {
+ $packStream->read( 20 );
+ }
+
+ yield from (new ZlibExtractorStream())->stream( $stream );
+ }
+ );
+ }
+
+ public function streamEntryGenerator(
+ PackContext $context
+ ): Generator {
+ yield from $context->streamGeneratorDedicated(
+ function(
+ StreamReader $stream,
+ int $offset
+ ) use ( $context ): Generator {
+ $packStream = new GitPackStream( $stream );
+ $packStream->seek( $offset );
+ $hdr = $packStream->readVarInt();
+
+ yield from $hdr['type'] === 6 || $hdr['type'] === 7
+ ? $this->streamDeltaObjectGenerator(
+ $packStream, $context, $hdr['type'], $offset
+ )
+ : (new ZlibInflaterStream())->stream( $stream );
+ }
+ );
+ }
+
+ private function streamDeltaObjectGenerator(
+ GitPackStream $stream,
+ PackContext $context,
+ int $type,
+ int $offset
+ ): Generator {
+ yield from $context->isWithinDepth( self::MAX_DEPTH )
+ ? ( $type === 6
+ ? $this->processOffsetDelta( $stream, $context, $offset )
+ : $this->processRefDelta( $stream, $context ) )
+ : [];
+ }
+
+ private function readSizeWithStream(
+ GitPackStream $stream,
+ int $offset
+ ): int {
+ $cur = $stream->tell();
+ $stream->seek( $offset );
+ $hdr = $stream->readVarInt();
+
+ $result = isset( $this->cache[$offset] )
+ ? \strlen( $this->cache[$offset] )
+ : ( $hdr['type'] === 6 || $hdr['type'] === 7
+ ? $this->decoder->readDeltaTargetSize(
+ $stream, $hdr['type']
+ )
+ : $hdr['size'] );
+
+ if( !isset( $this->cache[$offset] ) ) {
+ $stream->seek( $cur );
+ }
+
+ return $result;
+ }
+
+ private function processOffsetDelta(
+ GitPackStream $stream,
+ PackContext $context,
+ int $offset
+ ): Generator {
+ $neg = $stream->readOffsetDelta();
+ $cur = $stream->tell();
+ $baseOff = $offset - $neg;
+
+ $baseSrc = isset( $this->cache[$baseOff] )
+ ? $this->cache[$baseOff]
+ : ( $this->readSizeWithStream( $stream, $baseOff )
+ <= self::MAX_BASE_RAM
+ ? $this->readWithStream(
+ $stream,
+ $baseOff,
+ 0,
+ function( string $sha, int $cap ) use ( $context ): string {
+ return $this->resolveBaseSha( $sha, $cap, $context );
+ }
+ )
+ : $this->collectBase(
+ $this->streamEntryGenerator(
+ $context->deriveOffsetContext( $neg )
+ )
+ ) );
+
+ $stream->seek( $cur );
+
+ yield from $this->decoder->applyStreamGenerator(
+ $stream, $baseSrc
+ );
+ }
+
+ private function processRefDelta(
+ GitPackStream $stream,
+ PackContext $context
+ ): Generator {
+ $baseSha = \bin2hex( $stream->read( 20 ) );
+ $cur = $stream->tell();
+ $size = $context->resolveBaseSize( $baseSha );
+
+ $baseSrc = $size <= self::MAX_BASE_RAM
+ ? $this->resolveBaseSha( $baseSha, 0, $context )
+ : $this->collectBase(
+ $context->resolveBaseStream( $baseSha )
+ );
+
+ $stream->seek( $cur );
+
+ yield from $this->decoder->applyStreamGenerator(
+ $stream, $baseSrc
+ );
+ }
+
+ private function collectBase(
+ iterable $chunks
+ ): BufferedReader|string {
+ $parts = [];
+ $total = 0;
+ $tmp = false;
+
+ foreach( $chunks as $chunk ) {
+ $total += \strlen( $chunk );
+
+ if( $tmp instanceof BufferedReader ) {
+ $tmp->write( $chunk );
+ } elseif( $total > self::MAX_BASE_RAM ) {
+ $tmp = new BufferedReader(
+ 'php://temp/maxmemory:65536', 'w+b'
+ );
+
+ foreach( $parts as $part ) {
+ $tmp->write( $part );
+ }
+
+ $tmp->write( $chunk );
+ $parts = [];
+ } else {
+ $parts[] = $chunk;
+ }
+ }
+
+ if( $tmp instanceof BufferedReader ) {
+ $tmp->rewind();
+ }
+
+ return $tmp === false ? \implode( '', $parts ) : $tmp;
+ }
+
+ private function resolveBaseSha(
+ string $sha,
+ int $cap,
+ PackContext $context
+ ): string {
+ $chunks = [];
+
+ foreach( $context->resolveBaseStream( $sha ) as $chunk ) {
$chunks[] = $chunk;
}
git/PackfileWriter.php
$written[$sha] = $outPos;
$baseSha = $entry['baseSha'];
-
- $reuse = $baseSha !== ''
+ $reuse = $baseSha !== ''
&& isset( $written[$baseSha] );
-
- if( $reuse ) {
- $hdr = $this->encodeEntryHeader(
- 6, $entry['deltaSize']
- );
- $hdr .= $this->encodeOffsetDelta(
- $outPos - $written[$baseSha]
- );
- \hash_update( $ctx, $hdr );
- $outPos += \strlen( $hdr );
- yield $hdr;
+ $hdr = $reuse
+ ? $this->encodeEntryHeader( 6, $entry['deltaSize'] )
+ . $this->encodeOffsetDelta( $outPos - $written[$baseSha] )
+ : $this->encodeEntryHeader(
+ $entry['logicalType'],
+ $this->getObjectSize( $sha )
+ );
- foreach(
- $this->packs->streamRawDelta(
- $sha
- ) as $chunk
- ) {
- \hash_update( $ctx, $chunk );
- $outPos += \strlen( $chunk );
- yield $chunk;
- }
- } else {
- $size = $this->getObjectSize( $sha );
- $hdr = $this->encodeEntryHeader(
- $entry['logicalType'], $size
- );
+ \hash_update( $ctx, $hdr );
+ $outPos += \strlen( $hdr );
+ yield $hdr;
- \hash_update( $ctx, $hdr );
- $outPos += \strlen( $hdr );
- yield $hdr;
+ $stream = $reuse
+ ? $this->packs->streamRawDelta( $sha )
+ : $this->streamCompressed( $sha );
- foreach(
- $this->streamCompressed(
- $sha
- ) as $chunk
- ) {
- \hash_update( $ctx, $chunk );
- $outPos += \strlen( $chunk );
- yield $chunk;
- }
+ foreach( $stream as $chunk ) {
+ \hash_update( $ctx, $chunk );
+ $outPos += \strlen( $chunk );
+ yield $chunk;
}
}
yield \hash_final( $ctx, true );
}
- private function buildEntries(
- array $objs
- ): array {
+ private function buildEntries( array $objs ): array {
$entries = [];
$offToSha = [];
if( $meta['file'] !== '' ) {
- $offToSha[$meta['file']][$meta['offset']]
- = $sha;
+ $offToSha[$meta['file']][$meta['offset']] = $sha;
}
}
foreach( $entries as &$e ) {
- if(
- $e['packType'] === 6
- && $e['baseOffset'] > 0
- ) {
- $e['baseSha']
- = $offToSha[$e['packFile']][$e['baseOffset']]
- ?? '';
- }
+ $e['baseSha'] = $e['packType'] === 6 && $e['baseOffset'] > 0
+ ? ( $offToSha[$e['packFile']][$e['baseOffset']] ?? '' )
+ : $e['baseSha'];
}
}
- private function getObjectSize(
- string $sha
- ): int {
+ private function getObjectSize( string $sha ): int {
return $this->packs->getSize( $sha )
?: $this->loose->getSize( $sha );
}
-
- private function streamCompressed(
- string $sha
- ): Generator {
- $yielded = false;
- foreach(
- $this->packs->streamRawCompressed(
- $sha
- ) as $chunk
- ) {
- $yielded = true;
- yield $chunk;
- }
+ private function streamCompressed( string $sha ): Generator {
+ $generator = $this->packs->streamRawCompressed( $sha );
+ $generator->rewind();
- if( !$yielded ) {
- $deflate = \deflate_init(
- \ZLIB_ENCODING_DEFLATE
- );
+ if( $generator->valid() ) {
+ yield from $generator;
+ } else {
+ $deflate = \deflate_init( \ZLIB_ENCODING_DEFLATE );
- foreach(
- $this->getDecompressedChunks(
- $sha
- ) as $raw
- ) {
+ foreach( $this->getDecompressedChunks( $sha ) as $raw ) {
$compressed = \deflate_add(
$deflate, $raw, \ZLIB_NO_FLUSH
);
if( $compressed !== '' ) {
yield $compressed;
}
}
- $final = \deflate_add(
- $deflate, '', \ZLIB_FINISH
- );
+ $final = \deflate_add( $deflate, '', \ZLIB_FINISH );
if( $final !== '' ) {
yield $final;
}
}
}
-
- private function getDecompressedChunks(
- string $sha
- ): Generator {
- $any = false;
- foreach(
- $this->loose->streamChunks( $sha ) as $chunk
- ) {
- $any = true;
- yield $chunk;
- }
+ private function getDecompressedChunks( string $sha ): Generator {
+ $looseGen = $this->loose->streamChunks( $sha );
+ $looseGen->rewind();
- if( !$any ) {
- foreach(
- $this->packs->streamGenerator(
- $sha
- ) as $chunk
- ) {
- $any = true;
- yield $chunk;
- }
- }
+ if( $looseGen->valid() ) {
+ yield from $looseGen;
+ } else {
+ $packGen = $this->packs->streamGenerator( $sha );
+ $packGen->rewind();
- if( !$any ) {
- $data = $this->packs->read( $sha );
+ if( $packGen->valid() ) {
+ yield from $packGen;
+ } else {
+ $data = $this->packs->read( $sha );
- if( $data !== '' ) {
- yield $data;
+ if( $data !== '' ) {
+ yield $data;
+ }
}
}
}
- private function encodeEntryHeader(
- int $type,
- int $size
- ): string {
+ private function encodeEntryHeader( int $type, int $size ): string {
$byte = $type << 4 | $size & 0x0f;
$sz = $size >> 4;
$hdr = '';
while( $sz > 0 ) {
$hdr .= \chr( $byte | 0x80 );
$byte = $sz & 0x7f;
$sz >>= 7;
}
-
- $hdr .= \chr( $byte );
- return $hdr;
+ return $hdr . \chr( $byte );
}
- private function encodeOffsetDelta(
- int $offset
- ): string {
+ private function encodeOffsetDelta( int $offset ): string {
$buf = \chr( $offset & 0x7F );
$n = $offset >> 7;
while( $n > 0 ) {
$n--;
- $buf = \chr( 0x80 | ($n & 0x7F) ) . $buf;
+ $buf = \chr( 0x80 | $n & 0x7F ) . $buf;
$n >>= 7;
}