/** * Constructor * * @param GitPHP_Project $project project * @param GitPHP_HeadListLoadStrategy_Interface $strategy load strategy */ public function __construct($project, GitPHP_HeadListLoadStrategy_Interface $strategy) { parent::__construct($project); if (!$strategy) { throw new Exception('Head list load strategy is required'); } $this->SetStrategy($strategy); }
/** * Loads the ref list for a ref type * * @param GitPHP_RefList $refList ref list * @param string $type ref type * @return array array of refs */ protected function GetRefs(GitPHP_RefList $refList, $type) { if (!$refList) { return; } if (empty($type)) { return; } if (GitPHP_DebugLog::GetInstance()->GetEnabled()) { $autotimer = new GitPHP_DebugAutoLog(); } $refs = array(); $prefix = 'refs/' . $type; $fullPath = $refList->GetProject()->GetPath() . '/' . $prefix; $fullPathLen = strlen($fullPath) + 1; /* loose files */ $refFiles = GitPHP_Util::ListDir($fullPath); for ($i = 0; $i < count($refFiles); ++$i) { $ref = substr($refFiles[$i], $fullPathLen); if (empty($ref) || isset($refs[$ref])) { continue; } $hash = trim(file_get_contents($refFiles[$i])); if (preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) { $refs[$ref] = $hash; } } /* packed refs */ if (file_exists($refList->GetProject()->GetPath() . '/packed-refs')) { $packedRefs = explode("\n", file_get_contents($refList->GetProject()->GetPath() . '/packed-refs')); foreach ($packedRefs as $ref) { if (preg_match('/^([0-9A-Fa-f]{40}) refs\\/' . $type . '\\/(.+)$/', $ref, $regs)) { if (!isset($refs[$regs[2]])) { $refs[$regs[2]] = $regs[1]; } } } } return $refs; }
/** * Get refs in a specific order * * @param GitPHP_RefList $refList ref list * @param string $type type of ref * @param string $order order to use * @param int $count limit the number of results * @param int $skip skip a number of results * @return array array of refs */ protected function GetOrderedRefs($refList, $type, $order, $count = 0, $skip = 0) { if (!$refList) { return; } if (empty($type) || empty($order)) { return null; } $args = array(); $args[] = '--sort=' . $order; $args[] = '--format="%(refname)"'; if ($count > 0) { if ($skip > 0) { $args[] = '--count=' . ($count + $skip); } else { $args[] = '--count=' . $count; } } $args[] = '--'; $args[] = 'refs/' . $type; $ret = $this->exe->Execute($refList->GetProject()->GetPath(), GIT_FOR_EACH_REF, $args); $lines = explode("\n", $ret); $prefix = 'refs/' . $type . '/'; $prefixLen = strlen($prefix); $refs = array(); foreach ($lines as $ref) { $ref = substr($ref, $prefixLen); if (!empty($ref)) { $refs[] = $ref; } } if ($skip > 0) { $refs = array_slice($refs, $skip); } return $refs; }