From 47d358db849d509e8ad4936d6a9fd5abec1743bb Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 7 Jun 2026 12:39:33 -0400 Subject: [PATCH] refactor: share git cherry count parsing --- inc/Workspace/Workspace.php | 54 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/inc/Workspace/Workspace.php b/inc/Workspace/Workspace.php index 490f105..daca633 100644 --- a/inc/Workspace/Workspace.php +++ b/inc/Workspace/Workspace.php @@ -2899,18 +2899,8 @@ private function build_clean_upstream_equivalence_evidence( string $primary_path $cherry = $this->run_git($wt_path, sprintf('cherry %s HEAD', escapeshellarg($default_ref)), self::CLEANUP_GIT_PROBE_TIMEOUT); if ( ! is_wp_error($cherry) ) { - $lines = array_values(array_filter(array_map('trim', explode("\n", (string) ( $cherry['output'] ?? '' ))))); - foreach ( $lines as $line ) { - if ( str_starts_with($line, '-') ) { - ++$evidence['git_cherry']['equivalent']; - } elseif ( str_starts_with($line, '+') ) { - ++$evidence['git_cherry']['unmatched']; - } else { - ++$evidence['git_cherry']['unknown']; - } - } - $evidence['git_cherry']['total'] = count($lines); - if ( 0 < count($lines) && 0 === (int) $evidence['git_cherry']['unmatched'] && 0 === (int) $evidence['git_cherry']['unknown'] ) { + $evidence['git_cherry'] = $this->parse_git_cherry_counts( (string) ( $cherry['output'] ?? '' ) ); + if ( 0 < (int) $evidence['git_cherry']['total'] && 0 === (int) $evidence['git_cherry']['unmatched'] && 0 === (int) $evidence['git_cherry']['unknown'] ) { $evidence['effective_status'] = 'equivalent_clean'; return $evidence; } @@ -3006,16 +2996,7 @@ private function build_dirty_unpushed_upstream_equivalence_evidence( string $pri $cherry = $this->time_worktree_probe($evidence['probe_timings_ms'], 'git_cherry', fn() => $this->run_git($wt_path, sprintf('cherry %s HEAD', escapeshellarg($default_ref)), self::CLEANUP_GIT_PROBE_TIMEOUT)); if ( ! is_wp_error($cherry) ) { - $lines = array_values(array_filter(array_map('trim', explode("\n", (string) ( $cherry['output'] ?? '' ))))); - foreach ( $lines as $line ) { - if ( str_starts_with($line, '-') ) { - ++$evidence['unpushed_cherry']['equivalent']; - } elseif ( str_starts_with($line, '+') ) { - ++$evidence['unpushed_cherry']['unmatched']; - } else { - ++$evidence['unpushed_cherry']['unknown']; - } - } + $evidence['unpushed_cherry'] = $this->parse_git_cherry_counts( (string) ( $cherry['output'] ?? '' ) ); $evidence['unpushed_patch_equivalent'] = 0 === (int) $evidence['unpushed_cherry']['unmatched'] && 0 === (int) $evidence['unpushed_cherry']['unknown']; } @@ -3066,6 +3047,35 @@ private function build_dirty_unpushed_upstream_equivalence_evidence( string $pri return $evidence; } + /** + * Parse `git cherry` output into equivalent/unmatched/unknown counters. + * + * @param string $output Raw `git cherry` output. + * @return array + */ + private function parse_git_cherry_counts( string $output ): array { + $counts = array( + 'equivalent' => 0, + 'unmatched' => 0, + 'unknown' => 0, + 'total' => 0, + ); + + $lines = array_values(array_filter(array_map('trim', explode("\n", $output)))); + foreach ( $lines as $line ) { + if ( str_starts_with($line, '-') ) { + ++$counts['equivalent']; + } elseif ( str_starts_with($line, '+') ) { + ++$counts['unmatched']; + } else { + ++$counts['unknown']; + } + } + $counts['total'] = count($lines); + + return $counts; + } + /** * Classify dirty paths against the remote default branch with batched git probes. *