Dave Jarvis' Repositories

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

Fixes doubled files

Author Dave Jarvis <email>
Date 2026-02-16 12:25:36 GMT-0800
Commit 09922570e4e8b127a0445e3a68f954267a3b8fdc
Parent d1eedc6
git/Git.php
$file = new MissingFile();
- if( isset( $info['sha'] ) && !$info['isDir'] && $info['sha'] !== '' ) {
- $file = new File(
- basename( $path ),
- $info['sha'],
- $info['mode'],
- 0,
- $this->getObjectSize( $info['sha'] ),
- $this->peek( $info['sha'] )
- );
- }
-
- return $file;
- }
-
- public function getObjectSize( string $sha, string $path = '' ): int {
- $target = $sha;
-
- if( $path !== '' ) {
- $info = $this->resolvePath(
- $this->getTreeSha( $this->resolve( $sha ) ),
- $path
- );
- $target = $info['sha'] ?? '';
- }
-
- return $target !== ''
- ? $this->packs->getSize( $target ) ?? $this->getLooseObjectSize( $target )
- : 0;
- }
-
- public function stream(
- string $sha,
- callable $callback,
- string $path = ''
- ): void {
- $target = $sha;
-
- if( $path !== '' ) {
- $info = $this->resolvePath(
- $this->getTreeSha( $this->resolve( $sha ) ),
- $path
- );
- $target = isset( $info['isDir'] ) && !$info['isDir']
- ? $info['sha']
- : '';
- }
-
- if( $target !== '' ) {
- $this->slurp( $target, $callback );
- }
- }
-
- private function getTreeSha( string $commitOrTreeSha ): string {
- $data = $this->read( $commitOrTreeSha );
- $sha = $commitOrTreeSha;
-
- if( preg_match( '/^object ([0-9a-f]{40})/m', $data, $matches ) ) {
- $sha = $this->getTreeSha( $matches[1] );
- }
-
- if( $sha === $commitOrTreeSha &&
- preg_match( '/^tree ([0-9a-f]{40})/m', $data, $matches ) ) {
- $sha = $matches[1];
- }
-
- return $sha;
- }
-
- private function resolvePath( string $treeSha, string $path ): array {
- $parts = explode( '/', trim( $path, '/' ) );
- $sha = $treeSha;
- $mode = '40000';
-
- foreach( $parts as $part ) {
- $entry = $part !== '' && $sha !== ''
- ? $this->findTreeEntry( $sha, $part )
- : [ 'sha' => '', 'mode' => '' ];
-
- $sha = $entry['sha'];
- $mode = $entry['mode'];
- }
-
- return [
- 'sha' => $sha,
- 'mode' => $mode,
- 'isDir' => $mode === '40000' || $mode === '040000'
- ];
- }
-
- private function findTreeEntry( string $treeSha, string $name ): array {
- $data = $this->read( $treeSha );
- $pos = 0;
- $len = strlen( $data );
- $entry = [ 'sha' => '', 'mode' => '' ];
-
- while( $pos < $len ) {
- $space = strpos( $data, ' ', $pos );
- $eos = strpos( $data, "\0", $space );
-
- if( $space === false || $eos === false ) {
- break;
- }
-
- if( substr( $data, $space + 1, $eos - $space - 1 ) === $name ) {
- $entry = [
- 'sha' => bin2hex( substr( $data, $eos + 1, 20 ) ),
- 'mode' => substr( $data, $pos, $space - $pos )
- ];
- break;
- }
-
- $pos = $eos + 21;
- }
-
- return $entry;
- }
-
- private function parseTagData(
- string $name,
- string $sha,
- string $data
- ): Tag {
- $isAnn = strncmp( $data, 'object ', 7 ) === 0;
- $pattern = $isAnn
- ? '/^tagger (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
- : '/^author (.*) <(.*)> (\d+) [+\-]\d{4}$/m';
- $id = $this->parseIdentity( $data, $pattern );
- $target = $isAnn
- ? $this->extractPattern( $data, '/^object (.*)$/m', 1, $sha )
- : $sha;
-
- return new Tag(
- $name,
- $sha,
- $target,
- $id['timestamp'],
- $this->extractMessage( $data ),
- $id['name']
- );
- }
-
- private function extractPattern(
- string $data,
- string $pattern,
- int $group,
- string $default = ''
- ): string {
- return preg_match( $pattern, $data, $matches )
- ? $matches[$group]
- : $default;
- }
-
- private function parseIdentity( string $data, string $pattern ): array {
- $found = preg_match( $pattern, $data, $matches );
-
- return [
- 'name' => $found ? trim($matches[1]) : 'Unknown',
- 'email' => $found ? $matches[2] : '',
- 'timestamp' => $found ? (int)$matches[3] : 0
- ];
- }
-
- private function extractMessage( string $data ): string {
- $pos = strpos( $data, "\n\n" );
-
- return $pos !== false ? trim( substr( $data, $pos + 2 ) ) : '';
- }
-
- public function peek( string $sha, int $length = 255 ): string {
- $size = $this->packs->getSize( $sha );
-
- return $size === null
- ? $this->peekLooseObject( $sha, $length )
- : $this->packs->peek( $sha, $length ) ?? '';
- }
-
- public function read( string $sha ): string {
- $size = $this->getObjectSize( $sha );
- $content = '';
-
- if( $size > 0 && $size <= self::MAX_READ_SIZE ) {
- $this->slurp( $sha, function( $chunk ) use ( &$content ) {
- $content .= $chunk;
- } );
- }
-
- return $content;
- }
-
- private function slurp( string $sha, callable $callback ): void {
- $path = $this->getLoosePath( $sha );
-
- if( is_file($path) ) {
- $this->slurpLooseObject( $path, $callback );
- }
-
- if( !is_file($path) ) {
- $this->slurpPackedObject( $sha, $callback );
- }
- }
-
- private function iterateInflated( string $path, callable $processor ): void {
- $this->withInflatedFile(
- $path,
- function( $handle, $inflator ) use ( $processor ) {
- $found = false;
- $buffer = '';
-
- while( !feof($handle) ) {
- $inflated = inflate_add( $inflator, fread( $handle, 16384 ) );
-
- if( $inflated === false ) {
- break;
- }
-
- if( !$found ) {
- $buffer .= $inflated;
- $eos = strpos( $buffer, "\0" );
-
- if( $eos !== false ) {
- $found = true;
- $body = substr( $buffer, $eos + 1 );
- $head = substr( $buffer, 0, $eos );
-
- if( $processor( $body, $head ) === false ) {
- break;
- }
- }
- }
-
- if( $found ) {
- if( $processor( $inflated, null ) === false ) {
- break;
- }
- }
- }
- }
- );
- }
-
- private function slurpLooseObject(
- string $path,
- callable $callback
- ): void {
- $this->iterateInflated(
- $path,
- function( $chunk ) use ( $callback ) {
- if( $chunk !== '' ) {
- $callback( $chunk );
- }
-
- return true;
- }
- );
- }
-
- private function withInflatedFile( string $path, callable $callback ): void {
- $handle = fopen( $path, 'rb' );
- $infl = $handle ? inflate_init( ZLIB_ENCODING_DEFLATE ) : null;
-
- if( $handle && $infl ) {
- $callback( $handle, $infl );
- fclose( $handle );
- }
- }
-
- private function slurpPackedObject( string $sha, callable $callback ): void {
- $streamed = $this->packs->stream( $sha, $callback );
-
- if( !$streamed ) {
- $data = $this->packs->read( $sha );
-
- if( $data !== null && $data !== '' ) {
- $callback( $data );
- }
- }
- }
-
- private function peekLooseObject( string $sha, int $length ): string {
- $path = $this->getLoosePath( $sha );
-
- return is_file($path)
- ? $this->inflateLooseObjectPrefix( $path, $length )
- : '';
- }
-
- private function inflateLooseObjectPrefix(
- string $path,
- int $length
- ): string {
- $buf = '';
-
- $this->iterateInflated(
- $path,
- function( $chunk ) use ( $length, &$buf ) {
- $buf .= $chunk;
-
- return strlen($buf) < $length;
- }
- );
-
- return substr( $buf, 0, $length );
- }
-
- public function history( string $ref, int $limit, callable $callback ): void {
- $sha = $this->resolve( $ref );
- $count = 0;
-
- while( $sha !== '' && $count < $limit ) {
- $commit = $this->parseCommit( $sha );
-
- if( $commit->sha === '' ) {
- $sha = '';
- }
-
- if( $sha !== '' ) {
- $callback( $commit );
- $sha = $commit->parentSha;
- $count++;
- }
- }
- }
-
- private function parseCommit( string $sha ): object {
- $data = $this->read( $sha );
-
- return $data !== ''
- ? $this->buildCommitObject( $sha, $data )
- : (object)[ 'sha' => '' ];
- }
-
- private function buildCommitObject( string $sha, string $data ): object {
- $id = $this->parseIdentity( $data, '/^author (.*) <(.*)> (\d+)/m' );
-
- return (object)[
- 'sha' => $sha,
- 'message' => $this->extractMessage( $data ),
- 'author' => $id['name'],
- 'email' => $id['email'],
- 'date' => $id['timestamp'],
- 'parentSha' => $this->extractPattern( $data, '/^parent (.*)$/m', 1 )
- ];
- }
-
- private function walkTree( string $sha, callable $callback ): void {
- $data = $this->read( $sha );
- $tree = $data !== '' && preg_match( '/^tree (.*)$/m', $data, $m )
- ? $this->read($m[1])
- : $data;
-
- if( $tree !== '' && $this->isTreeData($tree) ) {
- $this->processTree( $tree, $callback );
- }
- }
-
- private function processTree( string $data, callable $callback ): void {
- $pos = 0;
- $len = strlen( $data );
-
- while( $pos < $len ) {
- $entry = $this->parseTreeEntry( $data, $pos, $len );
-
- if( $entry === null ) {
- break;
- }
-
- $callback( $entry['file'] );
- $pos = $entry['nextPosition'];
- }
- }
-
- private function parseTreeEntry(
- string $data,
- int $pos,
- int $len
- ): ?array {
- $space = strpos( $data, ' ', $pos );
- $eos = strpos( $data, "\0", $space );
-
- return $space !== false && $eos !== false && $eos + 21 <= $len
- ? $this->buildTreeEntryResult( $data, $pos, $space, $eos )
- : null;
- }
-
- private function buildTreeEntryResult(
- string $data,
- int $pos,
- int $space,
- int $eos
- ): array {
- $mode = substr( $data, $pos, $space - $pos );
- $sha = bin2hex( substr( $data, $eos + 1, 20 ) );
- $isD = $mode === '40000' || $mode === '040000';
-
- return [
- 'file' => new File(
- substr( $data, $space + 1, $eos - $space - 1 ),
- $sha,
- $mode,
- 0,
- $isD ? 0 : $this->getObjectSize( $sha ),
- $isD ? '' : $this->peek( $sha )
- ),
- 'nextPosition' => $eos + 21
- ];
- }
-
- private function isTreeData( string $data ): bool {
- $len = strlen( $data );
- $patt = '/^(40000|100644|100755|120000|160000) /';
- $match = $len >= 25 && preg_match( $patt, $data );
- $eos = $match ? strpos( $data, "\0" ) : false;
-
- return $match && $eos !== false && $eos + 21 <= $len;
- }
-
- private function getLoosePath( string $sha ): string {
- return "{$this->objectsPath}/" .
- substr( $sha, 0, 2 ) . "/" .
- substr( $sha, 2 );
- }
-
- private function getLooseObjectSize( string $sha ): int {
- $path = $this->getLoosePath( $sha );
-
- return is_file($path) ? $this->readLooseObjectHeader($path) : 0;
- }
-
- private function readLooseObjectHeader( string $path ): int {
- $size = 0;
-
- $this->iterateInflated( $path, function( $chunk, $header ) use ( &$size ) {
- if( $header !== null ) {
- $parts = explode( ' ', $header );
- $size = isset( $parts[1] ) ? (int)$parts[1] : 0;
- }
-
- return false;
- } );
-
- return $size;
- }
-
- public function streamRaw( string $subPath ): bool {
- return strpos($subPath, '..') === false
- ? $this->streamRawFile( $subPath )
- : false;
- }
-
- private function streamRawFile( string $subPath ): bool {
- $path = "{$this->repoPath}/$subPath";
-
- return is_file($path) ? $this->streamIfPathValid($path) : false;
- }
-
- private function streamIfPathValid( string $fullPath ): bool {
- $real = realpath( $fullPath );
- $repo = realpath( $this->repoPath );
- $isValid = $real && strpos($real, $repo) === 0;
-
- return $isValid ? readfile($fullPath) !== false : false;
- }
-
- public function eachRef( callable $callback ): void {
- $head = $this->resolve( 'HEAD' );
-
- if( $head !== '' ) {
- $callback( 'HEAD', $head );
- }
-
- $this->refs->scanRefs( 'refs/heads', function( $n, $s ) use ( $callback ) {
- $callback( "refs/heads/$n", $s );
- } );
-
- $this->refs->scanRefs( 'refs/tags', function( $n, $s ) use ( $callback ) {
- $callback( "refs/tags/$n", $s );
- } );
- }
-
- public function collectObjects( array $wants, array $haves = [] ): array {
- $objs = [];
- $seen = [];
-
- foreach( $wants as $sha ) {
- $this->collectObjectsRecursive( $sha, $objs, $seen );
- }
-
- foreach( $haves as $sha ) {
- unset($objs[$sha]);
- }
-
- return $objs;
- }
-
- private function collectObjectsRecursive(
- string $sha,
- array &$objs,
- array &$seen
- ): void {
- if( !isset( $seen[$sha] ) ) {
+ if( isset($info['sha']) && !$info['isDir'] && $info['sha'] !== '' ) {
+ $file = new File(
+ basename( $path ),
+ $info['sha'],
+ $info['mode'],
+ 0,
+ $this->getObjectSize( $info['sha'] ),
+ $this->peek( $info['sha'] )
+ );
+ }
+
+ return $file;
+ }
+
+ public function getObjectSize( string $sha, string $path = '' ): int {
+ $target = $sha;
+
+ if( $path !== '' ) {
+ $info = $this->resolvePath(
+ $this->getTreeSha( $this->resolve( $sha ) ),
+ $path
+ );
+ $target = $info['sha'] ?? '';
+ }
+
+ return $target !== ''
+ ? $this->packs->getSize( $target ) ?? $this->getLooseObjectSize( $target )
+ : 0;
+ }
+
+ public function stream(
+ string $sha,
+ callable $callback,
+ string $path = ''
+ ): void {
+ $target = $sha;
+
+ if( $path !== '' ) {
+ $info = $this->resolvePath(
+ $this->getTreeSha( $this->resolve( $sha ) ),
+ $path
+ );
+ $target = isset($info['isDir']) && !$info['isDir']
+ ? $info['sha']
+ : '';
+ }
+
+ if( $target !== '' ) {
+ $this->slurp( $target, $callback );
+ }
+ }
+
+ private function getTreeSha( string $commitOrTreeSha ): string {
+ $data = $this->read( $commitOrTreeSha );
+ $sha = $commitOrTreeSha;
+
+ if( preg_match( '/^object ([0-9a-f]{40})/m', $data, $matches ) ) {
+ $sha = $this->getTreeSha( $matches[1] );
+ }
+
+ if( $sha === $commitOrTreeSha &&
+ preg_match( '/^tree ([0-9a-f]{40})/m', $data, $matches ) ) {
+ $sha = $matches[1];
+ }
+
+ return $sha;
+ }
+
+ private function resolvePath( string $treeSha, string $path ): array {
+ $parts = explode( '/', trim( $path, '/' ) );
+ $sha = $treeSha;
+ $mode = '40000';
+
+ foreach( $parts as $part ) {
+ $entry = $part !== '' && $sha !== ''
+ ? $this->findTreeEntry( $sha, $part )
+ : [ 'sha' => '', 'mode' => '' ];
+
+ $sha = $entry['sha'];
+ $mode = $entry['mode'];
+ }
+
+ return [
+ 'sha' => $sha,
+ 'mode' => $mode,
+ 'isDir' => $mode === '40000' || $mode === '040000'
+ ];
+ }
+
+ private function findTreeEntry( string $treeSha, string $name ): array {
+ $data = $this->read( $treeSha );
+ $pos = 0;
+ $len = strlen( $data );
+ $entry = [ 'sha' => '', 'mode' => '' ];
+
+ while( $pos < $len ) {
+ $space = strpos( $data, ' ', $pos );
+ $eos = strpos( $data, "\0", $space );
+
+ if( $space === false || $eos === false ) {
+ break;
+ }
+
+ if( substr( $data, $space + 1, $eos - $space - 1 ) === $name ) {
+ $entry = [
+ 'sha' => bin2hex( substr( $data, $eos + 1, 20 ) ),
+ 'mode' => substr( $data, $pos, $space - $pos )
+ ];
+ break;
+ }
+
+ $pos = $eos + 21;
+ }
+
+ return $entry;
+ }
+
+ private function parseTagData(
+ string $name,
+ string $sha,
+ string $data
+ ): Tag {
+ $isAnn = strncmp( $data, 'object ', 7 ) === 0;
+ $pattern = $isAnn
+ ? '/^tagger (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
+ : '/^author (.*) <(.*)> (\d+) [+\-]\d{4}$/m';
+ $id = $this->parseIdentity( $data, $pattern );
+ $target = $isAnn
+ ? $this->extractPattern( $data, '/^object (.*)$/m', 1, $sha )
+ : $sha;
+
+ return new Tag(
+ $name,
+ $sha,
+ $target,
+ $id['timestamp'],
+ $this->extractMessage( $data ),
+ $id['name']
+ );
+ }
+
+ private function extractPattern(
+ string $data,
+ string $pattern,
+ int $group,
+ string $default = ''
+ ): string {
+ return preg_match( $pattern, $data, $matches )
+ ? $matches[$group]
+ : $default;
+ }
+
+ private function parseIdentity( string $data, string $pattern ): array {
+ $found = preg_match( $pattern, $data, $matches );
+
+ return [
+ 'name' => $found ? trim($matches[1]) : 'Unknown',
+ 'email' => $found ? $matches[2] : '',
+ 'timestamp' => $found ? (int)$matches[3] : 0
+ ];
+ }
+
+ private function extractMessage( string $data ): string {
+ $pos = strpos( $data, "\n\n" );
+
+ return $pos !== false ? trim( substr( $data, $pos + 2 ) ) : '';
+ }
+
+ public function peek( string $sha, int $length = 255 ): string {
+ $size = $this->packs->getSize( $sha );
+
+ return $size === null
+ ? $this->peekLooseObject( $sha, $length )
+ : $this->packs->peek( $sha, $length ) ?? '';
+ }
+
+ public function read( string $sha ): string {
+ $size = $this->getObjectSize( $sha );
+ $content = '';
+
+ if( $size > 0 && $size <= self::MAX_READ_SIZE ) {
+ $this->slurp( $sha, function( $chunk ) use ( &$content ) {
+ $content .= $chunk;
+ } );
+ }
+
+ return $content;
+ }
+
+ private function slurp( string $sha, callable $callback ): void {
+ $path = $this->getLoosePath( $sha );
+
+ if( is_file($path) ) {
+ $this->slurpLooseObject( $path, $callback );
+ }
+
+ if( !is_file($path) ) {
+ $this->slurpPackedObject( $sha, $callback );
+ }
+ }
+
+ private function iterateInflated( string $path, callable $processor ): void {
+ $this->withInflatedFile(
+ $path,
+ function( $handle, $inflator ) use ( $processor ) {
+ $found = false;
+ $buffer = '';
+
+ while( !feof($handle) ) {
+ $inflated = inflate_add( $inflator, fread( $handle, 16384 ) );
+
+ if( $inflated === false ) {
+ break;
+ }
+
+ if( !$found ) {
+ $buffer .= $inflated;
+ $eos = strpos( $buffer, "\0" );
+
+ if( $eos !== false ) {
+ $found = true;
+ $body = substr( $buffer, $eos + 1 );
+ $head = substr( $buffer, 0, $eos );
+
+ if( $processor( $body, $head ) === false ) {
+ break;
+ }
+ }
+ } elseif( $found ) {
+ if( $processor( $inflated, null ) === false ) {
+ break;
+ }
+ }
+ }
+ }
+ );
+ }
+
+ private function slurpLooseObject(
+ string $path,
+ callable $callback
+ ): void {
+ $this->iterateInflated(
+ $path,
+ function( $chunk ) use ( $callback ) {
+ if( $chunk !== '' ) {
+ $callback( $chunk );
+ }
+
+ return true;
+ }
+ );
+ }
+
+ private function withInflatedFile( string $path, callable $callback ): void {
+ $handle = fopen( $path, 'rb' );
+ $infl = $handle ? inflate_init( ZLIB_ENCODING_DEFLATE ) : null;
+
+ if( $handle && $infl ) {
+ $callback( $handle, $infl );
+ fclose( $handle );
+ }
+ }
+
+ private function slurpPackedObject( string $sha, callable $callback ): void {
+ $streamed = $this->packs->stream( $sha, $callback );
+
+ if( !$streamed ) {
+ $data = $this->packs->read( $sha );
+
+ if( $data !== null && $data !== '' ) {
+ $callback( $data );
+ }
+ }
+ }
+
+ private function peekLooseObject( string $sha, int $length ): string {
+ $path = $this->getLoosePath( $sha );
+
+ return is_file($path)
+ ? $this->inflateLooseObjectPrefix( $path, $length )
+ : '';
+ }
+
+ private function inflateLooseObjectPrefix(
+ string $path,
+ int $length
+ ): string {
+ $buf = '';
+
+ $this->iterateInflated(
+ $path,
+ function( $chunk ) use ( $length, &$buf ) {
+ $buf .= $chunk;
+
+ return strlen($buf) < $length;
+ }
+ );
+
+ return substr( $buf, 0, $length );
+ }
+
+ public function history( string $ref, int $limit, callable $callback ): void {
+ $sha = $this->resolve( $ref );
+ $count = 0;
+
+ while( $sha !== '' && $count < $limit ) {
+ $commit = $this->parseCommit( $sha );
+
+ if( $commit->sha === '' ) {
+ $sha = '';
+ }
+
+ if( $sha !== '' ) {
+ $callback( $commit );
+ $sha = $commit->parentSha;
+ $count++;
+ }
+ }
+ }
+
+ private function parseCommit( string $sha ): object {
+ $data = $this->read( $sha );
+
+ return $data !== ''
+ ? $this->buildCommitObject( $sha, $data )
+ : (object)[ 'sha' => '' ];
+ }
+
+ private function buildCommitObject( string $sha, string $data ): object {
+ $id = $this->parseIdentity( $data, '/^author (.*) <(.*)> (\d+)/m' );
+
+ return (object)[
+ 'sha' => $sha,
+ 'message' => $this->extractMessage( $data ),
+ 'author' => $id['name'],
+ 'email' => $id['email'],
+ 'date' => $id['timestamp'],
+ 'parentSha' => $this->extractPattern( $data, '/^parent (.*)$/m', 1 )
+ ];
+ }
+
+ private function walkTree( string $sha, callable $callback ): void {
+ $data = $this->read( $sha );
+ $tree = $data !== '' && preg_match( '/^tree (.*)$/m', $data, $m )
+ ? $this->read($m[1])
+ : $data;
+
+ if( $tree !== '' && $this->isTreeData($tree) ) {
+ $this->processTree( $tree, $callback );
+ }
+ }
+
+ private function processTree( string $data, callable $callback ): void {
+ $pos = 0;
+ $len = strlen( $data );
+
+ while( $pos < $len ) {
+ $entry = $this->parseTreeEntry( $data, $pos, $len );
+
+ if( $entry === null ) {
+ break;
+ }
+
+ $callback( $entry['file'] );
+ $pos = $entry['nextPosition'];
+ }
+ }
+
+ private function parseTreeEntry(
+ string $data,
+ int $pos,
+ int $len
+ ): ?array {
+ $space = strpos( $data, ' ', $pos );
+ $eos = strpos( $data, "\0", $space );
+
+ return $space !== false && $eos !== false && $eos + 21 <= $len
+ ? $this->buildTreeEntryResult( $data, $pos, $space, $eos )
+ : null;
+ }
+
+ private function buildTreeEntryResult(
+ string $data,
+ int $pos,
+ int $space,
+ int $eos
+ ): array {
+ $mode = substr( $data, $pos, $space - $pos );
+ $sha = bin2hex( substr( $data, $eos + 1, 20 ) );
+ $isD = $mode === '40000' || $mode === '040000';
+
+ return [
+ 'file' => new File(
+ substr( $data, $space + 1, $eos - $space - 1 ),
+ $sha,
+ $mode,
+ 0,
+ $isD ? 0 : $this->getObjectSize( $sha ),
+ $isD ? '' : $this->peek( $sha )
+ ),
+ 'nextPosition' => $eos + 21
+ ];
+ }
+
+ private function isTreeData( string $data ): bool {
+ $len = strlen( $data );
+ $patt = '/^(40000|100644|100755|120000|160000) /';
+ $match = $len >= 25 && preg_match( $patt, $data );
+ $eos = $match ? strpos( $data, "\0" ) : false;
+
+ return $match && $eos !== false && $eos + 21 <= $len;
+ }
+
+ private function getLoosePath( string $sha ): string {
+ return "{$this->objectsPath}/" .
+ substr( $sha, 0, 2 ) . "/" .
+ substr( $sha, 2 );
+ }
+
+ private function getLooseObjectSize( string $sha ): int {
+ $path = $this->getLoosePath( $sha );
+
+ return is_file($path) ? $this->readLooseObjectHeader($path) : 0;
+ }
+
+ private function readLooseObjectHeader( string $path ): int {
+ $size = 0;
+
+ $this->iterateInflated( $path, function( $chunk, $header ) use ( &$size ) {
+ if( $header !== null ) {
+ $parts = explode( ' ', $header );
+ $size = isset($parts[1]) ? (int)$parts[1] : 0;
+ }
+
+ return false;
+ } );
+
+ return $size;
+ }
+
+ public function streamRaw( string $subPath ): bool {
+ return strpos($subPath, '..') === false
+ ? $this->streamRawFile( $subPath )
+ : false;
+ }
+
+ private function streamRawFile( string $subPath ): bool {
+ $path = "{$this->repoPath}/$subPath";
+
+ return is_file($path) ? $this->streamIfPathValid($path) : false;
+ }
+
+ private function streamIfPathValid( string $fullPath ): bool {
+ $real = realpath( $fullPath );
+ $repo = realpath( $this->repoPath );
+ $isValid = $real && strpos($real, $repo) === 0;
+
+ return $isValid ? readfile($fullPath) !== false : false;
+ }
+
+ public function eachRef( callable $callback ): void {
+ $head = $this->resolve( 'HEAD' );
+
+ if( $head !== '' ) {
+ $callback( 'HEAD', $head );
+ }
+
+ $this->refs->scanRefs( 'refs/heads', function( $n, $s ) use ( $callback ) {
+ $callback( "refs/heads/$n", $s );
+ } );
+
+ $this->refs->scanRefs( 'refs/tags', function( $n, $s ) use ( $callback ) {
+ $callback( "refs/tags/$n", $s );
+ } );
+ }
+
+ public function collectObjects( array $wants, array $haves = [] ): array {
+ $objs = [];
+ $seen = [];
+
+ foreach( $wants as $sha ) {
+ $this->collectObjectsRecursive( $sha, $objs, $seen );
+ }
+
+ foreach( $haves as $sha ) {
+ unset($objs[$sha]);
+ }
+
+ return $objs;
+ }
+
+ private function collectObjectsRecursive(
+ string $sha,
+ array &$objs,
+ array &$seen
+ ): void {
+ if( !isset($seen[$sha]) ) {
$seen[$sha] = true;
$data = $this->read( $sha );
Delta 498 lines added, 500 lines removed, 2-line decrease