-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace-target-probe.php
More file actions
66 lines (59 loc) · 2.34 KB
/
Copy pathworkspace-target-probe.php
File metadata and controls
66 lines (59 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Killable child probe used by WorkspaceTargetInspector.
*
* @package DataMachineCode\Workspace
*/
if ( 'cli' !== PHP_SAPI || ! isset($argv[1]) ) {
exit(2);
}
$workspace_path = (string) $argv[1];
$filesystem_probe = (string) ( $argv[2] ?? '' );
$git_command = (string) ( $argv[3] ?? 'git' );
fwrite(STDERR, "DMC_BOUNDARY:filesystem:is_dir\n");
if ( '' !== $filesystem_probe ) {
$output = array();
$exit = 0;
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec -- Injected read-only probe remains outer-deadline-controlled.
exec($filesystem_probe . ' ' . escapeshellarg($workspace_path), $output, $exit);
$exists = 0 === $exit && '1' === trim(implode("\n", $output));
} else {
$exists = is_dir($workspace_path);
}
if ( ! $exists ) {
fwrite(STDOUT, json_encode(array( 'exists' => false ), JSON_UNESCAPED_SLASHES));
exit(0);
}
/** @return string|null */
$git_probe = static function ( string $operation, string $args ) use ( $workspace_path, $git_command ): ?string {
fwrite(STDERR, 'DMC_BOUNDARY:git:' . $operation . "\n");
$command = $git_command . ' --no-optional-locks -C ' . escapeshellarg($workspace_path) . ' ' . $args . ' 2>&1';
$output = array();
$exit = 0;
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec -- Isolated, outer-deadline-controlled read-only probe.
exec($command, $output, $exit);
if ( 0 !== $exit ) {
return null;
}
return trim(implode("\n", $output));
};
$branch = $git_probe('branch', 'rev-parse --abbrev-ref HEAD');
$remote = $git_probe('remote', 'config --get ' . escapeshellarg('remote.origin.url'));
$commit = $git_probe('commit', 'log -1 --format=' . escapeshellarg('%h %s'));
$branch_status = $git_probe('status', 'status --porcelain=v1 --branch');
$status_lines = null === $branch_status ? array() : array_filter(array_map('trim', explode("\n", $branch_status)));
$dirty = count(array_filter($status_lines, static fn ( string $line ): bool => ! str_starts_with($line, '## ')));
fwrite(
STDOUT,
json_encode(
array(
'exists' => true,
'branch' => '' !== (string) $branch ? $branch : null,
'remote' => '' !== (string) $remote ? $remote : null,
'commit' => '' !== (string) $commit ? $commit : null,
'dirty' => $dirty,
'branch_status' => $branch_status,
),
JSON_UNESCAPED_SLASHES
)
);