From d51eee7ed707568f58d305abb750349cb2f27bcc Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 22 Jul 2025 11:16:40 +0200 Subject: add blob route and log parse cache --- index.php | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 82 insertions(+), 11 deletions(-) diff --git a/index.php b/index.php index 9f8b568..d45341a 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,10 @@ + */ function find_projects(string $path, array $projects = []): array { $directories = glob($path . "/{.[!.],}*", GLOB_ONLYDIR | GLOB_BRACE); if (($idx = array_search("$path/.git", $directories)) !== false) { @@ -14,10 +19,23 @@ function find_projects(string $path, array $projects = []): array { return $projects; } -function parse_log(string $path): array|null { + +global $cache_parse_log; +$cache_parse_log = []; + +/** + * parse git log at $path + */ +function parse_log(string $path): array { + global $cache_parse_log; + + if (isset($cache_parse_log[$path])) { + return $cache_parse_log[$path]; + } + $proxy = md5(strval(microtime(true))); $process = proc_open( - "git -C {$path} log " . "--pretty=format:'{%n {$proxy}commit{$proxy}: {$proxy}%H{$proxy},%n {$proxy}author{$proxy}: {$proxy}%aN <%aE>{$proxy},%n {$proxy}date{$proxy}: {$proxy}%ad{$proxy},%n {$proxy}message{$proxy}: {$proxy}%s{$proxy}%n},'", + "git -C \"{$path}\" log " . "--pretty=format:'{%n {$proxy}commit{$proxy}: {$proxy}%H{$proxy},%n {$proxy}author{$proxy}: {$proxy}%aN <%aE>{$proxy},%n {$proxy}date{$proxy}: {$proxy}%ad{$proxy},%n {$proxy}message{$proxy}: {$proxy}%s{$proxy}%n},'", [ ["pipe", "r"], ["pipe", "w"], @@ -32,12 +50,15 @@ function parse_log(string $path): array|null { $json = json_decode($string, true); - return $json; + return $cache_parse_log[$path] = $json ?? []; } +/** + * parse git status at $path + */ function parse_status(string $path): array { $process = proc_open( - "git -C {$path} status --porcelain=v2 --branch", + "git -C \"{$path}\" status --porcelain=v2 --branch", [ ["pipe", "r"], ["pipe", "w"], @@ -117,11 +138,14 @@ function parse_status(string $path): array { return $status; } +/** + * get git file tree at $path for $revision under $tree_base_path + */ function get_file_tree(string $path, string $revision, string $tree_base_path): array { $proxy = md5(strval(microtime(true))); $process = proc_open( - "git -C {$path} ls-tree {$revision} {$tree_base_path}" . " --format='{%n {$proxy}mode{$proxy}: {$proxy}%(objectmode){$proxy},%n {$proxy}type{$proxy}: {$proxy}%(objecttype){$proxy},%n {$proxy}name{$proxy}: {$proxy}%(objectname){$proxy},%n {$proxy}size{$proxy}: {$proxy}%(objectsize){$proxy},%n {$proxy}path{$proxy}: {$proxy}%(path){$proxy}%n},'", + "git -C \"{$path}\" ls-tree {$revision} {$tree_base_path}" . " --format='{%n {$proxy}mode{$proxy}: {$proxy}%(objectmode){$proxy},%n {$proxy}type{$proxy}: {$proxy}%(objecttype){$proxy},%n {$proxy}name{$proxy}: {$proxy}%(objectname){$proxy},%n {$proxy}size{$proxy}: {$proxy}%(objectsize){$proxy},%n {$proxy}path{$proxy}: {$proxy}%(path){$proxy}%n},'", [ ["pipe", "r"], ["pipe", "w"], @@ -131,7 +155,7 @@ function get_file_tree(string $path, string $revision, string $tree_base_path): ); $output = stream_get_contents($pipes[1]); - $string = str_replace(['"', $proxy], ['\\"', '"'], $output); + $string = str_replace(['\\', '"', $proxy], ['\\\\', '\\"', '"'], $output); $string = "[" . rtrim(trim($string), ",") . "]"; $tree = json_decode($string, true); @@ -140,6 +164,9 @@ function get_file_tree(string $path, string $revision, string $tree_base_path): } +/** + * Template root + */ function template_root(string $content, array $meta = []): string { $meta = array_replace_recursive([ @@ -149,9 +176,19 @@ function template_root(string $content, array $meta = []): string ob_start(); ?> + <?php echo $meta["title"]; ?> + @@ -167,6 +204,7 @@ $url = parse_url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); ob_start(); +// Route: / if ($url["path"] == "/") { $projects = find_projects($projects_path); @@ -238,10 +276,10 @@ if ($url["path"] == "/") { } -// /$project/tree/$file_path +// Route: /$project/tree/$file_path elseif (preg_match("#(.*)/tree/?(.*)#", $url["path"], $matches)) { - $project = ltrim($matches[1], "/"); - $file_path = rtrim($matches[2], "/"); + $project = urldecode(ltrim($matches[1], "/")); + $file_path = urldecode(rtrim($matches[2], "/")); // get tree $tree = get_file_tree("$projects_path/$project", "HEAD", "./{$file_path}/"); @@ -260,6 +298,7 @@ elseif (preg_match("#(.*)/tree/?(.*)#", $url["path"], $matches)) { + @@ -267,11 +306,18 @@ elseif (preg_match("#(.*)/tree/?(.*)#", $url["path"], $matches)) { root"; + $crumb_parts = ""; + foreach ($breadcrumbs as $idx => $crumb) { + $crumb_parts .= "/$crumb"; + if ($idx != count($breadcrumbs) - 1) { + echo "/$crumb"; + } else { + echo "/$crumb"; + } + } + + ?> +
+
+
+
Mode File
">">">