git::cat_file PHP Method

cat_file() static public method

static public cat_file ( $ids )
    static function cat_file($ids)
    {
        $id_to_data = array();
        if (is_string($ids)) {
            $ids = array($ids);
            $id_to_data = false;
        }
        # load
        $out = git::exec("cat-file --batch", implode("\n", $ids));
        $p = 0;
        $numobjects = count($ids);
        # parse
        for ($i = 0; $i < $numobjects; $i++) {
            # <id> SP <type> SP <size> LF
            # <contents> LF
            $hend = strpos($out, "\n", $p);
            $hs = substr($out, $p, $hend - $p);
            $h = explode(' ', $hs);
            if ($h[1] === 'missing') {
                throw new UnexpectedValueException('missing blob ' . $hs);
            }
            $dstart = $hend + 1;
            $size = intval($h[2]);
            $data = substr($out, $dstart, $size);
            if ($id_to_data === false) {
                # a single object was requested (string input)
                return $data;
            }
            $id_to_data[$h[0]] = $data;
            $p = $dstart + $size + 1;
        }
        return $id_to_data;
    }

Usage Example

Example #1
0
 function rawBody($data = null)
 {
     if ($this->_rawBody === null) {
         if ($data === null) {
             if ($this->isWorkVersion()) {
                 $data = file_get_contents(gb::$site_dir . '/' . $this->name);
             } else {
                 $data = git::cat_file($this->id);
             }
         }
         $p = self::findHeaderTerminatorOffset($data);
         if ($p === false) {
             return '';
         }
         $this->_rawBody = substr($data, $p);
     }
     return $this->_rawBody;
 }