@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 60 lines 3.02 KB
Raw
1 import * as AzureDevOpsDriver from "@t3tools/source-control-azure-devops/server/driver";
2 import * as BitbucketDriver from "@t3tools/source-control-bitbucket/server/driver";
3 import * as CursorOriginDriver from "@t3tools/source-control-cursor-origin/server/driver";
4 import { builtInSourceControlDefinitions } from "@t3tools/source-control-definitions/registry";
5 import * as ForgejoDriver from "@t3tools/source-control-forgejo/server/driver";
6 import * as GitCafeDriver from "@t3tools/source-control-gitcafe/server/driver";
7 import * as GitHubDriver from "@t3tools/source-control-github/server/driver";
8 import * as GitLabDriver from "@t3tools/source-control-gitlab/server/driver";
9
10 /** In the shared registry's order, which is the order hosts are listed and probed in. */
11 export const BUILT_IN_SOURCE_CONTROL_DRIVER_LIST = [
12 GitHubDriver.driver,
13 CursorOriginDriver.driver,
14 GitLabDriver.driver,
15 GitCafeDriver.driver,
16 ForgejoDriver.driver,
17 BitbucketDriver.driver,
18 AzureDevOpsDriver.driver,
19 ] as const;
20
21 export const sourceControlDefinitions = builtInSourceControlDefinitions;
22
23 /** A host CLI command that merges or closes a change request, such as `gh pr merge`. */
24 export const isChangeRequestCloseCommand = (command: string) =>
25 sourceControlDefinitions.definitions.some(
26 (definition) => definition.changeRequestCloseCommand?.test(command) === true,
27 );
28
29 // A quoted or bare shell word. An unclosed quote, as Codex's `/bin/zsh -lc "gh pr merge 19"`
30 // leaves after the match, runs to the end.
31 const SHELL_WORD = /"((?:\\[\s\S]|[^"\\])*)"?|'([^']*)'?|((?:\\[\s\S]|[^\s"'\\])+)/gu;
32 // A whole word that names one, so a number inside `--subject "Release 20"` does not.
33 const NAMED_CHANGE_REQUEST = /^(?:--?[\w-]+=)?#?(\d+)$|^\S*\/(?:pulls?|merge_requests)\/(\d+)\/?$/u;
34
35 /**
36 * The change request numbers a merge or close command names, or "any" when one names none, as a
37 * bare `gh pr merge` on the checked-out branch or `gh stack merge` does.
38 */
39 export function changeRequestsClosedBy(command: string): ReadonlySet<number> | "any" {
40 const named = new Set<number>();
41 for (const { changeRequestCloseCommand: close } of sourceControlDefinitions.definitions) {
42 if (close === undefined) continue;
43 for (const match of command.matchAll(new RegExp(close.source, `${close.flags}g`))) {
44 // From the match's start, since Azure's match runs through `--id` to `--status`.
45 const args = command.slice(match.index).split(/[;&|\n]/u, 1)[0] ?? "";
46 // Every host's merge and close takes one change request, so any later one is option text.
47 let target: string | undefined;
48 for (const [, double, single, bare] of args.matchAll(SHELL_WORD)) {
49 // A comment ends the command.
50 if (bare !== undefined && /^#(?!\d+$)/u.test(bare)) break;
51 const [, number, url] = NAMED_CHANGE_REQUEST.exec(double ?? single ?? bare ?? "") ?? [];
52 target = number ?? url;
53 if (target !== undefined) break;
54 }
55 if (target === undefined) return "any";
56 named.add(Number(target));
57 }
58 }
59 return named.size === 0 ? "any" : named;
60 }