Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sourceControlDefinitions.ts

65 lines3.2 KB

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