Skip to content

Commit 8c844b9

Browse files
authored
Merge pull request #596 from Extra-Chill/refactor/cherry-count-helper
Refactor git cherry count parsing
2 parents 96f3a63 + 47d358d commit 8c844b9

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

inc/Workspace/Workspace.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,18 +2899,8 @@ private function build_clean_upstream_equivalence_evidence( string $primary_path
28992899

29002900
$cherry = $this->run_git($wt_path, sprintf('cherry %s HEAD', escapeshellarg($default_ref)), self::CLEANUP_GIT_PROBE_TIMEOUT);
29012901
if ( ! is_wp_error($cherry) ) {
2902-
$lines = array_values(array_filter(array_map('trim', explode("\n", (string) ( $cherry['output'] ?? '' )))));
2903-
foreach ( $lines as $line ) {
2904-
if ( str_starts_with($line, '-') ) {
2905-
++$evidence['git_cherry']['equivalent'];
2906-
} elseif ( str_starts_with($line, '+') ) {
2907-
++$evidence['git_cherry']['unmatched'];
2908-
} else {
2909-
++$evidence['git_cherry']['unknown'];
2910-
}
2911-
}
2912-
$evidence['git_cherry']['total'] = count($lines);
2913-
if ( 0 < count($lines) && 0 === (int) $evidence['git_cherry']['unmatched'] && 0 === (int) $evidence['git_cherry']['unknown'] ) {
2902+
$evidence['git_cherry'] = $this->parse_git_cherry_counts( (string) ( $cherry['output'] ?? '' ) );
2903+
if ( 0 < (int) $evidence['git_cherry']['total'] && 0 === (int) $evidence['git_cherry']['unmatched'] && 0 === (int) $evidence['git_cherry']['unknown'] ) {
29142904
$evidence['effective_status'] = 'equivalent_clean';
29152905
return $evidence;
29162906
}
@@ -3006,16 +2996,7 @@ private function build_dirty_unpushed_upstream_equivalence_evidence( string $pri
30062996

30072997
$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));
30082998
if ( ! is_wp_error($cherry) ) {
3009-
$lines = array_values(array_filter(array_map('trim', explode("\n", (string) ( $cherry['output'] ?? '' )))));
3010-
foreach ( $lines as $line ) {
3011-
if ( str_starts_with($line, '-') ) {
3012-
++$evidence['unpushed_cherry']['equivalent'];
3013-
} elseif ( str_starts_with($line, '+') ) {
3014-
++$evidence['unpushed_cherry']['unmatched'];
3015-
} else {
3016-
++$evidence['unpushed_cherry']['unknown'];
3017-
}
3018-
}
2999+
$evidence['unpushed_cherry'] = $this->parse_git_cherry_counts( (string) ( $cherry['output'] ?? '' ) );
30193000
$evidence['unpushed_patch_equivalent'] = 0 === (int) $evidence['unpushed_cherry']['unmatched'] && 0 === (int) $evidence['unpushed_cherry']['unknown'];
30203001
}
30213002

@@ -3066,6 +3047,35 @@ private function build_dirty_unpushed_upstream_equivalence_evidence( string $pri
30663047
return $evidence;
30673048
}
30683049

3050+
/**
3051+
* Parse `git cherry` output into equivalent/unmatched/unknown counters.
3052+
*
3053+
* @param string $output Raw `git cherry` output.
3054+
* @return array<string,int>
3055+
*/
3056+
private function parse_git_cherry_counts( string $output ): array {
3057+
$counts = array(
3058+
'equivalent' => 0,
3059+
'unmatched' => 0,
3060+
'unknown' => 0,
3061+
'total' => 0,
3062+
);
3063+
3064+
$lines = array_values(array_filter(array_map('trim', explode("\n", $output))));
3065+
foreach ( $lines as $line ) {
3066+
if ( str_starts_with($line, '-') ) {
3067+
++$counts['equivalent'];
3068+
} elseif ( str_starts_with($line, '+') ) {
3069+
++$counts['unmatched'];
3070+
} else {
3071+
++$counts['unknown'];
3072+
}
3073+
}
3074+
$counts['total'] = count($lines);
3075+
3076+
return $counts;
3077+
}
3078+
30693079
/**
30703080
* Classify dirty paths against the remote default branch with batched git probes.
30713081
*

0 commit comments

Comments
 (0)