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

sourceControlDefinitions.ts

71 lines3.7 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. A closed one right after `=` stays in that word and is
36// unquoted before matching, so `--id="19"` names 19 and `--subject="20"` names none, while the
37// wrapper's closing quote in `-lc "gh pr merge 19" > "log"` still ends the word `19`.
38const SHELL_WORD =
39 /"((?:\\[\s\S]|[^"\\])*)"?|'([^']*)'?|((?:\\[\s\S]|[^\s"'\\])+(?:(?<==)(?:"(?:\\[\s\S]|[^"\\])*"|'[^']*'))?)/gu;
40// A whole word that names one, so a number inside `--subject "Release 20"` does not. Of the
41// hosts' close commands only Azure's selects one with a flag, so `--subject=2026` and
42// `--body=<pull request URL>` are option text.
43const NAMED_CHANGE_REQUEST = /^(?:--id=)?#?(\d+)$|^(?!-)\S*\/(?:pulls?|merge_requests)\/(\d+)\/?$/u;
44​
45/**
46 * The change request numbers a merge or close command names, or "any" when one names none, as a
47 * bare `gh pr merge` on the checked-out branch or `gh stack merge` does.
48 */
49export function changeRequestsClosedBy(command: string): ReadonlySet<number> | "any" {
50 const named = new Set<number>();
51 for (const { changeRequestCloseCommand: close } of sourceControlDefinitions.definitions) {
52 if (close === undefined) continue;
53 for (const match of command.matchAll(new RegExp(close.source, `${close.flags}g`))) {
54 // From the match's start, since Azure's match runs through `--id` to `--status`.
55 const args = command.slice(match.index).split(/[;&|\n]/u, 1)[0] ?? "";
56 // Every host's merge and close takes one change request, so any later one is option text.
57 let target: string | undefined;
58 for (const [, double, single, bare] of args.matchAll(SHELL_WORD)) {
59 // A comment ends the command.
60 if (bare !== undefined && /^#(?!\d+$)/u.test(bare)) break;
61 const word = double ?? single ?? bare?.replace(/\\([\s\S])|["']/gu, "$1") ?? "";
62 const [, number, url] = NAMED_CHANGE_REQUEST.exec(word) ?? [];
63 target = number ?? url;
64 if (target !== undefined) break;
65 }
66 if (target === undefined) return "any";
67 named.add(Number(target));
68 }
69 }
70 return named.size === 0 ? "any" : named;