preg_match('#^/' . preg_quote($name) . '(/|\.git|$)#', $_SERVER['REQUEST_URI']) === 1,
);
if ($repoName === null) {
header('HTTP/1.0 404 Not Found');
echo '404';
die();
}
$repoPath = REPOS_PATH . '/' . $repoName;
$branches = shell_exec('git --git-dir=' . escapeshellarg($repoPath . '/.git') . ' for-each-ref --format="%(refname:short) (%(committerdate:iso))" refs/heads 2>&1');
$filePaths = shell_exec('git --git-dir=' . escapeshellarg($repoPath . '/.git') . ' -c core.quotePath=false ls-tree --full-tree --name-only -r HEAD');
$readme = shell_exec('git --git-dir=' . escapeshellarg($repoPath . '/.git') . ' show HEAD:README.md', );
$filePaths = $filePaths === null ? [] : explode("\n", trim($filePaths));
usort(
$filePaths,
function(string $filePathA, string $filePathB): int {
return str_contains($filePathB, '/') <=> str_contains($filePathA, '/') ?: strcmp($filePathA, $filePathB);
},
);
$branches = $branches === null ? [] : explode("\n", trim($branches));
if (str_starts_with($_SERVER['REQUEST_URI'], '/' . $repoName . '.git')) {
$filePath = str_replace('/' . $repoName . '.git/', '', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (str_starts_with(realpath($repoPath . '/.git/' . $filePath), realpath($repoPath . '/.git')) === false) {
header('HTTP/1.0 400 Bad Request');
exit;
}
if (file_exists($repoPath . '/.git/' . $filePath) === false) {
header('HTTP/1.0 404 Not Found');
echo '404';
exit;
}
readfile($repoPath . '/.git/' . $filePath);
die();
}
if (str_starts_with($_SERVER['REQUEST_URI'], '/' . $repoName . '/file/')) {
$filePath = array_find(
$filePaths,
fn (string $path): bool => $_SERVER['REQUEST_URI'] === '/' . $repoName . '/file/' . $path
);
if ($filePath === null) {
header('HTTP/1.0 404 Not Found');
echo '404';
die();
}
match(pathinfo($filePath, PATHINFO_EXTENSION)) {
'jpg', 'jpeg', => header('Content-Type: image/jpeg'),
'png', => header('Content-Type: image/png'),
'gif' => header('Content-Type: image/gif'),
'svg' => header('Content-Type: image/svg+xml'),
'json' => header('Content-Type: application/json'),
default => header('Content-Type: text/plain'),
};
echo shell_exec('git --git-dir=' . escapeshellarg($repoPath . '/.git') . ' show ' . escapeshellarg('HEAD:' . $filePath));
die();
}
?>
= e($repoName) ?>
= e($repoName) ?>
Branches
Files
| Name |
|
' . e($filePathDirectory) . '/' . e(basename($filePath));
} else {
$label = e($filePath);
}
?>
= $label ?>
|