/** * @brief Fetch an object in its binary representation by name. * * Throws an exception if the object cannot be found. * * @param $object_name (string) name of the object (binary SHA1) * @returns (array) an array consisting of the object type (int) and the * binary representation of the object (string) */ public function getRawObject($object_name) { static $cache = array(); /* FIXME allow limiting the cache to a certain size */ if (isset($cache[$object_name])) { return $cache[$object_name]; } $sha1 = sha1_hex($object_name); $path = sprintf('%s/objects/%s/%s', $this->dir, substr($sha1, 0, 2), substr($sha1, 2)); if (file_exists($path)) { list($hdr, $object_data) = explode("", gzuncompress(file_get_contents($path)), 2); sscanf($hdr, "%s %d", $type, $object_size); $object_type = Git::getTypeID($type); $r = array($object_type, $object_data); } else { if ($x = $this->findPackedObject($object_name)) { list($pack_name, $object_offset) = $x; $pack = fopen(sprintf('%s/objects/pack/pack-%s.pack', $this->dir, sha1_hex($pack_name)), 'rb'); flock($pack, LOCK_SH); /* check magic and version */ $magic = fread($pack, 4); $version = Binary::fuint32($pack); if ($magic != 'PACK' || $version != 2) { throw new Exception('unsupported pack format'); } $r = $this->unpackObject($pack, $object_offset); fclose($pack); } } // Try alternate repositories. foreach ($this->alternates as $alternate_dir) { try { $alternate_git = new Git($alternate_dir); $r = $alternate_git->getRawObject($object_name); break; } catch (Exception $e) { } } if (!isset($r)) { throw new Exception(sprintf('object not found: %s', sha1_hex($object_name))); } $cache[$object_name] = $r; return $r; }
/** * @brief Fetch an object in its binary representation by name. * * Throws an exception if the object cannot be found. * * @param $objectName (string) name of the object (binary SHA1) * @returns (array) an array consisting of the object type (int) and the * binary representation of the object (string) */ protected function getRawObject($objectName) { static $cache = array(); /* FIXME allow limiting the cache to a certain size */ if (isset($cache[$objectName])) { return $cache[$objectName]; } $sha1 = Git::sha1Hex($objectName); $path = sprintf('%s/objects/%s/%s', $this->dir, substr($sha1, 0, 2), substr($sha1, 2)); if (file_exists($path)) { list($hdr, $objectData) = explode("", gzuncompress(file_get_contents($path)), 2); sscanf($hdr, "%s %d", $type, $objectSize); $objectType = Git::getTypeID($type); $result = array($objectType, $objectData); } elseif ($packedObject = $this->findPackedObject($objectName)) { list($packName, $objectOffset) = $packedObject; $pack = fopen(sprintf('%s/objects/pack/pack-%s.pack', $this->dir, sha1Hex($packName)), 'rb'); flock($pack, LOCK_SH); /* check magic and version */ $magic = fread($pack, 4); $version = Binary::fuint32($pack); if ($magic != 'PACK' || $version != 2) { throw new Exception('unsupported pack format'); } $result = $this->unpackObject($pack, $objectOffset); fclose($pack); } else { throw new Exception(sprintf('object not found: %s', sha1Hex($objectName))); } $cache[$objectName] = $result; return $result; }
/** * @brief Fetch an object in its binary representation by name. * * Throws an exception if the object cannot be found. * * @param $object_name (string) name of the object (binary SHA1) * @return (array) an array consisting of the object type name (string) and the * binary representation of the object (string) */ public function getRawObject(SHA $sha) { static $cache = array(); /* FIXME allow limiting the cache to a certain size */ if (!isset($cache[(string) $sha])) { $path = sprintf('%s/objects/%s/%s', $this->dir, substr($sha->hex(), 0, 2), substr($sha->hex(), 2)); if (file_exists($path)) { list($hdr, $object_data) = explode("", gzuncompress(file_get_contents($path)), 2); sscanf($hdr, "%s %d", $type, $object_size); $cache[(string) $sha] = array($type, $object_data); } else { if ($x = $this->findPackedObject($sha)) { list($pack_sha, $object_offset) = $x; $pack = fopen(sprintf('%s/objects/pack/pack-%s.pack', $this->dir, $pack_sha->hex()), 'rb'); flock($pack, LOCK_SH); /* check magic and version */ $magic = fread($pack, 4); $version = Binary::fuint32($pack); if ($magic != 'PACK' || $version != 2) { throw new Exception('unsupported pack format'); } $cache[(string) $sha] = $this->unpackObject($pack, $object_offset); fclose($pack); } else { throw new Exception(sprintf('object not found: %s', $sha->hex())); } } } return $cache[(string) $sha]; }