diff options
Diffstat (limited to '3rdparty/granite/git/object')
-rw-r--r-- | 3rdparty/granite/git/object/index.php | 210 | ||||
-rw-r--r-- | 3rdparty/granite/git/object/loose.php | 81 | ||||
-rw-r--r-- | 3rdparty/granite/git/object/packed.php | 304 | ||||
-rw-r--r-- | 3rdparty/granite/git/object/raw.php | 153 |
4 files changed, 0 insertions, 748 deletions
diff --git a/3rdparty/granite/git/object/index.php b/3rdparty/granite/git/object/index.php deleted file mode 100644 index 239706d4efd..00000000000 --- a/3rdparty/granite/git/object/index.php +++ /dev/null @@ -1,210 +0,0 @@ -<?php -/** - * Index - provides an 'index' object for packfile indexes - * - * PHP version 5.3 - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT License - * @link http://craig0990.github.com/Granite/ - */ - -namespace Granite\Git\Object; -use \UnexpectedValueException as UnexpectedValueException; - -/** - * Index represents a packfile index - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT License - * @link http://craig0990.github.com/Granite/ - */ -class Index -{ - const INDEX_MAGIC = "\377tOc"; - - /** - * The full path to the packfile index - */ - private $path; - /** - * The offset at which the fanout begins, version 2+ indexes have a 2-byte header - */ - private $offset = 8; - /** - * The size of the SHA-1 entries, version 1 stores 4-byte offsets alongside to - * total 24 bytes, version 2+ stores offsets separately - */ - private $size = 20; - /** - * The version of the index file format, versions 1 and 2 are in use and - * currently supported - */ - private $version; - - /** - * Fetches a raw Git object and parses the result - * - * @param string $path The path to the repository root - * @param string $packname The name of the packfile index to read - */ - public function __construct($path, $packname) - { - $this->path = $path - . 'objects' - . DIRECTORY_SEPARATOR - . 'pack' - . DIRECTORY_SEPARATOR - . 'pack-' . $packname . '.idx'; - - $this->version = $this->_readVersion(); - if ($this->version !== 1 && $this->version !== 2) { - throw new UnexpectedValueException( - "Unsupported index version (version $version)" - ); - } - - if ($this->version == 1) { - $this->offset = 0; // Version 1 index has no header/version - $this->size = 24; // Offsets + SHA-1 ids are stored together - } - } - - /** - * Returns the offset of the object stored in the index - * - * @param string $sha The SHA-1 id of the object being requested - * - * @return int The offset of the object in the packfile - */ - public function find($sha) - { - $index = fopen($this->path, 'rb'); - $offset = false; // Offset for object in packfile not found by default - - // Read the fanout to skip to the start char in the sorted SHA-1 list - list($start, $after) = $this->_readFanout($index, $sha); - - if ($start == $after) { - fclose($index); - return false; // Object is apparently located in a 0-length section - } - - // Seek $offset + 255 4-byte fanout entries and read 256th entry - fseek($index, $this->offset + 4 * 255); - $totalObjects = $this->_uint32($index); - - // Look up the SHA-1 id of the object - // TODO: Binary search - fseek($index, $this->offset + 1024 + $this->size * $start); - for ($i = $start; $i < $after; $i++) { - if ($this->version == 1) { - $offset = $this->_uint32($index); - } - - $name = fread($index, 20); - if ($name == pack('H40', $sha)) { - break; // Found it - } - } - - if ($i == $after) { - fclose($index); - return false; // Scanned entire section, couldn't find it - } - - if ($this->version == 2) { - // Jump to the offset location and read it - fseek($index, 1032 + 24 * $totalObjects + 4 * $i); - $offset = $this->_uint32($index); - if ($offset & 0x80000000) { - // Offset is a 64-bit integer; packfile is larger than 2GB - fclose($index); - throw new UnexpectedValueException( - "Packfile larger than 2GB, currently unsupported" - ); - } - } - - fclose($index); - return $offset; - } - - /** - * Converts a binary string into a 32-bit unsigned integer - * - * @param handle $file Binary string to convert - * - * @return int Integer value - */ - private function _uint32($file) - { - $val = unpack('Nx', fread($file, 4)); - return $val['x']; - } - - /** - * Reads the fanout for a particular SHA-1 id - * - * Largely modified from Glip, with some reference to Grit - largely because I - * can't see how to re-implement this in PHP - * - * @param handle $file File handle to the index file - * @param string $sha The SHA-1 id to search for - * @param int $offset The offset at which the fanout begins - * - * @return array Array containing integer 'start' and - * 'past-the-end' locations - */ - private function _readFanout($file, $sha) - { - $sha = pack('H40', $sha); - fseek($file, $this->offset); - if ($sha{0} == "\00") { - /** - * First character is 0, read first fanout entry to provide - * 'past-the-end' location (since first fanout entry provides start - * point for '1'-prefixed SHA-1 ids) - */ - $start = 0; - fseek($file, $this->offset); // Jump to start of fanout, $offset bytes in - $after = $this->_uint32($file); - } else { - /** - * Take ASCII value of first character, minus one to get the fanout - * position of the offset (minus one because the fanout does not - * contain an entry for "\00"), multiplied by four bytes per entry - */ - fseek($file, $this->offset + (ord($sha{0}) - 1) * 4); - $start = $this->_uint32($file); - $after = $this->_uint32($file); - } - - return array($start, $after); - } - - /** - * Returns the version number of the index file, or 1 if there is no version - * information - * - * @return int - */ - private function _readVersion() - { - $file = fopen($this->path, 'rb'); - $magic = fread($file, 4); - $version = $this->_uint32($file); - - if ($magic !== self::INDEX_MAGIC) { - $version = 1; - } - - fclose($file); - return $version; - } - -} diff --git a/3rdparty/granite/git/object/loose.php b/3rdparty/granite/git/object/loose.php deleted file mode 100644 index 32f894845b9..00000000000 --- a/3rdparty/granite/git/object/loose.php +++ /dev/null @@ -1,81 +0,0 @@ -<?php -/** - * Loose - provides a 'loose object' object - * - * PHP version 5.3 - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ - -namespace Granite\Git\Object; -use \UnexpectedValueException as UnexpectedValueException; - -/** - * Loose represents a loose object in the Git repository - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ -class Loose extends Raw -{ - - /** - * Reads an object from a loose object file based on the SHA-1 id - * - * @param string $path The path to the repository root - * @param string $sha The SHA-1 id of the requested object - * - * @throws UnexpectedValueException If the type is not 'commit', 'tree', - * 'tag' or 'blob' - */ - public function __construct($path, $sha) - { - $this->sha = $sha; - - $loose_path = $path - . 'objects/' - . substr($sha, 0, 2) - . '/' - . substr($sha, 2); - - if (!file_exists($loose_path)) { - throw new InvalidArgumentException("Cannot open loose object file for $sha"); - } - - $raw = gzuncompress(file_get_contents($loose_path)); - $data = explode("\0", $raw, 2); - - $header = $data[0]; - $this->content = $data[1]; - - list($this->type, $this->size) = explode(' ', $header); - - switch ($this->type) { - case 'commit': - $this->type = Raw::OBJ_COMMIT; - break; - case 'tree': - $this->type = Raw::OBJ_TREE; - break; - case 'blob': - $this->type = Raw::OBJ_BLOB; - break; - case 'tag': - $this->type = Raw::OBJ_TAG; - break; - default: - throw new UnexpectedValueException( - "Unexpected type '{$this->type}'" - ); - break; - } - } - -} diff --git a/3rdparty/granite/git/object/packed.php b/3rdparty/granite/git/object/packed.php deleted file mode 100644 index 7e8d663b32e..00000000000 --- a/3rdparty/granite/git/object/packed.php +++ /dev/null @@ -1,304 +0,0 @@ -<?php -/** - * Packed - provides a 'packed object' object - * - * PHP version 5.3 - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ - -namespace Granite\Git\Object; -use \UnexpectedValueException as UnexpectedValueException; - -/** - * Packed represents a packed object in the Git repository - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ -class Packed extends Raw -{ - - /** - * The name of the packfile being read - */ - private $_packfile; - - /** - * Added to the object size to make a 'best-guess' effort at how much compressed - * data to read - should be reimplemented, ideally with streams. - */ - const OBJ_PADDING = 512; - - /** - * Reads the object data from the compressed data at $offset in $packfile - * - * @param string $packfile The path to the packfile - * @param int $offset The offset of the object data - */ - public function __construct($packfile, $offset) - { - $this->_packfile = $packfile; - - list($this->type, $this->size, $this->content) - = $this->_readPackedObject($offset); - } - - /** - * Reads the object data at $this->_offset - * - * @param int $offset Offset of the object header - * - * @return array Containing the type, size and object data - */ - private function _readPackedObject($offset) - { - $file = fopen($this->_packfile, 'rb'); - fseek($file, $offset); - // Read the type and uncompressed size from the object header - list($type, $size) = $this->_readHeader($file, $offset); - $object_offset = ftell($file); - - if ($type == self::OBJ_OFS_DELTA || $type == self::OBJ_REF_DELTA) { - return $this->_unpackDeltified( - $file, $offset, $object_offset, $type, $size - ); - } - - $content = gzuncompress(fread($file, $size + self::OBJ_PADDING), $size); - - return array($type, $size, $content); - } - - /** - * Reads a packed object header, returning the type and the size. For more - * detailed information, refer to the @see tag. - * - * From the @see tag: "Each byte is really 7 bits of data, with the first bit - * being used to say if that hunk is the last one or not before the data starts. - * If the first bit is a 1, you will read another byte, otherwise the data starts - * next. The first 3 bits in the first byte specifies the type of data..." - * - * @param handle $file File handle to read - * @param int $offset Offset of the object header - * - * @return array Containing the type and the size - * @see http://book.git-scm.com/7_the_packfile.html - */ - private function _readHeader($file, $offset) - { - // Read the object header byte-by-byte - fseek($file, $offset); - $byte = ord(fgetc($file)); - /** - * Bit-shift right by four, then ignore the first bit with a bitwise AND - * This gives us the object type in binary: - * 001 commit self::OBJ_COMMIT - * 010 tree self::OBJ_TREE - * 011 blob self::OBJ_BLOB - * 100 tag self::OBJ_TAG - * 110 offset delta self::OBJ_OFS_DELTA - * 111 ref delta self::OBJ_REF_DELTA - * - * (000 is undefined, 101 is not currently in use) - * See http://book.git-scm.com/7_the_packfile.html for details - */ - $type = ($byte >> 4) & 0x07; - - // Read the last four bits of the first byte, used to find the size - $size = $byte & 0x0F; - - /** - * $shift initially set to four, since we use the last four bits of the first - * byte - * - * $byte & 0x80 checks the initial bit is set to 1 (i.e. keep reading data) - * - * Finally, $shift is incremented by seven for each consecutive byte (because - * we ignore the initial bit) - */ - for ($shift = 4; $byte & 0x80; $shift += 7) { - $byte = ord(fgetc($file)); - /** - * The size is ANDed against 0x7F to strip the initial bit, then - * bitshifted by left $shift (4 or 7, depending on whether it's the - * initial byte) and ORed against the existing binary $size. This - * continuously increments the $size variable. - */ - $size |= (($byte & 0x7F) << $shift); - } - - return array($type, $size); - } - - /** - * Unpacks a deltified object located at $offset in $file - * - * @param handle $file File handle to read - * @param int $offset Offset of the object data - * @param int $object_offset Offset of the object data, past the header - * @param int $type The object type, either OBJ_REF_DELTA - or OBJ_OFS_DELTA - * @param int $size The expected size of the uncompressed data - * - * @return array Containing the type, size and object data - */ - private function _unpackDeltified($file, $offset, $object_offset, $type, $size) - { - fseek($file, $object_offset); - - if ($type == self::OBJ_REF_DELTA) { - - $base_sha = bin2hex(fread($file, 20)); - - $path = substr($this->_packfile, 0, strpos($this->_packfile, '.git')+5); - $base = Raw::factory($path, $base_sha); - $type = $base->type(); - $base = $base->content(); - - $delta = gzuncompress( - fread($file, $size + self::OBJ_PADDING), $size - ); - - $content = $this->_applyDelta($base, $delta); - - } elseif ($type == self::OBJ_OFS_DELTA) { - - // 20 = maximum varint size according to Glip - $data = fread($file, $size + self::OBJ_PADDING + 20); - - list($base_offset, $length) = $this->_bigEndianNumber($data); - - $delta = gzuncompress(substr($data, $length), $size); - unset($data); - - $base_offset = $offset - $base_offset; - list($type, $size, $base) = $this->_readPackedObject($base_offset); - - $content = $this->_applyDelta($base, $delta); - - } else { - throw new UnexpectedValueException( - "Unknown type $type for deltified object" - ); - } - - return array($type, strlen($content), $content); - } - - /** - * Applies the $delta byte-sequence to $base and returns the - * resultant binary string. - * - * This code is modified from Grit (see below), the Ruby - * implementation used for GitHub under an MIT license. - * - * @param string $base The base string for the delta to be applied to - * @param string $delta The delta string to apply - * - * @return string The patched binary string - * @see - * https://github.com/mojombo/grit/blob/master/lib/grit/git-ruby/internal/pack.rb - */ - private function _applyDelta($base, $delta) - { - $pos = 0; - $src_size = $this->_varint($delta, $pos); - $dst_size = $this->_varint($delta, $pos); - - if ($src_size !== strlen($base)) { - throw new UnexpectedValueException( - 'Expected base delta size ' . strlen($base) . ' does not match the expected ' - . "value $src_size" - ); - } - - $dest = ""; - while ($pos < strlen($delta)) { - $byte = ord($delta{$pos++}); - - if ($byte & 0x80) { - /* copy a part of $base */ - $offset = 0; - if ($byte & 0x01) $offset = ord($delta{$pos++}); - if ($byte & 0x02) $offset |= ord($delta{$pos++}) << 8; - if ($byte & 0x04) $offset |= ord($delta{$pos++}) << 16; - if ($byte & 0x08) $offset |= ord($delta{$pos++}) << 24; - $length = 0; - if ($byte & 0x10) $length = ord($delta{$pos++}); - if ($byte & 0x20) $length |= ord($delta{$pos++}) << 8; - if ($byte & 0x40) $length |= ord($delta{$pos++}) << 16; - if ($length == 0) $length = 0x10000; - $dest .= substr($base, $offset, $length); - } else { - /* take the next $byte bytes as they are */ - $dest .= substr($delta, $pos, $byte); - $pos += $byte; - } - } - - if (strlen($dest) !== $dst_size) { - throw new UnexpectedValueException( - "Deltified string expected to be $dst_size bytes, but actually " - . strlen($dest) . ' bytes' - ); - } - - return $dest; - } - - /** - * Parse a Git varint (variable-length integer). Used in the `_applyDelta()` - * method to read the delta header. - * - * @param string $string The string to parse - * @param int &$pos The position in the string to read from - * - * @return int The integer value - */ - private function _varint($string, &$pos = 0) - { - $varint = 0; - $bitmask = 0x80; - for ($i = 0; $bitmask & 0x80; $i += 7) { - $bitmask = ord($string{$pos++}); - $varint |= (($bitmask & 0x7F) << $i); - } - return $varint; - } - - /** - * Decodes a big endian modified base 128 number (refer to @see tag); this only - * appears to be used in one place, the offset delta in packfiles. The offset - * is the number of bytes to seek back from the start of the delta object to find - * the base object. - * - * This code has been implemented using the C code given in the @see tag below. - * - * @param string &$data The data to read from and decode the number - * - * @return Array Containing the base offset (number of bytes to seek back) and - * the length to use when reading the delta - * @see http://git.rsbx.net/Documents/Git_Data_Formats.txt - */ - private function _bigEndianNumber(&$data) - { - $i = 0; - $byte = ord($data{$i++}); - $number = $byte & 0x7F; - while ($byte & 0x80) { - $byte = ord($data{$i++}); - $number = (($number + 1) << 7) | ($byte & 0x7F); - } - - return array($number, $i); - } - -} diff --git a/3rdparty/granite/git/object/raw.php b/3rdparty/granite/git/object/raw.php deleted file mode 100644 index 56f363c37b2..00000000000 --- a/3rdparty/granite/git/object/raw.php +++ /dev/null @@ -1,153 +0,0 @@ -<?php -/** - * Raw - provides a raw Git object - * - * PHP version 5.3 - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ - -namespace Granite\Git\Object; -use \InvalidArgumentException as InvalidArgumentException; - -/** - * Raw represents a raw Git object, using Index to locate - * packed objects. - * - * @category Git - * @package Granite - * @author Craig Roberts <craig0990@googlemail.com> - * @license http://www.opensource.org/licenses/mit-license.php MIT Expat License - * @link http://craig0990.github.com/Granite/ - */ -class Raw -{ - /** - * Integer values for Git objects - * @see http://book.git-scm.com/7_the_packfile.html - */ - const OBJ_COMMIT = 1; - const OBJ_TREE = 2; - const OBJ_BLOB = 3; - const OBJ_TAG = 4; - const OBJ_OFS_DELTA = 6; - const OBJ_REF_DELTA = 7; - - /** - * The SHA-1 id of the requested object - */ - protected $sha; - /** - * The type of the requested object (see class constants) - */ - protected $type; - /** - * The binary string content of the requested object - */ - protected $content; - - /** - * Returns an instance of a raw Git object - * - * @param string $path The path to the repository root - * @param string $sha The SHA-1 id of the requested object - * - * @return Packed|Loose - */ - public static function factory($path, $sha) - { - $loose_path = $path - . 'objects/' - . substr($sha, 0, 2) - . '/' - . substr($sha, 2); - if (file_exists($loose_path)) { - return new Loose($path, $sha); - } else { - return self::_findPackedObject($path, $sha); - } - } - - /** - * Returns the raw content of the Git object requested - * - * @return string Raw object content - */ - public function content() - { - return $this->content; - } - - /** - * Returns the size of the Git object - * - * @return int The size of the object in bytes - */ - public function size() - { - return strlen($this->content); - } - - /** - * Returns the type of the object as either commit, tag, blob or tree - * - * @return string The object type - */ - public function type() - { - return $this->type; - } - - /** - * Searches a packfile for the SHA id and reads the object from the packfile - * - * @param string $path The path to the repository - * @param string $sha The SHA-1 id of the object being requested - * - * @throws \InvalidArgumentException - * @return array An array containing the type, size and object data - */ - private static function _findPackedObject($path, $sha) - { - $packfiles = glob( - $path - . 'objects' - . DIRECTORY_SEPARATOR - . 'pack' - . DIRECTORY_SEPARATOR - . 'pack-*.pack' - ); - - $offset = false; - foreach ($packfiles as $packfile) { - $packname = substr(basename($packfile, '.pack'), 5); - $idx = new Index($path, $packname); - $offset = $idx->find($sha); - - if ($offset !== false) { - break; // Found it - } - } - - if ($offset == false) { - throw new InvalidArgumentException("Could not find packed object $sha"); - } - - $packname = $path - . 'objects' - . DIRECTORY_SEPARATOR - . 'pack' - . DIRECTORY_SEPARATOR - . 'pack-' . $packname . '.pack'; - $object = new Packed($packname, $offset); - - return $object; - } - -} - -?> |