| | |
| | if( $input === 'HEAD' && file_exists( $headFile ) ) { |
| | - $head = trim( file_get_contents( $headFile ) ); |
| | + $head = trim( file_get_contents( $headFile ) ); |
| | $result = strpos( $head, 'ref: ' ) === 0 |
| | ? $this->resolve( substr( $head, 5 ) ) |
 |
| | } |
| | |
| | - if( empty( $found ) ) { |
| | - $key = array_key_first( $branches ); |
| | - $found = $key |
| | - ? [ 'name' => $key, 'hash' => $branches[$key] ] |
| | - : [ 'name' => '', 'hash' => '' ]; |
| | + if( empty( $found ) && !empty( $branches ) ) { |
| | + $key = array_key_first( $branches ); |
| | + $found = [ 'name' => $key, 'hash' => $branches[$key] ]; |
| | + } elseif( empty( $found ) ) { |
| | + $found = [ 'name' => '', 'hash' => '' ]; |
| | } |
| | |
| | return $found; |
| | } |
| | |
| | public function scanRefs( string $prefix, callable $callback ): void { |
| | $dir = "{$this->repoPath}/$prefix"; |
| | + |
| | + $this->traverseDirectory( $dir, $callback, '' ); |
| | + } |
| | |
| | + private function traverseDirectory( |
| | + string $dir, |
| | + callable $callback, |
| | + string $subPath |
| | + ): void { |
| | if( is_dir( $dir ) ) { |
| | $files = array_diff( scandir( $dir ), ['.', '..'] ); |
| | + |
| | foreach( $files as $file ) { |
| | - $callback( $file, trim( file_get_contents( "$dir/$file" ) ) ); |
| | + $path = "$dir/$file"; |
| | + $name = $subPath === '' ? $file : "$subPath/$file"; |
| | + |
| | + if( is_dir( $path ) ) { |
| | + $this->traverseDirectory( $path, $callback, $name ); |
| | + } elseif( is_file( $path ) ) { |
| | + $sha = trim( file_get_contents( $path ) ); |
| | + |
| | + if( preg_match( '/^[0-9a-f]{40}$/', $sha ) ) { |
| | + $callback( $name, $sha ); |
| | + } |
| | + } |
| | } |
| | } |
 |
| | foreach( $paths as $ref ) { |
| | $path = "{$this->repoPath}/$ref"; |
| | + |
| | if( file_exists( $path ) ) { |
| | $result = trim( file_get_contents( $path ) ); |
| | break; |
| | } |
| | } |
| | |
| | if( $result === '' ) { |
| | $packedPath = "{$this->repoPath}/packed-refs"; |
| | + |
| | if( file_exists( $packedPath ) ) { |
| | $result = $this->findInPackedRefs( $packedPath, $input ); |
| | } |
| | + } |
| | + |
| | + if( !preg_match( '/^[0-9a-f]{40}$/', $result ) ) { |
| | + $result = ''; |
| | } |
| | |
 |
| | if( $line[0] !== '#' && $line[0] !== '^' ) { |
| | $parts = explode( ' ', trim( $line ) ); |
| | + |
| | if( count( $parts ) >= 2 && in_array( $parts[1], $targets ) ) { |
| | $result = $parts[0]; |