|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +if ( ! defined('ABSPATH') ) { |
| 6 | + define('ABSPATH', __DIR__ . '/fixtures/'); |
| 7 | +} |
| 8 | + |
| 9 | +if ( ! class_exists('WP_Error') ) { |
| 10 | + class WP_Error { |
| 11 | + private string $code; |
| 12 | + private string $message; |
| 13 | + private array $data; |
| 14 | + |
| 15 | + public function __construct( string $code = '', string $message = '', array $data = array() ) { |
| 16 | + $this->code = $code; |
| 17 | + $this->message = $message; |
| 18 | + $this->data = $data; |
| 19 | + } |
| 20 | + |
| 21 | + public function get_error_code(): string { |
| 22 | + return $this->code; |
| 23 | + } |
| 24 | + |
| 25 | + public function get_error_message(): string { |
| 26 | + return $this->message; |
| 27 | + } |
| 28 | + |
| 29 | + public function get_error_data(): array { |
| 30 | + return $this->data; |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +if ( ! function_exists('is_wp_error') ) { |
| 36 | + function is_wp_error( mixed $thing ): bool { |
| 37 | + return $thing instanceof WP_Error; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +require_once dirname(__DIR__) . '/inc/Support/CommandSpec.php'; |
| 42 | +require_once dirname(__DIR__) . '/inc/Support/RuntimeCapabilities.php'; |
| 43 | +require_once dirname(__DIR__) . '/inc/Support/ProcessRunner.php'; |
| 44 | +require_once dirname(__DIR__) . '/inc/Support/GitRunner.php'; |
| 45 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceHandle.php'; |
| 46 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceCoreUtilities.php'; |
| 47 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceActiveNoSignalCleanup.php'; |
| 48 | +require_once dirname(__DIR__) . '/inc/Workspace/WorkspaceWorktreeCleanupEngine.php'; |
| 49 | + |
| 50 | +use DataMachineCode\Workspace\WorkspaceActiveNoSignalCleanup; |
| 51 | +use DataMachineCode\Workspace\WorkspaceCoreUtilities; |
| 52 | +use DataMachineCode\Workspace\WorkspaceWorktreeCleanupEngine; |
| 53 | + |
| 54 | +function worktree_cleanup_patch_equivalence_assert_same( mixed $expected, mixed $actual, string $message ): void { |
| 55 | + if ( $expected !== $actual ) { |
| 56 | + throw new RuntimeException(sprintf('%s Expected %s, got %s.', $message, var_export($expected, true), var_export($actual, true))); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function worktree_cleanup_patch_equivalence_run( string $cwd, string $command ): void { |
| 61 | + $descriptor_spec = array( |
| 62 | + 0 => array( 'pipe', 'r' ), |
| 63 | + 1 => array( 'pipe', 'w' ), |
| 64 | + 2 => array( 'pipe', 'w' ), |
| 65 | + ); |
| 66 | + $process = proc_open($command, $descriptor_spec, $pipes, $cwd); |
| 67 | + if ( ! is_resource($process) ) { |
| 68 | + throw new RuntimeException('Failed to start command: ' . $command); |
| 69 | + } |
| 70 | + fclose($pipes[0]); |
| 71 | + $output = stream_get_contents($pipes[1]); |
| 72 | + $error = stream_get_contents($pipes[2]); |
| 73 | + fclose($pipes[1]); |
| 74 | + fclose($pipes[2]); |
| 75 | + $status = proc_close($process); |
| 76 | + if ( 0 !== $status ) { |
| 77 | + throw new RuntimeException(sprintf("Command failed (%d): %s\n%s\n%s", $status, $command, $output, $error)); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +$root = sys_get_temp_dir() . '/dmc-patch-equivalence-' . bin2hex(random_bytes(4)); |
| 82 | +$origin = $root . '/origin.git'; |
| 83 | +$primary = $root . '/primary'; |
| 84 | +$wt = $root . '/feature-worktree'; |
| 85 | + |
| 86 | +mkdir($root, 0700, true); |
| 87 | +worktree_cleanup_patch_equivalence_run($root, 'git init --bare origin.git'); |
| 88 | +worktree_cleanup_patch_equivalence_run($root, 'git clone origin.git primary'); |
| 89 | +worktree_cleanup_patch_equivalence_run($primary, 'git config user.email test@example.com'); |
| 90 | +worktree_cleanup_patch_equivalence_run($primary, 'git config user.name Test'); |
| 91 | +worktree_cleanup_patch_equivalence_run($primary, 'git checkout -b main'); |
| 92 | +file_put_contents($primary . '/recipe.txt', "base\n"); |
| 93 | +worktree_cleanup_patch_equivalence_run($primary, 'git add recipe.txt'); |
| 94 | +worktree_cleanup_patch_equivalence_run($primary, 'git commit -m base'); |
| 95 | +worktree_cleanup_patch_equivalence_run($primary, 'git push -u origin main'); |
| 96 | +worktree_cleanup_patch_equivalence_run($origin, 'git symbolic-ref HEAD refs/heads/main'); |
| 97 | +worktree_cleanup_patch_equivalence_run($primary, 'git worktree add -b feature ../feature-worktree origin/main'); |
| 98 | +worktree_cleanup_patch_equivalence_run($wt, 'git config user.email test@example.com'); |
| 99 | +worktree_cleanup_patch_equivalence_run($wt, 'git config user.name Test'); |
| 100 | +file_put_contents($wt . '/recipe.txt', "base\nsquash-equivalent\n"); |
| 101 | +worktree_cleanup_patch_equivalence_run($wt, 'git add recipe.txt'); |
| 102 | +worktree_cleanup_patch_equivalence_run($wt, 'git commit -m feature-change'); |
| 103 | +file_put_contents($primary . '/recipe.txt', "base\nsquash-equivalent\n"); |
| 104 | +worktree_cleanup_patch_equivalence_run($primary, 'git add recipe.txt'); |
| 105 | +worktree_cleanup_patch_equivalence_run($primary, 'git commit -m squash-feature-change'); |
| 106 | +worktree_cleanup_patch_equivalence_run($primary, 'git push origin main'); |
| 107 | + |
| 108 | +$cleanup = new class { |
| 109 | + use WorkspaceCoreUtilities; |
| 110 | + use WorkspaceActiveNoSignalCleanup; |
| 111 | + use WorkspaceWorktreeCleanupEngine { |
| 112 | + WorkspaceWorktreeCleanupEngine::classify_unpushed_patch_equivalent_to_default as public classifyPatchEquivalent; |
| 113 | + } |
| 114 | + |
| 115 | + protected const CLEANUP_GIT_PROBE_TIMEOUT = 5; |
| 116 | + |
| 117 | + protected function resolve_remote_default_ref( string $primary_path, int $timeout_seconds = 0 ): string|WP_Error|null { |
| 118 | + $result = $this->run_git($primary_path, 'symbolic-ref --quiet refs/remotes/origin/HEAD', $timeout_seconds); |
| 119 | + if ( is_wp_error($result) ) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + $ref = trim( (string) ( $result['output'] ?? '' )); |
| 124 | + return '' === $ref ? null : $ref; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +$fetched = array(); |
| 129 | +$fetch_timeouts = array(); |
| 130 | +$evidence = $cleanup->classifyPatchEquivalent($primary, $wt, 'repo', $fetched, $fetch_timeouts); |
| 131 | + |
| 132 | +worktree_cleanup_patch_equivalence_assert_same(true, is_array($evidence), 'squash-equivalent commit should be promoted with evidence'); |
| 133 | +worktree_cleanup_patch_equivalence_assert_same('equivalent_clean', $evidence['effective_status'], 'git cherry proves patch equivalence'); |
| 134 | +worktree_cleanup_patch_equivalence_assert_same('refs/remotes/origin/main', $evidence['compared_refs']['left'], 'evidence records default ref'); |
| 135 | +worktree_cleanup_patch_equivalence_assert_same('HEAD', $evidence['compared_refs']['right'], 'evidence records worktree ref'); |
| 136 | +worktree_cleanup_patch_equivalence_assert_same(1, $evidence['git_cherry']['equivalent'], 'squash-equivalent commit is counted as equivalent'); |
| 137 | +worktree_cleanup_patch_equivalence_assert_same(0, $evidence['git_cherry']['unmatched'], 'no unmatched commits are promoted'); |
| 138 | +worktree_cleanup_patch_equivalence_assert_same('preserve_local_branch', $evidence['local_branch_handling'], 'cleanup preserves non-contained local branch'); |
| 139 | + |
| 140 | +echo "worktree-cleanup-patch-equivalence: ok\n"; |
0 commit comments