From b60a706cd38327471dba489bcc100b32c0955a6f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 23 Oct 2024 13:32:52 +0200 Subject: parse status --- index.php | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) (limited to 'index.php') diff --git a/index.php b/index.php index e186d67..463af36 100644 --- a/index.php +++ b/index.php @@ -37,11 +37,99 @@ function parse_log(): array|null { return $json; } +function parse_status(): array { + $process = proc_open( + "git status --porcelain=v2 --branch", + [ + ["pipe", "r"], + ["pipe", "w"], + ["pipe", "w"], + ], + $pipes + ); + + $output = stream_get_contents($pipes[1]); + + $status = ["files" => []]; + foreach (explode("\n", $output) as $line) { + if (empty($line)) { + continue; + } + + $start = substr($line, 0, 1); + + if ($start == "#") { + if (str_contains($line, "branch.oid")) { + $status["oid"] = substr($line, strlen("# branch.oid ")); + } + else if (str_contains($line, "branch.head")) { + $status["head"] = substr($line, strlen("# branch.head ")); + } + else if (str_contains($line, "branch.upstream")) { + $status["upstream"] = substr($line, strlen("# branch.upstream ")); + } + else if (str_contains($line, "branch.ab")) { + $status["ab"] = substr($line, strlen("# branch.ab ")); + } + } + + else { + $parts = explode(" ", $line); + $file = [ + "filename" => $parts[count($parts) - 1], + "staged" => false, + "unstaged" => false, + "untracked" => false, + "new_file" => false, + "modified" => false, + ]; + + if ($start == "1") { + $track_status = $parts[1]; + if (substr($track_status, 0, 1) == "A") { + $file["staged"] = true; + $file["new_file"] = true; + } + else if (substr($track_status, 0, 1) == "M") { + $file["staged"] = true; + $file["modified"] = true; + } + + if (substr($track_status, 1, 1) == "M") { + $file["unstaged"] = true; + $file["modified"] = true; + } + + $file = array_merge($file, [ + "previous_mode" => $parts[3], + "new_mode" => $parts[4], + "previous_id" => $parts[6], + "new_id" => $parts[7], + ]); + } + + else if ($start == "?") { + $file["untracked"] = true; + } + + $status["files"][] = $file; + } + } + + return $status; +} + foreach ($projects as $project) { chdir($project); echo $project . "
"; + + $status = parse_status(); + foreach ($status["files"] as $file) { + var_dump($file); + } + foreach (parse_log() as $commit) { - echo "$commit[message]
"; + // echo "$commit[message]
"; } } -- cgit v1.2.3