88namespace DataMachineCode \Workspace ;
99
1010use DataMachineCode \Support \GitHubRemote ;
11+ use DataMachineCode \Support \GitRunner ;
1112use DataMachineCode \Support \PathSecurity ;
1213use DataMachineCode \Support \ProcessRunner ;
1314
@@ -41,10 +42,6 @@ public function git_status( string $handle ): array|\WP_Error {
4142 return $ policy_attestation ;
4243 }
4344
44- $ branch_result = $ this ->run_git ($ repo_path , 'rev-parse --abbrev-ref HEAD ' );
45- $ remote_result = $ this ->run_git ($ repo_path , 'config --get remote.origin.url ' );
46- $ latest_result = $ this ->run_git ($ repo_path , 'log -1 --format="%h %s" ' );
47-
4845 $ files = array_filter (array_map ('trim ' , explode ("\n" , $ status_result ['output ' ] ?? '' )));
4946
5047 $ response = array (
@@ -53,9 +50,9 @@ public function git_status( string $handle ): array|\WP_Error {
5350 'repo ' => $ parsed ['repo ' ],
5451 'is_worktree ' => $ parsed ['is_worktree ' ],
5552 'path ' => $ repo_path ,
56- 'branch ' => ! is_wp_error ( $ branch_result ) ? trim ( ( string ) $ branch_result [ ' output ' ]) : null ,
57- 'remote ' => ! is_wp_error ( $ remote_result ) ? trim ( ( string ) $ remote_result [ ' output ' ]) : null ,
58- 'commit ' => ! is_wp_error ( $ latest_result ) ? trim ( ( string ) $ latest_result [ ' output ' ]) : null ,
53+ 'branch ' => GitRunner:: current_branch ( $ repo_path ) ,
54+ 'remote ' => GitRunner:: remote_url ( $ repo_path ) ,
55+ 'commit ' => GitRunner:: latest_commit_summary ( $ repo_path ) ,
5956 'dirty ' => count ($ files ),
6057 'files ' => array_values ($ files ),
6158 );
@@ -1709,14 +1706,7 @@ private function get_workspace_git_policies(): array {
17091706 * @return string|null Remote URL or null.
17101707 */
17111708 private function git_get_remote ( string $ repo_path , string $ remote_name = 'origin ' ): ?string {
1712- $ escaped = escapeshellarg ($ repo_path );
1713- $ remote_name = preg_replace ('/[^A-Za-z0-9._-]/ ' , '' , $ remote_name );
1714- if ( '' === $ remote_name ) {
1715- return null ;
1716- }
1717- // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
1718- $ remote = exec (sprintf ('git -C %s config --get %s 2>/dev/null ' , $ escaped , escapeshellarg ('remote. ' . $ remote_name . '.url ' )));
1719- return ( '' !== $ remote ) ? $ remote : null ;
1709+ return GitRunner::remote_url ($ repo_path , $ remote_name );
17201710 }
17211711
17221712 /**
@@ -1726,9 +1716,6 @@ private function git_get_remote( string $repo_path, string $remote_name = 'origi
17261716 * @return string|null Branch name or null.
17271717 */
17281718 private function git_get_branch ( string $ repo_path ): ?string {
1729- $ escaped = escapeshellarg ($ repo_path );
1730- // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec
1731- $ branch = exec (sprintf ('git -C %s rev-parse --abbrev-ref HEAD 2>/dev/null ' , $ escaped ));
1732- return ( '' !== $ branch ) ? $ branch : null ;
1719+ return GitRunner::current_branch ($ repo_path );
17331720 }
17341721}
0 commit comments