Dave Jarvis' Repositories

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

Separates high-level concepts out of Git and into new classes

Author Dave Jarvis <email>
Date 2026-02-22 21:59:44 GMT-0800
Commit 46c36533923637d349a283924b7a7230a568cba8
Parent ea52160
git/Git.php
require_once __DIR__ . '/GitRefs.php';
require_once __DIR__ . '/GitPacks.php';
-require_once __DIR__ . '/BufferedReader.php';
-
-class Git {
- private const MAX_READ = 1048576;
-
- private string $repoPath;
- private string $objPath;
- private GitRefs $refs;
- private GitPacks $packs;
-
- public function __construct( string $repoPath ) {
- $this->setRepository( $repoPath );
- }
-
- public function setRepository( string $repoPath ): void {
- $this->repoPath = \rtrim( $repoPath, '/' );
- $this->objPath = $this->repoPath . '/objects';
- $this->refs = new GitRefs( $this->repoPath );
- $this->packs = new GitPacks( $this->objPath );
- }
-
- public function resolve( string $reference ): string {
- return $this->refs->resolve( $reference );
- }
-
- public function getMainBranch(): array {
- return $this->refs->getMainBranch();
- }
-
- public function eachBranch( callable $callback ): void {
- $this->refs->scanRefs(
- 'refs/heads', $callback
- );
- }
-
- public function eachTag( callable $callback ): void {
- $this->refs->scanRefs(
- 'refs/tags',
- function( $name, $sha ) use ( $callback ) {
- $callback(
- $this->parseTagData(
- $name, $sha, $this->read( $sha )
- )
- );
- }
- );
- }
-
- public function walk(
- string $refOrSha,
- callable $callback,
- string $path = ''
- ): void {
- $sha = $this->resolve( $refOrSha );
- $treeSha = $sha !== ''
- ? $this->getTreeSha( $sha )
- : '';
-
- if( $path !== '' && $treeSha !== '' ) {
- $info = $this->resolvePath(
- $treeSha, $path
- );
- $treeSha = $info['isDir'] ? $info['sha'] : '';
- }
-
- if( $treeSha !== '' ) {
- $this->walkTree( $treeSha, $callback );
- }
- }
-
- public function readFile(
- string $ref,
- string $path
- ): File {
- $sha = $this->resolve( $ref );
- $tree = $sha !== ''
- ? $this->getTreeSha( $sha )
- : '';
- $info = $tree !== ''
- ? $this->resolvePath( $tree, $path )
- : [];
-
- return isset( $info['sha'] )
- && !$info['isDir']
- && $info['sha'] !== ''
- ? new File(
- \basename( $path ),
- $info['sha'],
- $info['mode'],
- 0,
- $this->getObjectSize( $info['sha'] ),
- $this->peek( $info['sha'] )
- )
- : new MissingFile();
- }
-
- public function getObjectSize(
- string $sha,
- string $path = ''
- ): int {
- $target = $sha;
- $result = 0;
-
- if( $path !== '' ) {
- $info = $this->resolvePath(
- $this->getTreeSha( $this->resolve( $sha ) ),
- $path
- );
- $target = $info['sha'] ?? '';
- }
-
- if( $target !== '' ) {
- $result = $this->packs->getSize( $target );
-
- if( $result === 0 ) {
- $result = $this->getLooseObjectSize(
- $target
- );
- }
- }
-
- return $result;
- }
-
- 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 );
- }
- }
-
- public function peek(
- string $sha,
- int $length = 255
- ): string {
- $size = $this->packs->getSize( $sha );
-
- return $size === 0
- ? $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 ) {
- $this->slurp(
- $sha,
- function( $chunk ) use ( &$content ) {
- $content .= $chunk;
- }
- );
- }
-
- return $content;
- }
-
- public function history(
- string $ref,
- int $limit,
- callable $callback
- ): void {
- $sha = $this->resolve( $ref );
- $count = 0;
- $done = false;
-
- while( !$done && $sha !== '' && $count < $limit ) {
- $data = $this->read( $sha );
-
- if( $data === '' ) {
- $done = true;
- } else {
- $id = $this->parseIdentity(
- $data, '/^author (.*) <(.*)> (\d+)/m'
- );
- $parentSha = $this->extractPattern(
- $data, '/^parent (.*)$/m', 1
- );
-
- $commit = new Commit(
- $sha,
- $this->extractMessage( $data ),
- $id['name'],
- $id['email'],
- $id['timestamp'],
- $parentSha
- );
-
- if( $callback( $commit ) === false ) {
- $done = true;
- } else {
- $sha = $parentSha;
- $count++;
- }
- }
- }
- }
-
- public function streamRaw( string $subPath ): bool {
- $result = false;
-
- if( \strpos( $subPath, '..' ) === false ) {
- $path = "{$this->repoPath}/$subPath";
-
- if( \is_file( $path ) ) {
- $real = \realpath( $path );
- $repo = \realpath( $this->repoPath );
-
- if(
- $real !== false
- && \strpos( $real, $repo ) === 0
- ) {
- \header(
- 'X-Accel-Redirect: ' . $path
- );
- \header(
- 'Content-Type: application/octet-stream'
- );
- $result = true;
- }
- }
- }
-
- return $result;
- }
-
- 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 generatePackfile(
- array $objs
- ): Generator {
- $entries = $this->buildPackEntries( $objs );
- $ctx = \hash_init( 'sha1' );
- $head = "PACK"
- . \pack( 'N', 2 )
- . \pack( 'N', \count( $objs ) );
-
- \hash_update( $ctx, $head );
- yield $head;
-
- $written = [];
- $outPos = 12;
-
- foreach( $entries as $sha => $entry ) {
- $written[$sha] = $outPos;
- $baseSha = $entry['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;
-
- 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;
-
- foreach(
- $this->streamCompressed( $sha ) as $chunk
- ) {
- \hash_update( $ctx, $chunk );
- $outPos += \strlen( $chunk );
- yield $chunk;
- }
- }
- }
-
- yield \hash_final( $ctx, true );
- }
-
- private function buildPackEntries(
- array $objs
- ): array {
- $entries = [];
- $offToSha = [];
-
- foreach( $objs as $sha => $logicalType ) {
- $meta = $this->packs->getEntryMeta( $sha );
-
- $entries[$sha] = [
- 'logicalType' => $logicalType,
- 'packType' => $meta['type'],
- 'deltaSize' => $meta['size'],
- 'packFile' => $meta['file'],
- 'offset' => $meta['offset'],
- 'baseOffset' => $meta['baseOffset'] ?? 0,
- 'baseSha' => $meta['baseSha'] ?? '',
- ];
-
- if( $meta['file'] !== '' ) {
- $offToSha[$meta['file']][$meta['offset']]
- = $sha;
- }
- }
-
- foreach( $entries as &$e ) {
- if(
- $e['packType'] === 6
- && $e['baseOffset'] > 0
- ) {
- $e['baseSha']
- = $offToSha[$e['packFile']][$e['baseOffset']]
- ?? '';
- }
- }
-
- unset( $e );
-
- \uasort(
- $entries,
- function( array $a, array $b ): int {
- $cmp = $a['packFile'] <=> $b['packFile'];
-
- return $cmp !== 0
- ? $cmp
- : $a['offset'] <=> $b['offset'];
- }
- );
-
- return $entries;
- }
-
- 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;
- }
-
- private function encodeOffsetDelta(
- int $offset
- ): string {
- $buf = \chr( $offset & 0x7F );
- $n = $offset >> 7;
-
- while( $n > 0 ) {
- $n--;
- $buf = \chr( 0x80 | ($n & 0x7F) ) . $buf;
- $n >>= 7;
- }
-
- return $buf;
- }
-
- private function streamCompressed(
- string $sha
- ): Generator {
- $yielded = false;
-
- foreach(
- $this->packs->streamRawCompressed(
- $sha
- ) as $chunk
- ) {
- $yielded = true;
- yield $chunk;
- }
-
- if( !$yielded ) {
- $deflate = \deflate_init(
- \ZLIB_ENCODING_DEFLATE
- );
-
- foreach( $this->slurpChunks( $sha ) as $raw ) {
- $compressed = \deflate_add(
- $deflate, $raw, \ZLIB_NO_FLUSH
- );
-
- if( $compressed !== '' ) {
- yield $compressed;
- }
- }
-
- $final = \deflate_add(
- $deflate, '', \ZLIB_FINISH
- );
-
- if( $final !== '' ) {
- yield $final;
- }
- }
- }
-
- private function slurpChunks(
- string $sha
- ): Generator {
- $path = $this->getLoosePath( $sha );
-
- if( \is_file( $path ) ) {
- foreach(
- $this->streamInflatedObjects(
- $path
- ) as $chunk
- ) {
- if( $chunk['body'] !== '' ) {
- yield $chunk['body'];
- }
- }
- } else {
- $any = false;
-
- foreach(
- $this->packs->streamGenerator(
- $sha
- ) as $chunk
- ) {
- $any = true;
- yield $chunk;
- }
-
- if( !$any ) {
- $data = $this->packs->read( $sha );
-
- if( $data !== '' ) {
- yield $data;
- }
- }
- }
- }
-
- private function streamInflatedObjects(
- string $path,
- int $bufSz = 16384
- ): Generator {
- $reader = new BufferedReader( $path );
- $infl = $reader->isOpen()
- ? \inflate_init( \ZLIB_ENCODING_DEFLATE )
- : false;
-
- if( $reader->isOpen() && $infl !== false ) {
- $found = false;
- $buffer = '';
-
- while( !$reader->eof() ) {
- $chunk = $reader->read( $bufSz );
- $inflated = \inflate_add( $infl, $chunk );
-
- if( $inflated === false ) {
- break;
- }
-
- if( !$found ) {
- $buffer .= $inflated;
- $eos = \strpos( $buffer, "\0" );
-
- if( $eos !== false ) {
- $found = true;
-
- yield [
- 'head' => \substr(
- $buffer, 0, $eos
- ),
- 'body' => \substr(
- $buffer, $eos + 1
- )
- ];
- }
- } elseif( $inflated !== '' ) {
- yield [
- 'head' => '',
- 'body' => $inflated
- ];
- }
- }
- }
- }
-
- 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 {
- $entry = [ 'sha' => '', 'mode' => '' ];
-
- $this->parseTreeData(
- $this->read( $treeSha ),
- function(
- $n, $s, $m
- ) use ( $name, &$entry ) {
- if( $n === $name ) {
- $entry = [
- 'sha' => $s,
- 'mode' => $m
- ];
-
- return false;
- }
- }
- );
-
- return $entry;
- }
-
- private function parseTagData(
- string $name,
- string $sha,
- string $data
- ): Tag {
- $isAnn = \strncmp( $data, 'object ', 7 ) === 0;
- $id = $this->parseIdentity(
- $data,
- $isAnn
- ? '/^tagger (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
- : '/^author (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
- );
-
- return new Tag(
- $name,
- $sha,
- $isAnn
- ? $this->extractPattern(
- $data, '/^object (.*)$/m', 1, $sha
- )
- : $sha,
- $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 ) )
- : '';
- }
-
- private function slurp(
- string $sha,
- callable $callback
- ): void {
- $path = $this->getLoosePath( $sha );
-
- if( \is_file( $path ) ) {
- foreach(
- $this->streamInflatedObjects(
- $path
- ) as $chunk
- ) {
- if( $chunk['body'] !== '' ) {
- $callback( $chunk['body'] );
- }
- }
- } elseif(
- !$this->packs->stream( $sha, $callback )
- ) {
- $data = $this->packs->read( $sha );
-
- if( $data !== '' ) {
- $callback( $data );
- }
- }
- }
-
- private function peekLooseObject(
- string $sha,
- int $length
- ): string {
- $path = $this->getLoosePath( $sha );
- $buf = '';
-
- if( \is_file( $path ) ) {
- foreach(
- $this->streamInflatedObjects(
- $path, 8192
- ) as $chunk
- ) {
- $buf .= $chunk['body'];
-
- if( \strlen( $buf ) >= $length ) {
- break;
- }
- }
- }
-
- return \substr( $buf, 0, $length );
- }
-
- 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->parseTreeData(
- $tree,
- function(
- $n, $s, $m
- ) use ( $callback ) {
- $dir = $m === '40000'
- || $m === '040000';
- $isSub = $m === '160000';
-
- $callback( new File(
- $n,
- $s,
- $m,
- 0,
- $dir || $isSub
- ? 0
- : $this->getObjectSize( $s ),
- $dir || $isSub
- ? ''
- : $this->peek( $s )
- ) );
- }
- );
- }
- }
-
- public function parseTreeData(
- string $data,
- callable $callback
- ): void {
- $pos = 0;
- $len = \strlen( $data );
-
- while( $pos < $len ) {
- $space = \strpos( $data, ' ', $pos );
- $eos = \strpos( $data, "\0", $space );
-
- if(
- $space === false
- || $eos === false
- || $eos + 21 > $len
- ) {
- break;
- }
-
- $mode = \substr(
- $data, $pos, $space - $pos
- );
- $name = \substr(
- $data, $space + 1, $eos - $space - 1
- );
- $sha = \bin2hex(
- \substr( $data, $eos + 1, 20 )
- );
-
- if(
- $callback( $name, $sha, $mode ) === false
- ) {
- break;
- }
-
- $pos = $eos + 21;
- }
- }
-
- private function isTreeData( string $data ): bool {
- $len = \strlen( $data );
- $match = $len >= 25
- && \preg_match(
- '/^(40000|100644|100755|120000|160000) /',
- $data
- );
- $eos = $match
- ? \strpos( $data, "\0" )
- : false;
-
- return $match
- && $eos !== false
- && $eos + 21 <= $len;
- }
-
- private function getLoosePath(
- string $sha
- ): string {
- return "{$this->objPath}/"
- . \substr( $sha, 0, 2 ) . "/"
- . \substr( $sha, 2 );
- }
-
- private function getLooseObjectSize(
- string $sha
- ): int {
- $path = $this->getLoosePath( $sha );
- $size = 0;
-
- if( \is_file( $path ) ) {
- foreach(
- $this->streamInflatedObjects(
- $path
- ) as $chunk
- ) {
- $parts = \explode( ' ', $chunk['head'] );
- $size = isset( $parts[1] )
- ? (int)$parts[1]
- : 0;
- break;
- }
- }
-
- return $size;
- }
-
- public function collectObjects(
- array $wants,
- array $haves = []
- ): array {
- $objs = $this->traverseObjects( $wants );
-
- if( !empty( $haves ) ) {
- foreach(
- $this->traverseObjects(
- $haves
- ) as $sha => $type
- ) {
- unset( $objs[$sha] );
- }
- }
-
- return $objs;
- }
-
- private function traverseObjects(
- array $roots
- ): array {
- $objs = [];
- $queue = [];
-
- foreach( $roots as $sha ) {
- $queue[] = [
- 'sha' => $sha,
- 'type' => 0
- ];
- }
-
- while( !empty( $queue ) ) {
- $item = \array_pop( $queue );
- $sha = $item['sha'];
- $type = $item['type'];
-
- if( !isset( $objs[$sha] ) ) {
- $data = $type !== 3
- ? $this->read( $sha )
- : '';
- $type = $type === 0
- ? $this->getObjectType( $data )
- : $type;
-
- $objs[$sha] = $type;
-
- if( $type === 1 ) {
- if(
- \preg_match(
- '/^tree ([0-9a-f]{40})/m',
- $data,
- $m
- )
- ) {
- $queue[] = [
- 'sha' => $m[1],
- 'type' => 2
- ];
- }
-
- if(
- \preg_match_all(
- '/^parent ([0-9a-f]{40})/m',
- $data,
- $m
- )
- ) {
- foreach( $m[1] as $parentSha ) {
- $queue[] = [
- 'sha' => $parentSha,
- 'type' => 1
- ];
- }
- }
- } elseif( $type === 2 ) {
- $pos = 0;
- $len = \strlen( $data );
-
- while( $pos < $len ) {
- $space = \strpos(
- $data, ' ', $pos
- );
- $eos = \strpos(
- $data, "\0", $space
- );
-
- if(
- $space === false
- || $eos === false
- ) {
- break;
- }
-
- $mode = \substr(
- $data, $pos, $space - $pos
- );
- $hash = \bin2hex(
- \substr( $data, $eos + 1, 20 )
- );
-
- if( $mode !== '160000' ) {
- $queue[] = [
- 'sha' => $hash,
- 'type' => $mode === '40000'
- || $mode === '040000'
- ? 2
- : 3
- ];
- }
-
- $pos = $eos + 21;
- }
+require_once __DIR__ . '/LooseObjects.php';
+require_once __DIR__ . '/PackfileWriter.php';
+
+class Git {
+ private const MAX_READ = 1048576;
+
+ private string $repoPath;
+ private GitRefs $refs;
+ private GitPacks $packs;
+ private LooseObjects $loose;
+ private PackfileWriter $packWriter;
+
+ public function __construct( string $repoPath ) {
+ $this->setRepository( $repoPath );
+ }
+
+ public function setRepository(
+ string $repoPath
+ ): void {
+ $this->repoPath = \rtrim( $repoPath, '/' );
+
+ $objPath = $this->repoPath . '/objects';
+ $this->refs = new GitRefs( $this->repoPath );
+ $this->packs = new GitPacks( $objPath );
+ $this->loose = new LooseObjects( $objPath );
+ $this->packWriter = new PackfileWriter(
+ $this->packs, $this->loose
+ );
+ }
+
+ public function resolve(
+ string $reference
+ ): string {
+ return $this->refs->resolve( $reference );
+ }
+
+ public function getMainBranch(): array {
+ return $this->refs->getMainBranch();
+ }
+
+ public function eachBranch(
+ callable $callback
+ ): void {
+ $this->refs->scanRefs(
+ 'refs/heads', $callback
+ );
+ }
+
+ public function eachTag(
+ callable $callback
+ ): void {
+ $this->refs->scanRefs(
+ 'refs/tags',
+ function( $name, $sha ) use ( $callback ) {
+ $callback(
+ $this->parseTagData(
+ $name, $sha, $this->read( $sha )
+ )
+ );
+ }
+ );
+ }
+
+ public function walk(
+ string $refOrSha,
+ callable $callback,
+ string $path = ''
+ ): void {
+ $sha = $this->resolve( $refOrSha );
+ $treeSha = $sha !== ''
+ ? $this->getTreeSha( $sha )
+ : '';
+
+ if( $path !== '' && $treeSha !== '' ) {
+ $info = $this->resolvePath(
+ $treeSha, $path
+ );
+ $treeSha = $info['isDir'] ? $info['sha'] : '';
+ }
+
+ if( $treeSha !== '' ) {
+ $this->walkTree( $treeSha, $callback );
+ }
+ }
+
+ public function readFile(
+ string $ref,
+ string $path
+ ): File {
+ $sha = $this->resolve( $ref );
+ $tree = $sha !== ''
+ ? $this->getTreeSha( $sha )
+ : '';
+ $info = $tree !== ''
+ ? $this->resolvePath( $tree, $path )
+ : [];
+
+ return isset( $info['sha'] )
+ && !$info['isDir']
+ && $info['sha'] !== ''
+ ? new File(
+ \basename( $path ),
+ $info['sha'],
+ $info['mode'],
+ 0,
+ $this->getObjectSize( $info['sha'] ),
+ $this->peek( $info['sha'] )
+ )
+ : new MissingFile();
+ }
+
+ 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->loose->getSize( $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 );
+ }
+ }
+
+ public function peek(
+ string $sha,
+ int $length = 255
+ ): string {
+ return $this->packs->getSize( $sha ) > 0
+ ? $this->packs->peek( $sha, $length )
+ : $this->loose->peek( $sha, $length );
+ }
+
+ public function read( string $sha ): string {
+ $size = $this->getObjectSize( $sha );
+ $content = '';
+
+ if( $size > 0 && $size <= self::MAX_READ ) {
+ $this->slurp(
+ $sha,
+ function( $chunk ) use ( &$content ) {
+ $content .= $chunk;
+ }
+ );
+ }
+
+ return $content;
+ }
+
+ public function history(
+ string $ref,
+ int $limit,
+ callable $callback
+ ): void {
+ $sha = $this->resolve( $ref );
+ $count = 0;
+ $done = false;
+
+ while(
+ !$done && $sha !== '' && $count < $limit
+ ) {
+ $data = $this->read( $sha );
+
+ if( $data === '' ) {
+ $done = true;
+ } else {
+ $id = $this->parseIdentity(
+ $data, '/^author (.*) <(.*)> (\d+)/m'
+ );
+ $parentSha = $this->extractPattern(
+ $data, '/^parent (.*)$/m', 1
+ );
+
+ $commit = new Commit(
+ $sha,
+ $this->extractMessage( $data ),
+ $id['name'],
+ $id['email'],
+ $id['timestamp'],
+ $parentSha
+ );
+
+ if( $callback( $commit ) === false ) {
+ $done = true;
+ } else {
+ $sha = $parentSha;
+ $count++;
+ }
+ }
+ }
+ }
+
+ public function streamRaw(
+ string $subPath
+ ): bool {
+ $result = false;
+
+ if( \strpos( $subPath, '..' ) === false ) {
+ $path = "{$this->repoPath}/$subPath";
+
+ if( \is_file( $path ) ) {
+ $real = \realpath( $path );
+ $repo = \realpath( $this->repoPath );
+
+ if(
+ $real !== false
+ && \strpos( $real, $repo ) === 0
+ ) {
+ \header(
+ 'X-Accel-Redirect: ' . $path
+ );
+ \header(
+ 'Content-Type: application/octet-stream'
+ );
+ $result = true;
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ 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 generatePackfile(
+ array $objs
+ ): Generator {
+ yield from $this->packWriter->generate(
+ $objs
+ );
+ }
+
+ public function collectObjects(
+ array $wants,
+ array $haves = []
+ ): array {
+ $objs = $this->traverseObjects( $wants );
+
+ if( !empty( $haves ) ) {
+ foreach(
+ $this->traverseObjects(
+ $haves
+ ) as $sha => $type
+ ) {
+ unset( $objs[$sha] );
+ }
+ }
+
+ return $objs;
+ }
+
+ public function parseTreeData(
+ string $data,
+ callable $callback
+ ): void {
+ $pos = 0;
+ $len = \strlen( $data );
+
+ while( $pos < $len ) {
+ $space = \strpos( $data, ' ', $pos );
+ $eos = \strpos( $data, "\0", $space );
+
+ if(
+ $space === false
+ || $eos === false
+ || $eos + 21 > $len
+ ) {
+ break;
+ }
+
+ $mode = \substr(
+ $data, $pos, $space - $pos
+ );
+ $name = \substr(
+ $data, $space + 1, $eos - $space - 1
+ );
+ $sha = \bin2hex(
+ \substr( $data, $eos + 1, 20 )
+ );
+
+ if(
+ $callback( $name, $sha, $mode ) === false
+ ) {
+ break;
+ }
+
+ $pos = $eos + 21;
+ }
+ }
+
+ private function slurp(
+ string $sha,
+ callable $callback
+ ): void {
+ if(
+ !$this->loose->stream( $sha, $callback )
+ && !$this->packs->stream(
+ $sha, $callback
+ )
+ ) {
+ $data = $this->packs->read( $sha );
+
+ if( $data !== '' ) {
+ $callback( $data );
+ }
+ }
+ }
+
+ 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->parseTreeData(
+ $tree,
+ function(
+ $n, $s, $m
+ ) use ( $callback ) {
+ $dir = $m === '40000'
+ || $m === '040000';
+ $isSub = $m === '160000';
+
+ $callback( new File(
+ $n,
+ $s,
+ $m,
+ 0,
+ $dir || $isSub
+ ? 0
+ : $this->getObjectSize( $s ),
+ $dir || $isSub
+ ? ''
+ : $this->peek( $s )
+ ) );
+ }
+ );
+ }
+ }
+
+ private function isTreeData(
+ string $data
+ ): bool {
+ $len = \strlen( $data );
+ $match = $len >= 25
+ && \preg_match(
+ '/^(40000|100644|100755|120000|160000) /',
+ $data
+ );
+ $eos = $match
+ ? \strpos( $data, "\0" )
+ : false;
+
+ return $match
+ && $eos !== false
+ && $eos + 21 <= $len;
+ }
+
+ 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 {
+ $entry = [ 'sha' => '', 'mode' => '' ];
+
+ $this->parseTreeData(
+ $this->read( $treeSha ),
+ function(
+ $n, $s, $m
+ ) use ( $name, &$entry ) {
+ if( $n === $name ) {
+ $entry = [
+ 'sha' => $s,
+ 'mode' => $m
+ ];
+
+ return false;
+ }
+ }
+ );
+
+ return $entry;
+ }
+
+ private function parseTagData(
+ string $name,
+ string $sha,
+ string $data
+ ): Tag {
+ $isAnn = \strncmp(
+ $data, 'object ', 7
+ ) === 0;
+
+ $id = $this->parseIdentity(
+ $data,
+ $isAnn
+ ? '/^tagger (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
+ : '/^author (.*) <(.*)> (\d+) [+\-]\d{4}$/m'
+ );
+
+ return new Tag(
+ $name,
+ $sha,
+ $isAnn
+ ? $this->extractPattern(
+ $data,
+ '/^object (.*)$/m',
+ 1,
+ $sha
+ )
+ : $sha,
+ $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 ) )
+ : '';
+ }
+
+ private function traverseObjects(
+ array $roots
+ ): array {
+ $objs = [];
+ $queue = [];
+
+ foreach( $roots as $sha ) {
+ $queue[] = [
+ 'sha' => $sha,
+ 'type' => 0
+ ];
+ }
+
+ while( !empty( $queue ) ) {
+ $item = \array_pop( $queue );
+ $sha = $item['sha'];
+ $type = $item['type'];
+
+ if( !isset( $objs[$sha] ) ) {
+ $data = $type !== 3
+ ? $this->read( $sha )
+ : '';
+ $type = $type === 0
+ ? $this->getObjectType( $data )
+ : $type;
+
+ $objs[$sha] = $type;
+
+ if( $type === 1 ) {
+ if(
+ \preg_match(
+ '/^tree ([0-9a-f]{40})/m',
+ $data,
+ $m
+ )
+ ) {
+ $queue[] = [
+ 'sha' => $m[1],
+ 'type' => 2
+ ];
+ }
+
+ if(
+ \preg_match_all(
+ '/^parent ([0-9a-f]{40})/m',
+ $data,
+ $m
+ )
+ ) {
+ foreach( $m[1] as $parentSha ) {
+ $queue[] = [
+ 'sha' => $parentSha,
+ 'type' => 1
+ ];
+ }
+ }
+ } elseif( $type === 2 ) {
+ $this->parseTreeData(
+ $data,
+ function(
+ $n, $s, $m
+ ) use ( &$queue ) {
+ if( $m !== '160000' ) {
+ $queue[] = [
+ 'sha' => $s,
+ 'type' => $m === '40000'
+ || $m === '040000'
+ ? 2
+ : 3
+ ];
+ }
+ }
+ );
} elseif( $type === 4 ) {
if(
git/LooseObjects.php
+<?php
+require_once __DIR__ . '/BufferedReader.php';
+
+class LooseObjects {
+ private string $objPath;
+
+ public function __construct( string $objPath ) {
+ $this->objPath = $objPath;
+ }
+
+ public function getSize( string $sha ): int {
+ $path = $this->getPath( $sha );
+ $size = 0;
+
+ if( \is_file( $path ) ) {
+ foreach( $this->streamInflated( $path ) as $chunk ) {
+ $parts = \explode( ' ', $chunk['head'] );
+
+ $size = isset( $parts[1] )
+ ? (int)$parts[1]
+ : 0;
+
+ break;
+ }
+ }
+
+ return $size;
+ }
+
+ public function peek(
+ string $sha,
+ int $length = 255
+ ): string {
+ $path = $this->getPath( $sha );
+ $buf = '';
+
+ if( \is_file( $path ) ) {
+ foreach(
+ $this->streamInflated( $path, 8192 ) as $chunk
+ ) {
+ $buf .= $chunk['body'];
+
+ if( \strlen( $buf ) >= $length ) {
+ break;
+ }
+ }
+ }
+
+ return \substr( $buf, 0, $length );
+ }
+
+ public function stream(
+ string $sha,
+ callable $callback
+ ): bool {
+ $found = false;
+
+ foreach( $this->streamChunks( $sha ) as $chunk ) {
+ $found = true;
+ $callback( $chunk );
+ }
+
+ return $found;
+ }
+
+ public function streamChunks(
+ string $sha
+ ): Generator {
+ $path = $this->getPath( $sha );
+
+ if( \is_file( $path ) ) {
+ foreach(
+ $this->streamInflated( $path ) as $chunk
+ ) {
+ if( $chunk['body'] !== '' ) {
+ yield $chunk['body'];
+ }
+ }
+ }
+ }
+
+ private function getPath( string $sha ): string {
+ return "{$this->objPath}/"
+ . \substr( $sha, 0, 2 ) . "/"
+ . \substr( $sha, 2 );
+ }
+
+ private function streamInflated(
+ string $path,
+ int $bufSz = 16384
+ ): Generator {
+ $reader = new BufferedReader( $path );
+ $infl = $reader->isOpen()
+ ? \inflate_init( \ZLIB_ENCODING_DEFLATE )
+ : false;
+
+ if( $reader->isOpen() && $infl !== false ) {
+ $found = false;
+ $buffer = '';
+
+ while( !$reader->eof() ) {
+ $chunk = $reader->read( $bufSz );
+ $inflated = \inflate_add( $infl, $chunk );
+
+ if( $inflated === false ) {
+ break;
+ }
+
+ if( !$found ) {
+ $buffer .= $inflated;
+ $eos = \strpos( $buffer, "\0" );
+
+ if( $eos !== false ) {
+ $found = true;
+
+ yield [
+ 'head' => \substr(
+ $buffer, 0, $eos
+ ),
+ 'body' => \substr(
+ $buffer, $eos + 1
+ )
+ ];
+ }
+ } elseif( $inflated !== '' ) {
+ yield [
+ 'head' => '',
+ 'body' => $inflated
+ ];
+ }
+ }
+ }
+ }
+}
git/PackfileWriter.php
+<?php
+require_once __DIR__ . '/GitPacks.php';
+require_once __DIR__ . '/LooseObjects.php';
+
+class PackfileWriter {
+ private GitPacks $packs;
+ private LooseObjects $loose;
+
+ public function __construct(
+ GitPacks $packs,
+ LooseObjects $loose
+ ) {
+ $this->packs = $packs;
+ $this->loose = $loose;
+ }
+
+ public function generate( array $objs ): Generator {
+ $entries = $this->buildEntries( $objs );
+ $ctx = \hash_init( 'sha1' );
+ $head = "PACK"
+ . \pack( 'N', 2 )
+ . \pack( 'N', \count( $objs ) );
+
+ \hash_update( $ctx, $head );
+ yield $head;
+
+ $written = [];
+ $outPos = 12;
+
+ foreach( $entries as $sha => $entry ) {
+ $written[$sha] = $outPos;
+ $baseSha = $entry['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;
+
+ 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;
+
+ foreach(
+ $this->streamCompressed(
+ $sha
+ ) as $chunk
+ ) {
+ \hash_update( $ctx, $chunk );
+ $outPos += \strlen( $chunk );
+ yield $chunk;
+ }
+ }
+ }
+
+ yield \hash_final( $ctx, true );
+ }
+
+ private function buildEntries(
+ array $objs
+ ): array {
+ $entries = [];
+ $offToSha = [];
+
+ foreach( $objs as $sha => $logicalType ) {
+ $meta = $this->packs->getEntryMeta( $sha );
+
+ $entries[$sha] = [
+ 'logicalType' => $logicalType,
+ 'packType' => $meta['type'],
+ 'deltaSize' => $meta['size'],
+ 'packFile' => $meta['file'],
+ 'offset' => $meta['offset'],
+ 'baseOffset' => $meta['baseOffset'] ?? 0,
+ 'baseSha' => $meta['baseSha'] ?? '',
+ ];
+
+ if( $meta['file'] !== '' ) {
+ $offToSha[$meta['file']][$meta['offset']]
+ = $sha;
+ }
+ }
+
+ foreach( $entries as &$e ) {
+ if(
+ $e['packType'] === 6
+ && $e['baseOffset'] > 0
+ ) {
+ $e['baseSha']
+ = $offToSha[$e['packFile']][$e['baseOffset']]
+ ?? '';
+ }
+ }
+
+ unset( $e );
+
+ \uasort(
+ $entries,
+ function( array $a, array $b ): int {
+ $cmp = $a['packFile'] <=> $b['packFile'];
+
+ return $cmp !== 0
+ ? $cmp
+ : $a['offset'] <=> $b['offset'];
+ }
+ );
+
+ return $entries;
+ }
+
+ 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;
+ }
+
+ if( !$yielded ) {
+ $deflate = \deflate_init(
+ \ZLIB_ENCODING_DEFLATE
+ );
+
+ foreach(
+ $this->getDecompressedChunks(
+ $sha
+ ) as $raw
+ ) {
+ $compressed = \deflate_add(
+ $deflate, $raw, \ZLIB_NO_FLUSH
+ );
+
+ if( $compressed !== '' ) {
+ yield $compressed;
+ }
+ }
+
+ $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;
+ }
+
+ if( !$any ) {
+ foreach(
+ $this->packs->streamGenerator(
+ $sha
+ ) as $chunk
+ ) {
+ $any = true;
+ yield $chunk;
+ }
+ }
+
+ if( !$any ) {
+ $data = $this->packs->read( $sha );
+
+ if( $data !== '' ) {
+ yield $data;
+ }
+ }
+ }
+
+ 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;
+ }
+
+ private function encodeOffsetDelta(
+ int $offset
+ ): string {
+ $buf = \chr( $offset & 0x7F );
+ $n = $offset >> 7;
+
+ while( $n > 0 ) {
+ $n--;
+ $buf = \chr( 0x80 | ($n & 0x7F) ) . $buf;
+ $n >>= 7;
+ }
+
+ return $buf;
+ }
+}
Delta 1041 lines added, 976 lines removed, 65-line increase