/** * @brief Parses a revision reference or hex sha1 * * @param $branch (string) The ref to look up, defaulting to @em HEAD. * @returns (string) Binary SHA1 of parsed reference. */ public function revParse($ref = 'HEAD') { if (is_valid_sha1($ref)) { return sha1_bin($ref); } if (!($r = $this->getRef("{$ref}"))) { if (!($r = $this->getRef("refs/{$ref}"))) { if (!($r = $this->getRef("refs/tags/{$ref}"))) { if (!($r = $this->getRef("refs/heads/{$ref}"))) { if (!($r = $this->getRef("refs/remotes/{$ref}"))) { if (!($r = $this->getRef("refs/remotes/{$ref}/HEAD"))) { throw new Exception(sprintf('bad reference: %s', $ref)); } } } } } } return $r; }
return; } try { // update references foreach ($refs as $refline) { // trim out everything after first null $nullpos = strpos($refline, ""); if ($nullpos !== false) { $refline = substr($refline, 0, $nullpos); } list($oldhash, $newhash, $uref) = explode(" ", $refline); //check refline for correctness if (is_null($uref)) { throw new Exception("Invalid refline {$refline}"); } if (!is_valid_sha1($oldhash) || !is_valid_sha1($newhash)) { $line = "ng {$uref} invalid sha1 hash\n"; echo sprintf("%04x%s", strlen($line) + 4, $line); continue; } if (substr($uref, 0, 5) !== "refs/" || strpos($uref, "..") !== false) { $line = "ng {$uref} invalid ref\n"; echo sprintf("%04x%s", strlen($line) + 4, $line); continue; } // check the oldhash if ($oldhash !== str_repeat('0', 40)) { $rfile = fopen($repo_dir . $uref, "r+"); flock($rfile, LOCK_EX); $oldhash_check = trim(stream_get_contents($rfile)); if ($oldhash !== $oldhash_check) {
/** * @brief reads a single ref from a file * @param $ref (string) reference to be loaded * @returns (integer) * 0 if reference is bad * 1 if reference sha1 is loaded * 2 if reference is need to be resolved */ protected function read_ref($ref) { // safety: refuse to resolve reference that points to parent directory if (substr($ref, 3) == '../' || strpos($ref, '/../')) { $this->brefs[$ref] = true; return 0; } $data = trim(@file_get_contents(sprintf("%s/%s", $this->dir, $ref))); if (substr($data, 0, 4) == "ref:") { $this->srefs[$ref] = trim(substr($data, 4)); return 2; } if (is_valid_sha1($data)) { $this->refs[$ref] = sha1_bin($data); return 1; } $this->brefs[$ref] = true; //mark reference as bad return 0; }