protected function parseCommitishPathParam($repository, $commitishPath, $repo)
{
$commitish = null;
$path = null;
$slashPosition = strpos($commitishPath, '/');
if (strlen($commitishPath) >= 40 && ($slashPosition === false || $slashPosition === 40)) {
// We may have a commit hash as our commitish.
$hash = substr($commitishPath, 0, 40);
if ($repository->hasCommit($hash)) {
$commitish = $hash;
}
}
if ($commitish === null) {
$branches = $repository->getBranches();
$tags = $repository->getTags();
if ($tags !== null && count($tags) > 0) {
$branches = array_merge($branches, $tags);
}
$matchedBranch = null;
$matchedBranchLength = 0;
foreach ($branches as $branch) {
if (strpos($commitishPath, $branch) === 0 && strlen($branch) > $matchedBranchLength) {
$matchedBranch = $branch;
$matchedBranchLength = strlen($matchedBranch);
}
}
if ($matchedBranch !== null) {
$commitish = $matchedBranch;
} else {
// We may have partial commit hash as our commitish.
$hash = $slashPosition === false ? $commitishPath : substr($commitishPath, 0, $slashPosition);
if ($repository->hasCommit($hash)) {
$commit = $repository->getCommit($hash);
$commitish = $commit->getHash();
} else {
throw new \Exception('This repository is currently empty. There are no commits.');
}
}
}
$commitishLength = strlen($commitish);
$path = substr($commitishPath, $commitishLength);
if (strpos($path, '/') === 0) {
$path = substr($path, 1);
}
return [$commitish, $path];
}