typeclaw 0.48.3 → 0.48.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/scripts/dump-system-prompt.ts +146 -11
- package/src/agent/plugin-tools.ts +7 -0
- package/src/agent/tool-file-safety.ts +14 -4
- package/src/agent/tools/runtime-notice.ts +29 -0
- package/src/bundled-plugins/github-cli-auth/git-command.ts +497 -652
- package/src/bundled-plugins/memory/load-memory.ts +23 -27
- package/src/bundled-plugins/security/index.ts +1 -0
- package/src/bundled-plugins/security/policies/private-surface-read.ts +74 -18
- package/src/channels/router.ts +32 -47
- package/src/cli/hostd.ts +1 -1
- package/src/hostd/kakao-renewal-manager.ts +23 -11
- package/src/plugin/index.ts +1 -0
- package/src/plugin/types.ts +9 -0
- package/src/secrets/kakao-renewal.ts +123 -20
|
@@ -1,23 +1,11 @@
|
|
|
1
|
-
// Plain-`git` analog of analyzeGhCommand.
|
|
2
|
-
//
|
|
3
|
-
// explicit github.com URL -> remote name via `git remote get-url` -> the
|
|
4
|
-
// branch.<cur>.pushRemote/remote.pushDefault/origin fallback chain. The slug
|
|
5
|
-
// feeds the same per-repo mint the `gh` path uses; the token is injected via
|
|
6
|
-
// GIT_ASKPASS env, never into the command string.
|
|
1
|
+
// Plain-`git` analog of analyzeGhCommand. Git names its target indirectly, so
|
|
2
|
+
// this analyzer resolves configured remotes before selecting a per-repo token.
|
|
7
3
|
|
|
8
4
|
export type GitCommandDecision =
|
|
9
5
|
| { kind: 'pass-through' }
|
|
10
6
|
| { kind: 'block'; reason: string }
|
|
11
|
-
// rewrittenCommand replaces the executed command: `cd <dir> && git …` becomes
|
|
12
|
-
// `git -C <dir> …` so the token-bearing command stays a single bare `git`
|
|
13
|
-
// (no sibling process inherits the askpass env).
|
|
14
7
|
| { kind: 'inject'; repoSlug: string; rewrittenCommand?: string }
|
|
15
8
|
|
|
16
|
-
// Returns null when the remote/config is absent or git fails — the analyzer
|
|
17
|
-
// then passes through so git fails honestly rather than us guessing a repo.
|
|
18
|
-
// forPush selects the PUSH url (`git remote get-url --push`), which differs from
|
|
19
|
-
// the fetch url when a remote sets `pushurl`; minting against the fetch url for a
|
|
20
|
-
// push would scope the token to the wrong repo/owner.
|
|
21
9
|
export type GitRemoteResolver = (cwd: string, remote: string, forPush: boolean) => Promise<string | null>
|
|
22
10
|
export type GitConfigResolver = (cwd: string, key: string) => Promise<string | null>
|
|
23
11
|
export type GitBranchResolver = (cwd: string) => Promise<string | null>
|
|
@@ -38,8 +26,7 @@ async function runGit(cwd: string, args: string[]): Promise<string | null> {
|
|
|
38
26
|
stderr: 'ignore',
|
|
39
27
|
env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_OPTIONAL_LOCKS: '0' },
|
|
40
28
|
})
|
|
41
|
-
|
|
42
|
-
if (exitCode !== 0) return null
|
|
29
|
+
if ((await proc.exited) !== 0) return null
|
|
43
30
|
const out = (await new Response(proc.stdout).text()).trim()
|
|
44
31
|
return out === '' ? null : out
|
|
45
32
|
} catch {
|
|
@@ -54,8 +41,6 @@ export const defaultGitResolvers: GitResolvers = {
|
|
|
54
41
|
resolveCurrentBranch: (cwd) => runGit(cwd, ['symbolic-ref', '--short', 'HEAD']),
|
|
55
42
|
}
|
|
56
43
|
|
|
57
|
-
const REMOTE_SUBCOMMANDS = new Set(['push', 'fetch', 'pull', 'clone', 'ls-remote'])
|
|
58
|
-
|
|
59
44
|
const MULTI_OWNER_REASON =
|
|
60
45
|
'This git command targets more than one repository; a single minted GitHub App ' +
|
|
61
46
|
'token is scoped to one repo and cannot authenticate all of them. Split it into ' +
|
|
@@ -67,661 +52,493 @@ const COMPOSITION_REASON =
|
|
|
67
52
|
'`git` command — no `;`, `&&`, `||`, `&`, newlines, pipes, redirections, ' +
|
|
68
53
|
'command/parameter substitution, or subshells (any sibling process would ' +
|
|
69
54
|
'inherit the token). The one accepted prefix is `cd <simple-path> && git …`, ' +
|
|
70
|
-
'which is rewritten to `git -C <path> …`.'
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
'A repo-targeting `git` command that would receive a minted GitHub App token ' +
|
|
74
|
-
'cannot carry a leading environment assignment or `-c`/`--config-env`/' +
|
|
75
|
-
'`--git-dir`/`--work-tree`/`--namespace`/`--exec-path`: those can redirect ' +
|
|
76
|
-
"git's authentication or destination away from the resolved repo (e.g. " +
|
|
77
|
-
'`-c url.https://evil/.insteadOf=…` or `-c core.askPass=…`), which would leak ' +
|
|
78
|
-
'the token. Remove them and re-run, or run without the minted token.'
|
|
79
|
-
|
|
80
|
-
const FETCH_ALL_REASON =
|
|
81
|
-
'`git fetch --all` / `git pull --all` contacts every configured remote, which ' +
|
|
82
|
-
'cannot be enumerated safely here — a minted token scoped to one remote could ' +
|
|
83
|
-
'be sent to another. Fetch a specific remote instead.'
|
|
84
|
-
|
|
85
|
-
// The exec prefix a `clone && <tail>` tail is re-executed under. Every key here
|
|
86
|
-
// MUST mirror the overlay index.ts injects for a token-bearing git (the two
|
|
87
|
-
// secrets TYPECLAW_GIT_TOKEN/GIT_ASKPASS, the operator PATs GH_TOKEN/GITHUB_TOKEN
|
|
88
|
-
// that live in bash env, and the forced GIT_CONFIG_* + GIT_TERMINAL_PROMPT) so
|
|
89
|
-
// the fresh tokenless shell inherits none of them. Absolute `/usr/bin/env` and
|
|
90
|
-
// `/bin/bash` so a PATH-shadowed shim cannot defeat the strip; a missing binary
|
|
91
|
-
// exits non-zero, failing closed. Drift here is a token leak — keep in lockstep
|
|
92
|
-
// with index.ts's git overlay (guarded by a test in index.test.ts).
|
|
55
|
+
'which is rewritten to `git -C <path> …`. Run local Git commands separately, ' +
|
|
56
|
+
'then retry the remote operation as a standalone `git -C <path> …` command.'
|
|
57
|
+
|
|
93
58
|
const TAIL_STRIP_PREFIX =
|
|
94
59
|
'exec /usr/bin/env -u TYPECLAW_GIT_TOKEN -u GIT_ASKPASS -u GH_TOKEN -u GITHUB_TOKEN ' +
|
|
95
60
|
'-u GIT_TERMINAL_PROMPT -u GIT_CONFIG_COUNT -u GIT_CONFIG_KEY_0 -u GIT_CONFIG_VALUE_0 ' +
|
|
96
61
|
'-u GIT_CONFIG_KEY_1 -u GIT_CONFIG_VALUE_1 -u GIT_CONFIG_KEY_2 -u GIT_CONFIG_VALUE_2 ' +
|
|
97
62
|
'-u GIT_CONFIG_KEY_3 -u GIT_CONFIG_VALUE_3 /bin/bash -c'
|
|
98
63
|
|
|
99
|
-
|
|
100
|
-
// askpass token) or expand shell state. `$`/backtick stay active inside double
|
|
101
|
-
// quotes too, so they are screened separately. Mirrors gh-command.ts.
|
|
102
|
-
const SHELL_ACTIVE_METACHARS = new Set(['|', ';', '&', '\n', '\r', '(', ')', '{', '}', '<', '>', '`', '$'])
|
|
64
|
+
type RemoteSubcommand = 'push' | 'fetch' | 'pull' | 'clone' | 'ls-remote'
|
|
103
65
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
66
|
+
type TargetSpec =
|
|
67
|
+
| { kind: 'default-push' }
|
|
68
|
+
| { kind: 'single'; value: string; forPush: boolean }
|
|
69
|
+
| { kind: 'multiple'; values: string[] }
|
|
70
|
+
|
|
71
|
+
type MintableInvocation = {
|
|
72
|
+
subcommand: RemoteSubcommand
|
|
73
|
+
cwd: string
|
|
74
|
+
dashCCount: number
|
|
75
|
+
target: TargetSpec
|
|
109
76
|
}
|
|
110
77
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
78
|
+
type RepoEvidence = {
|
|
79
|
+
slugs: string[]
|
|
80
|
+
complete: boolean
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const EVIDENCE_GLOBAL_VALUE_OPTIONS = new Set([
|
|
84
|
+
'--config-env',
|
|
85
|
+
'--git-dir',
|
|
86
|
+
'--work-tree',
|
|
87
|
+
'--namespace',
|
|
88
|
+
'--exec-path',
|
|
89
|
+
])
|
|
90
|
+
const EVIDENCE_GLOBAL_INLINE_PREFIXES = ['--config-env=', '--git-dir=', '--work-tree=', '--namespace=', '--exec-path=']
|
|
91
|
+
|
|
92
|
+
function noEvidence(complete = true): RepoEvidence {
|
|
93
|
+
return { slugs: [], complete }
|
|
116
94
|
}
|
|
117
95
|
|
|
118
96
|
export async function analyzeGitCommand(
|
|
119
97
|
command: string,
|
|
120
98
|
options: { cwd: string; resolvers: GitResolvers },
|
|
121
99
|
): Promise<GitCommandDecision> {
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
100
|
+
// clone-then-inspect is the established exception: the clone head is rebuilt
|
|
101
|
+
// from a tiny grammar and the tail runs only after exec strips every token var.
|
|
102
|
+
if (!containsEscapeOrComment(command)) {
|
|
103
|
+
const cloneThenInspect = analyzeCloneThenInspect(command)
|
|
104
|
+
if (cloneThenInspect !== null) return cloneThenInspect
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const safeCd = parseSafeCdPrefix(command, options.cwd)
|
|
108
|
+
if (safeCd.kind === 'unsafe') return { kind: 'pass-through' }
|
|
109
|
+
if (safeCd.kind === 'safe') {
|
|
110
|
+
const invocation = parseMintableGitInvocation(safeCd.rest, safeCd.cwd)
|
|
111
|
+
if (invocation !== null) {
|
|
112
|
+
const decision = await decideStandalone(invocation, options.resolvers)
|
|
113
|
+
if (invocation.dashCCount > 0) {
|
|
114
|
+
return decision.kind === 'inject' ? { kind: 'block', reason: COMPOSITION_REASON } : decision
|
|
115
|
+
}
|
|
116
|
+
if (decision.kind !== 'inject') return decision
|
|
117
|
+
return {
|
|
118
|
+
...decision,
|
|
119
|
+
rewrittenCommand: safeCd.rest.replace(/^git\b/, `git -C ${posixSingleQuote(safeCd.cwd)}`),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
134
122
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
// chains must use explicit `git -C` per segment instead.
|
|
139
|
-
const stripped = stripSafeCdPrefix(command)
|
|
140
|
-
if (stripped.unsafe) return { kind: 'pass-through' }
|
|
141
|
-
if (stripped.cdDir !== null) return analyzeSingleCdGit(stripped, options)
|
|
142
|
-
|
|
143
|
-
const cloneThenInspect = analyzeCloneThenInspect(command)
|
|
144
|
-
if (cloneThenInspect !== null) return cloneThenInspect
|
|
145
|
-
|
|
146
|
-
// Split the command into `&&`-joined git segments. null = not a pure
|
|
147
|
-
// git-only `&&` chain (a non-git segment, a forbidden shell operator, a
|
|
148
|
-
// leading env assignment, or no git at all).
|
|
149
|
-
const chain = extractGitInvocationChain(command)
|
|
150
|
-
if (chain === null) {
|
|
151
|
-
// Distinguish "no git here at all" (pass-through) from "git is present but
|
|
152
|
-
// the composition is unsafe" (block). A bare `ls`/`echo` is not our concern;
|
|
153
|
-
// a `git push … | tee` or `git … && curl evil` must be blocked, never run
|
|
154
|
-
// tokenless (which is what silently passing through used to do — the bug).
|
|
155
|
-
return containsGitInvocation(command) ? { kind: 'block', reason: COMPOSITION_REASON } : { kind: 'pass-through' }
|
|
123
|
+
if (!hasShellComposition(safeCd.rest)) return { kind: 'pass-through' }
|
|
124
|
+
const evidence = await discoverCompoundEvidence(safeCd.rest, safeCd.cwd, options.resolvers)
|
|
125
|
+
return evidence.slugs.length > 0 ? { kind: 'block', reason: COMPOSITION_REASON } : { kind: 'pass-through' }
|
|
156
126
|
}
|
|
157
127
|
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
return sub !== undefined && REMOTE_SUBCOMMANDS.has(sub)
|
|
161
|
-
})
|
|
162
|
-
// No segment talks to a remote (e.g. `git status && git log`): nothing to
|
|
163
|
-
// authenticate, so pass through and let git run with no token.
|
|
164
|
-
if (remoteSegments.length === 0) return { kind: 'pass-through' }
|
|
165
|
-
|
|
166
|
-
// Every segment must pass the redirect screen: a token would live in the shared
|
|
167
|
-
// env the whole chain inherits, so a `-c`/env-assignment on ANY git could steer
|
|
168
|
-
// auth or destination.
|
|
169
|
-
for (const seg of chain) {
|
|
170
|
-
if (seg.hasLeadingEnvAssignment || hasDangerousGitConfig(seg.args)) {
|
|
171
|
-
return { kind: 'block', reason: DANGEROUS_CONFIG_REASON }
|
|
172
|
-
}
|
|
173
|
-
const sub = findSubcommand(seg.args)
|
|
174
|
-
if ((sub === 'fetch' || sub === 'pull') && seg.args.includes('--all')) {
|
|
175
|
-
return { kind: 'block', reason: FETCH_ALL_REASON }
|
|
176
|
-
}
|
|
177
|
-
}
|
|
128
|
+
const invocation = parseMintableGitInvocation(command, options.cwd)
|
|
129
|
+
if (invocation !== null) return decideStandalone(invocation, options.resolvers)
|
|
178
130
|
|
|
179
|
-
|
|
180
|
-
for (const seg of remoteSegments) {
|
|
181
|
-
const sub = findSubcommand(seg.args) as string
|
|
182
|
-
const effectiveCwd = resolveEffectiveCwd(options.cwd, seg.args)
|
|
183
|
-
// A resolver that throws is treated as "couldn't resolve" → pass-through, so
|
|
184
|
-
// a git/subprocess failure never crashes the hook or guesses a repo.
|
|
185
|
-
let slugs: string[]
|
|
186
|
-
try {
|
|
187
|
-
slugs = await resolveRepoSlugs(sub, seg.args, effectiveCwd, options.resolvers)
|
|
188
|
-
} catch {
|
|
189
|
-
return { kind: 'pass-through' }
|
|
190
|
-
}
|
|
191
|
-
for (const slug of slugs) repos.add(slug)
|
|
192
|
-
}
|
|
131
|
+
if (!hasShellComposition(command)) return { kind: 'pass-through' }
|
|
193
132
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
133
|
+
const evidence = await discoverCompoundEvidence(command, options.cwd, options.resolvers)
|
|
134
|
+
return evidence.slugs.length > 0 ? { kind: 'block', reason: COMPOSITION_REASON } : { kind: 'pass-through' }
|
|
135
|
+
}
|
|
197
136
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// `cd <path> && git …`, handled by analyzeSingleCdGit (rewritten to one git -C).
|
|
202
|
-
if (chain.length > 1) return { kind: 'block', reason: COMPOSITION_REASON }
|
|
203
|
-
// One minted token is repo-scoped, so more than one distinct repo (e.g.
|
|
204
|
-
// `fetch --multiple origin upstream`) can't authenticate.
|
|
205
|
-
if (repos.size > 1) return { kind: 'block', reason: MULTI_OWNER_REASON }
|
|
137
|
+
async function decideStandalone(invocation: MintableInvocation, resolvers: GitResolvers): Promise<GitCommandDecision> {
|
|
138
|
+
const evidence = await resolveMintableEvidence(invocation, resolvers)
|
|
139
|
+
const repos = new Set(evidence.slugs)
|
|
206
140
|
|
|
141
|
+
// The order is deliberate: partial evidence may justify blocking a compound,
|
|
142
|
+
// but it can never justify minting for a standalone command.
|
|
143
|
+
if (repos.size === 0) return { kind: 'pass-through' }
|
|
144
|
+
if (!evidence.complete) return { kind: 'pass-through' }
|
|
145
|
+
if (repos.size > 1) return { kind: 'block', reason: MULTI_OWNER_REASON }
|
|
207
146
|
return { kind: 'inject', repoSlug: [...repos][0] as string }
|
|
208
147
|
}
|
|
209
148
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
if (segment.hasLeadingEnvAssignment || hasDangerousGitConfig(args)) {
|
|
224
|
-
return { kind: 'block', reason: DANGEROUS_CONFIG_REASON }
|
|
225
|
-
}
|
|
226
|
-
if ((subcommand === 'fetch' || subcommand === 'pull') && args.includes('--all')) {
|
|
227
|
-
return { kind: 'block', reason: FETCH_ALL_REASON }
|
|
228
|
-
}
|
|
229
|
-
const hasDashC = args.includes('-C')
|
|
230
|
-
const effectiveCwd = resolveEffectiveCwd(resolveCwd(options.cwd, stripped.cdDir), args)
|
|
231
|
-
let slugs: string[]
|
|
232
|
-
try {
|
|
233
|
-
slugs = await resolveRepoSlugs(subcommand, args, effectiveCwd, options.resolvers)
|
|
234
|
-
} catch {
|
|
235
|
-
return { kind: 'pass-through' }
|
|
236
|
-
}
|
|
237
|
-
if (slugs.length === 0) return { kind: 'pass-through' }
|
|
238
|
-
if (new Set(slugs).size > 1) return { kind: 'block', reason: MULTI_OWNER_REASON }
|
|
239
|
-
const repoSlug = slugs[0] as string
|
|
240
|
-
// The rewrite cannot stack two `-C` (the git part must not already carry one)
|
|
241
|
-
// and the rest must be a single bare git (no further composition).
|
|
242
|
-
if (containsShellActiveMetachar(stripped.rest) || hasDashC) {
|
|
243
|
-
return { kind: 'block', reason: COMPOSITION_REASON }
|
|
244
|
-
}
|
|
245
|
-
return { kind: 'inject', repoSlug, rewrittenCommand: rewriteCdToDashC(effectiveCwd, stripped.rest) }
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// The `git clone <url> <dir> && <tail>` shape: acquire a repo, then inspect it.
|
|
249
|
-
// A naive `env -u TOKEN <tail>` is UNSOUND — shell substitution in the tail runs
|
|
250
|
-
// in the still-token-bearing parent shell before `env`, and a stripped child can
|
|
251
|
-
// still read the parent's env via /proc/$PPID/environ. So instead the git head
|
|
252
|
-
// runs first (token reaches only the clone), then `exec` REPLACES the token-
|
|
253
|
-
// bearing shell with `/usr/bin/env -u <keys> /bin/bash -c '<tail>'`: exec removes
|
|
254
|
-
// that /proc target, the unset drops every injected key, and the tail rides as an
|
|
255
|
-
// opaque single-quoted argument the fresh tokenless shell alone parses/expands.
|
|
256
|
-
// Scoped to `clone` by DELIBERATE CHOICE, not a limit of the strip boundary
|
|
257
|
-
// (which would neutralize the token just as well after fetch/pull). `clone` names
|
|
258
|
-
// the repo in argv, so minting is unambiguous, and it creates a fresh tree the
|
|
259
|
-
// tail inspects. fetch/pull would resolve the repo through an existing repo's cwd,
|
|
260
|
-
// remotes, and mutable `.git/config` (and `pull` runs repo-configured integration
|
|
261
|
-
// while the token is live) — extra resolution complexity and token-bearing surface
|
|
262
|
-
// for a workflow already expressible as `git -C <repo> fetch` then a separate
|
|
263
|
-
// tokenless inspect. So fetch/pull/push stay single-bare. Returns null when the
|
|
264
|
-
// shape does not match, so the caller falls through to the normal chain analysis.
|
|
265
|
-
function analyzeCloneThenInspect(command: string): GitCommandDecision | null {
|
|
266
|
-
// `\` and `#` are already rejected by the early gate in analyzeGitCommand, so
|
|
267
|
-
// splitCloneHeadAndTail's quote-aware scan is trustworthy here.
|
|
268
|
-
const split = splitCloneHeadAndTail(command)
|
|
269
|
-
if (split === null) return null
|
|
270
|
-
const { head, tail } = split
|
|
271
|
-
if (tail === '') return { kind: 'block', reason: COMPOSITION_REASON }
|
|
272
|
-
|
|
273
|
-
// A git in the tail is NOT inspection — it either wants the token (so it must
|
|
274
|
-
// ride the single-bare path or an explicit `git -C`) or is an exfil sibling.
|
|
275
|
-
// Fall through so the strict chain analysis keeps its existing block/pass rules
|
|
276
|
-
// for every `clone && git …` shape.
|
|
277
|
-
if (containsGitInvocation(tail)) return null
|
|
278
|
-
|
|
279
|
-
const parsed = parseStrictCloneHead(head)
|
|
280
|
-
if (parsed === null) {
|
|
281
|
-
// A clone of a NON-github url needs no token, so the compound is harmless —
|
|
282
|
-
// pass through and let git run tokenless rather than block a legitimate
|
|
283
|
-
// e.g. gitlab clone-then-grep. Anything else (a github clone that failed the
|
|
284
|
-
// strict grammar — flags, odd dir, malformed url) falls through to the strict
|
|
285
|
-
// chain analysis, which blocks it.
|
|
286
|
-
return isNonGithubCloneHead(head) ? { kind: 'pass-through' } : null
|
|
149
|
+
function parseMintableGitInvocation(command: string, baseCwd: string): MintableInvocation | null {
|
|
150
|
+
const words = tokenizeStandalone(command)
|
|
151
|
+
if (words === null || words[0] !== 'git') return null
|
|
152
|
+
|
|
153
|
+
let cursor = 1
|
|
154
|
+
let cwd = baseCwd
|
|
155
|
+
let dashCCount = 0
|
|
156
|
+
while (words[cursor] === '-C') {
|
|
157
|
+
const dir = words[cursor + 1]
|
|
158
|
+
if (dir === undefined || !isLiteralDirectory(dir)) return null
|
|
159
|
+
cwd = resolveCwd(cwd, dir)
|
|
160
|
+
dashCCount += 1
|
|
161
|
+
cursor += 2
|
|
287
162
|
}
|
|
288
163
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
164
|
+
const subcommand = words[cursor]
|
|
165
|
+
if (!isRemoteSubcommand(subcommand)) return null
|
|
166
|
+
const args = words.slice(cursor + 1)
|
|
167
|
+
const target = parseMintableTarget(subcommand, args)
|
|
168
|
+
return target === null ? null : { subcommand, cwd, dashCCount, target }
|
|
169
|
+
}
|
|
294
170
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
171
|
+
function parseMintableTarget(subcommand: RemoteSubcommand, args: readonly string[]): TargetSpec | null {
|
|
172
|
+
if (subcommand === 'push') return parseMintablePush(args)
|
|
173
|
+
if (subcommand === 'fetch') return parseMintableFetch(args)
|
|
174
|
+
if (subcommand === 'pull') return parseSimpleRemote(args, false)
|
|
175
|
+
if (subcommand === 'clone') return parseExplicitUrl(args, 2)
|
|
176
|
+
return parseExplicitUrl(args, Number.POSITIVE_INFINITY)
|
|
300
177
|
}
|
|
301
178
|
|
|
302
|
-
|
|
179
|
+
function parseMintablePush(args: readonly string[]): TargetSpec | null {
|
|
180
|
+
const positionals: string[] = []
|
|
181
|
+
let repository: string | null = null
|
|
182
|
+
let upstream = false
|
|
303
183
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
184
|
+
for (let i = 0; i < args.length; i++) {
|
|
185
|
+
const arg = args[i] as string
|
|
186
|
+
if (arg === '-u' || arg === '--set-upstream') {
|
|
187
|
+
if (upstream) return null
|
|
188
|
+
upstream = true
|
|
189
|
+
continue
|
|
190
|
+
}
|
|
191
|
+
if (arg === '--repo' || arg === '--repository') {
|
|
192
|
+
const value = args[++i]
|
|
193
|
+
if (value === undefined || repository !== null || !isOptionValue(value)) return null
|
|
194
|
+
repository = value
|
|
195
|
+
continue
|
|
196
|
+
}
|
|
197
|
+
if (arg.startsWith('--repo=') || arg.startsWith('--repository=')) {
|
|
198
|
+
const value = arg.slice(arg.indexOf('=') + 1)
|
|
199
|
+
if (repository !== null || !isOptionValue(value)) return null
|
|
200
|
+
repository = value
|
|
201
|
+
continue
|
|
202
|
+
}
|
|
203
|
+
if (!isOptionValue(arg)) return null
|
|
204
|
+
positionals.push(arg)
|
|
205
|
+
}
|
|
313
206
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
const url = m[1] as string
|
|
318
|
-
const repoSlug = m[2] as string
|
|
319
|
-
const destination = m[3] as string
|
|
320
|
-
if (destination.startsWith('-')) return null
|
|
321
|
-
if (repoSlug.endsWith('/') || repoSlug.startsWith('/')) return null
|
|
322
|
-
return { repoSlug, url, destination }
|
|
207
|
+
if (repository !== null) return { kind: 'single', value: repository, forPush: true }
|
|
208
|
+
if (positionals.length > 0) return { kind: 'single', value: positionals[0] as string, forPush: true }
|
|
209
|
+
return upstream ? null : { kind: 'default-push' }
|
|
323
210
|
}
|
|
324
211
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
212
|
+
function parseMintableFetch(args: readonly string[]): TargetSpec | null {
|
|
213
|
+
let multiple = false
|
|
214
|
+
const positionals: string[] = []
|
|
215
|
+
for (const arg of args) {
|
|
216
|
+
if (arg === '--multiple') {
|
|
217
|
+
if (multiple) return null
|
|
218
|
+
multiple = true
|
|
219
|
+
} else if (!isOptionValue(arg)) {
|
|
220
|
+
return null
|
|
221
|
+
} else {
|
|
222
|
+
positionals.push(arg)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (positionals.length === 0) return null
|
|
226
|
+
return multiple
|
|
227
|
+
? { kind: 'multiple', values: positionals }
|
|
228
|
+
: { kind: 'single', value: positionals[0] as string, forPush: false }
|
|
334
229
|
}
|
|
335
230
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
function
|
|
231
|
+
function parseSimpleRemote(args: readonly string[], forPush: boolean): TargetSpec | null {
|
|
232
|
+
if (args.length === 0 || args.some((arg) => !isOptionValue(arg))) return null
|
|
233
|
+
return { kind: 'single', value: args[0] as string, forPush }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function parseExplicitUrl(args: readonly string[], maxPositionals: number): TargetSpec | null {
|
|
237
|
+
if (args.length === 0 || args.length > maxPositionals || args.some((arg) => !isOptionValue(arg))) return null
|
|
238
|
+
const url = args[0] as string
|
|
239
|
+
return looksLikeUrl(url) ? { kind: 'single', value: url, forPush: false } : null
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function tokenizeStandalone(command: string): string[] | null {
|
|
243
|
+
if (containsEscapeOrComment(command)) return null
|
|
244
|
+
const words: string[] = []
|
|
245
|
+
let current = ''
|
|
342
246
|
let quote: '"' | "'" | null = null
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
247
|
+
let hasContent = false
|
|
248
|
+
|
|
249
|
+
const flush = (): void => {
|
|
250
|
+
if (!hasContent) return
|
|
251
|
+
words.push(current)
|
|
252
|
+
current = ''
|
|
253
|
+
hasContent = false
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
for (const ch of command) {
|
|
257
|
+
if (quote === "'") {
|
|
258
|
+
if (ch === "'") quote = null
|
|
259
|
+
else current += ch
|
|
260
|
+
continue
|
|
261
|
+
}
|
|
262
|
+
if (quote === '"') {
|
|
263
|
+
if (ch === '$' || ch === '`') return null
|
|
264
|
+
if (ch === '"') quote = null
|
|
265
|
+
else current += ch
|
|
347
266
|
continue
|
|
348
267
|
}
|
|
349
268
|
if (ch === "'" || ch === '"') {
|
|
350
269
|
quote = ch
|
|
270
|
+
hasContent = true
|
|
351
271
|
continue
|
|
352
272
|
}
|
|
353
|
-
if (ch === '
|
|
354
|
-
|
|
273
|
+
if (ch === ' ' || ch === '\t') {
|
|
274
|
+
flush()
|
|
275
|
+
continue
|
|
355
276
|
}
|
|
356
|
-
if (
|
|
277
|
+
if (';&|\n\r(){}<>`$*?[]'.includes(ch)) return null
|
|
278
|
+
current += ch
|
|
279
|
+
hasContent = true
|
|
357
280
|
}
|
|
358
|
-
return null
|
|
281
|
+
if (quote !== null) return null
|
|
282
|
+
flush()
|
|
283
|
+
return words
|
|
359
284
|
}
|
|
360
285
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
// inert — no shell metacharacter inside can act — so the tail cannot break out of
|
|
364
|
-
// the `bash -c` argument.
|
|
365
|
-
function posixSingleQuote(value: string): string {
|
|
366
|
-
return `'${value.replaceAll("'", "'\\''")}'`
|
|
286
|
+
function isRemoteSubcommand(value: string | undefined): value is RemoteSubcommand {
|
|
287
|
+
return value === 'push' || value === 'fetch' || value === 'pull' || value === 'clone' || value === 'ls-remote'
|
|
367
288
|
}
|
|
368
289
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
if (arg === '--git-dir' || arg.startsWith('--git-dir=')) return true
|
|
381
|
-
if (arg === '--work-tree' || arg.startsWith('--work-tree=')) return true
|
|
382
|
-
if (arg === '--namespace' || arg.startsWith('--namespace=')) return true
|
|
383
|
-
if (arg === '--exec-path' || arg.startsWith('--exec-path=')) return true
|
|
290
|
+
function isLiteralDirectory(value: string): boolean {
|
|
291
|
+
return value !== '' && value !== '-' && value !== '~' && !value.startsWith('~/') && !value.startsWith('-')
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function isOptionValue(value: string): boolean {
|
|
295
|
+
return value !== '' && !value.startsWith('-') && !value.startsWith('~')
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function resolveMintableEvidence(invocation: MintableInvocation, resolvers: GitResolvers): Promise<RepoEvidence> {
|
|
299
|
+
if (invocation.target.kind === 'multiple') {
|
|
300
|
+
return resolveFetchMultiple(invocation.target.values, invocation.cwd, resolvers)
|
|
384
301
|
}
|
|
385
|
-
|
|
302
|
+
if (invocation.target.kind === 'default-push') {
|
|
303
|
+
try {
|
|
304
|
+
const remote = await resolveDefaultPushRemote(invocation.cwd, resolvers)
|
|
305
|
+
return resolveOneTarget(remote, invocation.cwd, true, resolvers, false)
|
|
306
|
+
} catch {
|
|
307
|
+
return noEvidence(false)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return resolveOneTarget(invocation.target.value, invocation.cwd, invocation.target.forPush, resolvers, false)
|
|
386
311
|
}
|
|
387
312
|
|
|
388
|
-
async function
|
|
389
|
-
|
|
390
|
-
args: readonly string[],
|
|
313
|
+
async function resolveFetchMultiple(
|
|
314
|
+
targets: readonly string[],
|
|
391
315
|
cwd: string,
|
|
392
316
|
resolvers: GitResolvers,
|
|
393
|
-
): Promise<
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
// Enumerate them all (not just the first URL) so the distinct-repo guard sees
|
|
397
|
-
// the full set. FAIL CLOSED: if ANY target can't be resolved to a github slug
|
|
398
|
-
// (an unknown remote, a config remote-group we can't expand, a non-github URL),
|
|
399
|
-
// throw so the caller passes through tokenless — never mint for a subset while
|
|
400
|
-
// git contacts the rest. Non-`--multiple` keeps the single-target path.
|
|
401
|
-
const forPush = subcommand === 'push'
|
|
402
|
-
if (subcommand === 'fetch' && args.includes('--multiple')) {
|
|
403
|
-
const slugs: string[] = []
|
|
404
|
-
for (const pos of positionalsAfterSubcommand('fetch', args)) {
|
|
405
|
-
const url = looksLikeUrl(pos) ? pos : await resolvers.resolveRemoteUrl(cwd, pos, forPush)
|
|
406
|
-
const slug = url === null ? null : parseGithubRepoFromGitUrl(url)
|
|
407
|
-
if (slug === null) throw new Error('unresolved --multiple target')
|
|
408
|
-
slugs.push(slug)
|
|
409
|
-
}
|
|
410
|
-
return slugs
|
|
411
|
-
}
|
|
317
|
+
): Promise<RepoEvidence> {
|
|
318
|
+
const slugs: string[] = []
|
|
319
|
+
let complete = targets.length > 0
|
|
412
320
|
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
321
|
+
for (const target of targets) {
|
|
322
|
+
const evidence = await resolveOneTarget(target, cwd, false, resolvers, true)
|
|
323
|
+
slugs.push(...evidence.slugs)
|
|
324
|
+
complete &&= evidence.complete
|
|
417
325
|
}
|
|
326
|
+
return { slugs, complete }
|
|
327
|
+
}
|
|
418
328
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (remote === null) continue
|
|
433
|
-
const url = looksLikeUrl(remote) ? remote : await resolvers.resolveRemoteUrl(cwd, remote, forPush)
|
|
434
|
-
if (url === null) continue
|
|
435
|
-
const slug = parseGithubRepoFromGitUrl(url)
|
|
436
|
-
if (slug !== null) slugs.push(slug)
|
|
329
|
+
async function resolveOneTarget(
|
|
330
|
+
target: string | null,
|
|
331
|
+
cwd: string,
|
|
332
|
+
forPush: boolean,
|
|
333
|
+
resolvers: GitResolvers,
|
|
334
|
+
nonGithubIsIncomplete: boolean,
|
|
335
|
+
): Promise<RepoEvidence> {
|
|
336
|
+
if (target === null) return noEvidence(false)
|
|
337
|
+
let url: string | null
|
|
338
|
+
try {
|
|
339
|
+
url = looksLikeUrl(target) ? target : await resolvers.resolveRemoteUrl(cwd, target, forPush)
|
|
340
|
+
} catch {
|
|
341
|
+
return noEvidence(false)
|
|
437
342
|
}
|
|
438
|
-
return
|
|
343
|
+
if (url === null) return noEvidence(false)
|
|
344
|
+
|
|
345
|
+
const slug = parseGithubRepoFromGitUrl(url)
|
|
346
|
+
if (slug !== null) return { slugs: [slug], complete: true }
|
|
347
|
+
return { slugs: [], complete: !nonGithubIsIncomplete }
|
|
439
348
|
}
|
|
440
349
|
|
|
441
|
-
|
|
442
|
-
async function resolveDefaultPushRemote(cwd: string, resolvers: GitResolvers): Promise<string | null> {
|
|
350
|
+
async function resolveDefaultPushRemote(cwd: string, resolvers: GitResolvers): Promise<string> {
|
|
443
351
|
const branch = await resolvers.resolveCurrentBranch(cwd)
|
|
444
352
|
if (branch !== null && branch !== '') {
|
|
445
353
|
const perBranch = await resolvers.resolveConfig(cwd, `branch.${branch}.pushRemote`)
|
|
446
354
|
if (perBranch !== null && perBranch !== '') return perBranch
|
|
447
355
|
}
|
|
448
356
|
const pushDefault = await resolvers.resolveConfig(cwd, 'remote.pushDefault')
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
//
|
|
454
|
-
//
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
const
|
|
482
|
-
if (
|
|
483
|
-
|
|
357
|
+
return pushDefault === null || pushDefault === '' ? 'origin' : pushDefault
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Evidence discovery is intentionally not a shell parser. It recognizes only a
|
|
361
|
+
// literal git at a few review-relevant boundaries, narrow redirection/wrapper
|
|
362
|
+
// prefixes, and one immediately preceding literal `cd`. Misses run tokenless.
|
|
363
|
+
async function discoverCompoundEvidence(command: string, cwd: string, resolvers: GitResolvers): Promise<RepoEvidence> {
|
|
364
|
+
const combined = noEvidence()
|
|
365
|
+
for (const segment of extractEvidenceGitSegments(command)) {
|
|
366
|
+
if (segment.cwd.kind === 'ambiguous') continue
|
|
367
|
+
const effectiveCwd = segment.cwd.kind === 'relative' ? resolveCwd(cwd, segment.cwd.dir) : cwd
|
|
368
|
+
const evidence = await discoverInvocationEvidence(segment.command, effectiveCwd, resolvers)
|
|
369
|
+
combined.slugs.push(...evidence.slugs)
|
|
370
|
+
combined.complete &&= evidence.complete
|
|
371
|
+
}
|
|
372
|
+
return combined
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
type EvidenceCwd = { kind: 'base' } | { kind: 'relative'; dir: string } | { kind: 'ambiguous' }
|
|
376
|
+
type EvidenceGitSegment = { command: string; cwd: EvidenceCwd }
|
|
377
|
+
|
|
378
|
+
function extractEvidenceGitSegments(command: string): EvidenceGitSegment[] {
|
|
379
|
+
const segments: EvidenceGitSegment[] = []
|
|
380
|
+
const assignment = '[A-Za-z_][A-Za-z0-9_]*=[^ \\t;&|(){}<>`\\r\\n]+'
|
|
381
|
+
const startRe = new RegExp(
|
|
382
|
+
`(?:^|&&|\\|\\||[;|&({\`\\n\\r])[ \\t]*` +
|
|
383
|
+
`(?:[0-9]*(?:>{1,2}|<)[ \\t]*[^ \\t;&|(){}<>\`\\r\\n]+[ \\t]+)*` +
|
|
384
|
+
`(?:(?:exec|command)[ \\t]+|env[ \\t]+(?:${assignment}[ \\t]+)*|(?:${assignment}[ \\t]+)+)?` +
|
|
385
|
+
'git(?=[ \\t]|$)',
|
|
386
|
+
'g',
|
|
387
|
+
)
|
|
388
|
+
for (const match of command.matchAll(startRe)) {
|
|
389
|
+
const matchText = match[0]
|
|
390
|
+
if (matchText === undefined || match.index === undefined) continue
|
|
391
|
+
const start = match.index + matchText.lastIndexOf('git')
|
|
392
|
+
let end = start + 3
|
|
393
|
+
while (end < command.length && !';|&(){}<>`#\n\r'.includes(command[end] as string)) end += 1
|
|
394
|
+
segments.push({
|
|
395
|
+
command: command.slice(start, end).trim(),
|
|
396
|
+
cwd: classifyEvidenceCwd(command.slice(0, match.index), matchText),
|
|
397
|
+
})
|
|
484
398
|
}
|
|
485
|
-
return
|
|
399
|
+
return segments
|
|
486
400
|
}
|
|
487
401
|
|
|
488
|
-
function
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
'
|
|
500
|
-
'-c',
|
|
501
|
-
'--config-env',
|
|
502
|
-
'--git-dir',
|
|
503
|
-
'--work-tree',
|
|
504
|
-
'--namespace',
|
|
505
|
-
'--exec-path',
|
|
506
|
-
'-o',
|
|
507
|
-
'--origin',
|
|
508
|
-
'-b',
|
|
509
|
-
'--branch',
|
|
510
|
-
'-u',
|
|
511
|
-
])
|
|
402
|
+
function classifyEvidenceCwd(prefix: string, matchText: string): EvidenceCwd {
|
|
403
|
+
const boundary = matchText.trimStart()
|
|
404
|
+
const direct = /(^|&&|\|\||[;|&({`\n\r])[ \t]*cd[ \t]+("[^"]*"|'[^']*'|[^\s'"]+)[ \t]*$/
|
|
405
|
+
const beforeGroup = /(^|&&|\|\||[;|&({`\n\r])[ \t]*cd[ \t]+("[^"]*"|'[^']*'|[^\s'"]+)[ \t]+&&[ \t]*$/
|
|
406
|
+
const match = boundary.startsWith('&&')
|
|
407
|
+
? direct.exec(prefix)
|
|
408
|
+
: boundary.startsWith('(') || boundary.startsWith('{')
|
|
409
|
+
? beforeGroup.exec(prefix)
|
|
410
|
+
: null
|
|
411
|
+
const containsPriorCd = /(?:^|&&|\|\||[;|&({`\n\r])[ \t]*cd(?:[ \t]|$)/
|
|
412
|
+
if (match === null) return containsPriorCd.test(prefix) ? { kind: 'ambiguous' } : { kind: 'base' }
|
|
413
|
+
if (containsPriorCd.test(prefix.slice(0, match.index))) return { kind: 'ambiguous' }
|
|
512
414
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
for (let i = 0; i < args.length; i++) {
|
|
516
|
-
const arg = args[i] as string
|
|
517
|
-
if (arg === '--repo' || arg === '--repository') {
|
|
518
|
-
const v = args[i + 1]
|
|
519
|
-
if (v !== undefined && looksLikeUrl(v)) return v
|
|
520
|
-
}
|
|
521
|
-
if (arg.startsWith('--repo=')) return arg.slice('--repo='.length)
|
|
522
|
-
if (arg.startsWith('--repository=')) return arg.slice('--repository='.length)
|
|
523
|
-
}
|
|
524
|
-
// First positional after the subcommand that looks like a URL.
|
|
525
|
-
for (const pos of positionalsAfterSubcommand(subcommand, args)) {
|
|
526
|
-
if (looksLikeUrl(pos)) return pos
|
|
527
|
-
}
|
|
528
|
-
return null
|
|
415
|
+
const dir = stripPairedQuotes(match[2] as string)
|
|
416
|
+
return isSafeCdDirectory(dir) ? { kind: 'relative', dir } : { kind: 'ambiguous' }
|
|
529
417
|
}
|
|
530
418
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
419
|
+
async function discoverInvocationEvidence(
|
|
420
|
+
segment: string,
|
|
421
|
+
baseCwd: string,
|
|
422
|
+
resolvers: GitResolvers,
|
|
423
|
+
): Promise<RepoEvidence> {
|
|
424
|
+
const words = segment.split(/[ \t]+/).map(stripPairedQuotes)
|
|
425
|
+
if (words[0] !== 'git') return noEvidence()
|
|
426
|
+
|
|
427
|
+
let cursor = 1
|
|
428
|
+
let cwd = baseCwd
|
|
429
|
+
while (cursor < words.length) {
|
|
430
|
+
const arg = words[cursor] as string
|
|
431
|
+
if (arg === '-C') {
|
|
432
|
+
const dir = words[cursor + 1]
|
|
433
|
+
if (dir === undefined || !isLiteralDirectory(dir)) return noEvidence()
|
|
434
|
+
cwd = resolveCwd(cwd, dir)
|
|
435
|
+
cursor += 2
|
|
539
436
|
continue
|
|
540
437
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// The remote name(s) a command targets. Normally just the first positional —
|
|
547
|
-
// later positionals are refspecs (`git push origin main` -> remote `origin`,
|
|
548
|
-
// refspec `main`). With `--multiple` (fetch), EVERY positional is a remote, so
|
|
549
|
-
// all are returned to feed the multi-owner guard. URL positionals are excluded
|
|
550
|
-
// here; resolveRepoSlugs handles a bare-URL target directly.
|
|
551
|
-
function extractRemoteNames(args: readonly string[]): string[] {
|
|
552
|
-
const sub = findSubcommand(args)
|
|
553
|
-
if (sub === undefined) return []
|
|
554
|
-
const positionals = positionalsAfterSubcommand(sub, args).filter((p) => !looksLikeUrl(p))
|
|
555
|
-
if (positionals.length === 0) return []
|
|
556
|
-
if (args.includes('--multiple')) return positionals
|
|
557
|
-
return [positionals[0] as string]
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
function positionalsAfterSubcommand(subcommand: string, args: readonly string[]): string[] {
|
|
561
|
-
const out: string[] = []
|
|
562
|
-
let seenSub = false
|
|
563
|
-
for (let i = 0; i < args.length; i++) {
|
|
564
|
-
const arg = args[i] as string
|
|
565
|
-
if (arg.startsWith('-')) {
|
|
566
|
-
if (!arg.includes('=') && GIT_VALUE_FLAGS.has(arg)) i += 1
|
|
438
|
+
if (arg === '-c' || EVIDENCE_GLOBAL_VALUE_OPTIONS.has(arg)) {
|
|
439
|
+
if (words[cursor + 1] === undefined) return noEvidence()
|
|
440
|
+
cursor += 2
|
|
567
441
|
continue
|
|
568
442
|
}
|
|
569
|
-
if (
|
|
570
|
-
|
|
443
|
+
if (arg.startsWith('-c') && arg.length > 2) {
|
|
444
|
+
cursor += 1
|
|
571
445
|
continue
|
|
572
446
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
return out
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
// git applies repeated `-C` CUMULATIVELY: each value is resolved relative to the
|
|
579
|
-
// result of the previous one, and an absolute value resets the base. So `-C
|
|
580
|
-
// /trusted -C child` operates in `/trusted/child`, not `child`. We fold them the
|
|
581
|
-
// same way from `base` so we resolve the repo of the checkout git actually uses —
|
|
582
|
-
// resolving only the last `-C` from cwd would mint against a different tree.
|
|
583
|
-
function resolveEffectiveCwd(base: string, args: readonly string[]): string {
|
|
584
|
-
let cwd = base
|
|
585
|
-
for (let i = 0; i < args.length; i++) {
|
|
586
|
-
if (args[i] === '-C') {
|
|
587
|
-
const v = args[i + 1]
|
|
588
|
-
if (v !== undefined) cwd = resolveCwd(cwd, stripQuotes(v))
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
return cwd
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
type GitInvocation = { args: string[]; hasLeadingEnvAssignment: boolean }
|
|
595
|
-
|
|
596
|
-
// Null unless there is exactly one `git` at command position. Composition is NOT
|
|
597
|
-
// rejected here — it is screened later against the original command so the block
|
|
598
|
-
// reason stays accurate. hasLeadingEnvAssignment flags `FOO=bar git …`: such an
|
|
599
|
-
// assignment lands in git's env (where a `GIT_ASKPASS=…` override would receive
|
|
600
|
-
// the token), so the caller blocks it for token-bearing commands.
|
|
601
|
-
function extractSingleGitInvocation(command: string): GitInvocation | null {
|
|
602
|
-
const tokens = tokenize(command)
|
|
603
|
-
const gitStarts: number[] = []
|
|
604
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
605
|
-
if (tokens[i] !== 'git') continue
|
|
606
|
-
if (i === 0 || isCommandBoundaryBefore(tokens, i)) gitStarts.push(i)
|
|
607
|
-
}
|
|
608
|
-
if (gitStarts.length !== 1) return null
|
|
609
|
-
const start = gitStarts[0] as number
|
|
610
|
-
const hasLeadingEnvAssignment = start > 0 && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[start - 1] ?? '')
|
|
611
|
-
const args = tokens.slice(start + 1).filter((t) => t !== '\n' && t !== ';' && t !== '|' && t !== '&')
|
|
612
|
-
return { args, hasLeadingEnvAssignment }
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
// True if `git` appears at any command boundary. Used to decide block-vs-pass:
|
|
616
|
-
// a command with no git at all is none of our business (pass-through), but one
|
|
617
|
-
// that DOES contain git yet is not a clean git-only `&&` chain must be blocked,
|
|
618
|
-
// not run tokenless.
|
|
619
|
-
function containsGitInvocation(command: string): boolean {
|
|
620
|
-
const tokens = tokenize(command)
|
|
621
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
622
|
-
if (tokens[i] === 'git' && (i === 0 || isCommandBoundaryBefore(tokens, i))) return true
|
|
623
|
-
}
|
|
624
|
-
return false
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// Splits a command into `&&`-joined segments and accepts it ONLY when EVERY
|
|
628
|
-
// segment is a single bare `git` invocation. Returns null (caller blocks or
|
|
629
|
-
// passes through) when any segment is non-git, the chain uses any operator other
|
|
630
|
-
// than `&&` (`;`, `|`, `||`, `&`, newline) at top level, or a segment carries a
|
|
631
|
-
// shell-active metachar (`$`, backtick, redirection, subshell) that could spawn
|
|
632
|
-
// or feed a NON-git process — which would inherit the shared token env. The
|
|
633
|
-
// all-git invariant is load-bearing: the token rides in a plain inherited env
|
|
634
|
-
// var, so a single non-git sibling could read it directly.
|
|
635
|
-
function extractGitInvocationChain(command: string): GitInvocation[] | null {
|
|
636
|
-
// Quote-aware screen on the ORIGINAL command: every shell-active metachar
|
|
637
|
-
// except the `&&` chain operator must be absent. This rejects `;`, `|`, `||`,
|
|
638
|
-
// single `&`, redirection, subshells, and `$`/backtick (active outside single
|
|
639
|
-
// quotes) — anything that could spawn or feed a non-git process that would
|
|
640
|
-
// inherit the shared token env. Screening the original (not dequoted tokens)
|
|
641
|
-
// keeps a safely single-quoted `$` from being a false positive.
|
|
642
|
-
if (containsUnsafeMetacharOutsideAndAnd(command)) return null
|
|
643
|
-
|
|
644
|
-
const tokens = tokenize(command)
|
|
645
|
-
if (tokens.length === 0) return null
|
|
646
|
-
|
|
647
|
-
const segments: string[][] = [[]]
|
|
648
|
-
for (const token of tokens) {
|
|
649
|
-
if (token === '&&') {
|
|
650
|
-
segments.push([])
|
|
447
|
+
if (EVIDENCE_GLOBAL_INLINE_PREFIXES.some((prefix) => arg.startsWith(prefix))) {
|
|
448
|
+
cursor += 1
|
|
651
449
|
continue
|
|
652
450
|
}
|
|
653
|
-
|
|
654
|
-
// belt-and-suspenders in case the tokenizer ever emits one we missed.
|
|
655
|
-
if (token === ';' || token === '|' || token === '||' || token === '&' || token === '\n') return null
|
|
656
|
-
;(segments[segments.length - 1] as string[]).push(token)
|
|
451
|
+
break
|
|
657
452
|
}
|
|
658
453
|
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
454
|
+
const subcommand = words[cursor]
|
|
455
|
+
if (!isRemoteSubcommand(subcommand)) return noEvidence()
|
|
456
|
+
const target = parseEvidenceTarget(subcommand, words.slice(cursor + 1))
|
|
457
|
+
if (target === null) return noEvidence()
|
|
458
|
+
if (target.kind === 'multiple') return resolveFetchMultiple(target.values, cwd, resolvers)
|
|
459
|
+
if (target.kind === 'default-push') {
|
|
460
|
+
try {
|
|
461
|
+
return resolveOneTarget(await resolveDefaultPushRemote(cwd, resolvers), cwd, true, resolvers, false)
|
|
462
|
+
} catch {
|
|
463
|
+
return noEvidence(false)
|
|
464
|
+
}
|
|
667
465
|
}
|
|
668
|
-
return
|
|
466
|
+
return resolveOneTarget(target.value, cwd, target.forPush, resolvers, false)
|
|
669
467
|
}
|
|
670
468
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
continue
|
|
683
|
-
}
|
|
684
|
-
if (quote === '"') {
|
|
685
|
-
if (ch === '$' || ch === '`') return true
|
|
686
|
-
if (ch === '"') quote = null
|
|
687
|
-
continue
|
|
688
|
-
}
|
|
689
|
-
if (ch === "'" || ch === '"') {
|
|
690
|
-
quote = ch
|
|
691
|
-
continue
|
|
692
|
-
}
|
|
693
|
-
if (ch === '&') {
|
|
694
|
-
// `&&` is the one allowed composer; a single `&` is backgrounding.
|
|
695
|
-
if (command[i + 1] === '&') {
|
|
696
|
-
i += 1
|
|
469
|
+
function parseEvidenceTarget(subcommand: RemoteSubcommand, args: readonly string[]): TargetSpec | null {
|
|
470
|
+
if (subcommand === 'push') {
|
|
471
|
+
let repository: string | null = null
|
|
472
|
+
const positionals: string[] = []
|
|
473
|
+
for (let i = 0; i < args.length; i++) {
|
|
474
|
+
const arg = args[i] as string
|
|
475
|
+
if (arg === '-u' || arg === '--set-upstream') continue
|
|
476
|
+
if (arg === '--repo' || arg === '--repository') {
|
|
477
|
+
const value = args[++i]
|
|
478
|
+
if (value === undefined || repository !== null || !isOptionValue(value)) return null
|
|
479
|
+
repository = value
|
|
697
480
|
continue
|
|
698
481
|
}
|
|
699
|
-
|
|
482
|
+
if (arg.startsWith('--repo=') || arg.startsWith('--repository=')) {
|
|
483
|
+
const value = arg.slice(arg.indexOf('=') + 1)
|
|
484
|
+
if (repository !== null || !isOptionValue(value)) return null
|
|
485
|
+
repository = value
|
|
486
|
+
continue
|
|
487
|
+
}
|
|
488
|
+
if (!isOptionValue(arg)) return null
|
|
489
|
+
positionals.push(arg)
|
|
490
|
+
}
|
|
491
|
+
if (repository !== null) return { kind: 'single', value: repository, forPush: true }
|
|
492
|
+
return positionals.length === 0
|
|
493
|
+
? { kind: 'default-push' }
|
|
494
|
+
: { kind: 'single', value: positionals[0] as string, forPush: true }
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if (subcommand === 'fetch') {
|
|
498
|
+
let multiple = false
|
|
499
|
+
const positionals: string[] = []
|
|
500
|
+
for (const arg of args) {
|
|
501
|
+
if (arg === '--multiple') multiple = true
|
|
502
|
+
else if (!isOptionValue(arg)) return null
|
|
503
|
+
else positionals.push(arg)
|
|
700
504
|
}
|
|
701
|
-
if (
|
|
505
|
+
if (positionals.length === 0) return null
|
|
506
|
+
return multiple
|
|
507
|
+
? { kind: 'multiple', values: positionals }
|
|
508
|
+
: { kind: 'single', value: positionals[0] as string, forPush: false }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (subcommand === 'pull' || subcommand === 'ls-remote') {
|
|
512
|
+
if (args.length === 0 || args.some((arg) => !isOptionValue(arg))) return null
|
|
513
|
+
return { kind: 'single', value: args[0] as string, forPush: false }
|
|
702
514
|
}
|
|
703
|
-
return false
|
|
704
|
-
}
|
|
705
515
|
|
|
706
|
-
|
|
707
|
-
let
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
516
|
+
const positionals: string[] = []
|
|
517
|
+
for (let i = 0; i < args.length; i++) {
|
|
518
|
+
const arg = args[i] as string
|
|
519
|
+
if (
|
|
520
|
+
arg === '--depth' ||
|
|
521
|
+
arg === '--branch' ||
|
|
522
|
+
arg === '-b' ||
|
|
523
|
+
arg === '--origin' ||
|
|
524
|
+
arg === '-o' ||
|
|
525
|
+
arg === '--config'
|
|
526
|
+
) {
|
|
527
|
+
if (args[++i] === undefined) return null
|
|
714
528
|
continue
|
|
715
529
|
}
|
|
716
|
-
|
|
530
|
+
if (arg.startsWith('--config=')) continue
|
|
531
|
+
if (arg.startsWith('-')) return null
|
|
532
|
+
positionals.push(arg)
|
|
717
533
|
}
|
|
718
|
-
return
|
|
534
|
+
if (positionals.length === 0 || !looksLikeUrl(positionals[0] as string)) return null
|
|
535
|
+
return { kind: 'single', value: positionals[0] as string, forPush: false }
|
|
719
536
|
}
|
|
720
537
|
|
|
721
|
-
function
|
|
538
|
+
function hasShellComposition(command: string): boolean {
|
|
539
|
+
if (containsEscapeOrComment(command)) return true
|
|
722
540
|
let quote: '"' | "'" | null = null
|
|
723
|
-
for (
|
|
724
|
-
const ch = command[i] as string
|
|
541
|
+
for (const ch of command) {
|
|
725
542
|
if (quote === "'") {
|
|
726
543
|
if (ch === "'") quote = null
|
|
727
544
|
continue
|
|
@@ -735,103 +552,131 @@ function containsShellActiveMetachar(command: string): boolean {
|
|
|
735
552
|
quote = ch
|
|
736
553
|
continue
|
|
737
554
|
}
|
|
738
|
-
if (
|
|
555
|
+
if (';&|\n\r(){}<>`$*?[]'.includes(ch)) return true
|
|
739
556
|
}
|
|
740
|
-
return
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// unsafe=true when the command LOOKS like a `cd … && git …` but the cd target is
|
|
744
|
-
// not a plain path we can faithfully turn into `git -C` (shell `~`/`-` expansion,
|
|
745
|
-
// metachars). The caller then passes through rather than rewrite wrong cwd.
|
|
746
|
-
type StrippedCd = { cdDir: string | null; rest: string; unsafe: boolean }
|
|
747
|
-
|
|
748
|
-
// Accepts ONLY `cd <simple-path> && git …`. <simple-path> must be a single
|
|
749
|
-
// token (optionally quoted) free of metachars and of a literal single quote
|
|
750
|
-
// (rewriteCdToDashC single-quotes it), and not a shell-expanded `~`/`-`. A plain
|
|
751
|
-
// command (no `cd` prefix) returns cdDir=null, unsafe=false.
|
|
752
|
-
function stripSafeCdPrefix(command: string): StrippedCd {
|
|
753
|
-
const m = command.match(/^\s*cd\s+("[^"]*"|'[^']*'|[^\s'"]+)\s+&&\s+(git\b[\s\S]*)$/)
|
|
754
|
-
if (m === null) return { cdDir: null, rest: command, unsafe: false }
|
|
755
|
-
const rawDir = m[1] as string
|
|
756
|
-
const rest = m[2] as string
|
|
757
|
-
const dir = stripQuotes(rawDir)
|
|
758
|
-
const shellExpands = dir === '-' || dir === '~' || dir.startsWith('~/')
|
|
759
|
-
if (dir.includes('$') || dir.includes('`') || dir.includes("'") || /[;|&<>()]/.test(dir) || shellExpands) {
|
|
760
|
-
return { cdDir: null, rest: command, unsafe: true }
|
|
761
|
-
}
|
|
762
|
-
return { cdDir: dir, rest, unsafe: false }
|
|
557
|
+
return quote !== null
|
|
763
558
|
}
|
|
764
559
|
|
|
765
|
-
function
|
|
766
|
-
return
|
|
560
|
+
function containsEscapeOrComment(command: string): boolean {
|
|
561
|
+
return command.includes('\\') || command.includes('#')
|
|
767
562
|
}
|
|
768
563
|
|
|
769
|
-
|
|
770
|
-
|
|
564
|
+
type SafeCd = { kind: 'none' } | { kind: 'unsafe' } | { kind: 'safe'; cwd: string; rest: string }
|
|
565
|
+
|
|
566
|
+
function parseSafeCdPrefix(command: string, baseCwd: string): SafeCd {
|
|
567
|
+
const match = command.match(/^\s*cd\s+("[^"]*"|'[^']*'|[^\s'"]+)\s+&&\s*(\S[\s\S]*?)\s*$/)
|
|
568
|
+
if (match === null) return { kind: 'none' }
|
|
569
|
+
const dir = stripPairedQuotes(match[1] as string)
|
|
570
|
+
if (!isSafeCdDirectory(dir)) return { kind: 'unsafe' }
|
|
571
|
+
return { kind: 'safe', cwd: resolveCwd(baseCwd, dir), rest: match[2] as string }
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function isSafeCdDirectory(dir: string): boolean {
|
|
575
|
+
return isLiteralDirectory(dir) && !dir.includes("'") && !/[;|&<>(){}$`\\#]/.test(dir)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function resolveCwd(base: string, dir: string): string {
|
|
771
579
|
if (dir.startsWith('/')) return dir
|
|
772
580
|
return `${base.replace(/\/$/, '')}/${dir}`
|
|
773
581
|
}
|
|
774
582
|
|
|
775
|
-
function
|
|
776
|
-
if (
|
|
777
|
-
const first =
|
|
778
|
-
const last =
|
|
779
|
-
|
|
780
|
-
return token
|
|
583
|
+
function stripPairedQuotes(value: string): string {
|
|
584
|
+
if (value.length < 2) return value
|
|
585
|
+
const first = value[0]
|
|
586
|
+
const last = value[value.length - 1]
|
|
587
|
+
return first === last && (first === '"' || first === "'") ? value.slice(1, -1) : value
|
|
781
588
|
}
|
|
782
589
|
|
|
783
|
-
//
|
|
784
|
-
//
|
|
785
|
-
function
|
|
786
|
-
const
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
590
|
+
// The token-bearing clone head is deliberately much smaller than Git's grammar.
|
|
591
|
+
// The tail is opaque and runs only in a new token-stripped shell.
|
|
592
|
+
function analyzeCloneThenInspect(command: string): GitCommandDecision | null {
|
|
593
|
+
const split = splitCloneHeadAndTail(command)
|
|
594
|
+
if (split === null) return null
|
|
595
|
+
if (split.tail === '') return { kind: 'block', reason: COMPOSITION_REASON }
|
|
596
|
+
if (tailContainsGitInvocation(split.tail)) return null
|
|
790
597
|
|
|
791
|
-
const
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
598
|
+
const parsed = parseStrictCloneHead(split.head)
|
|
599
|
+
if (parsed === null) return isNonGithubCloneHead(split.head) ? { kind: 'pass-through' } : null
|
|
600
|
+
|
|
601
|
+
const canonicalHead = `/usr/bin/git clone ${posixSingleQuote(parsed.url)} ${posixSingleQuote(parsed.destination)}`
|
|
602
|
+
return {
|
|
603
|
+
kind: 'inject',
|
|
604
|
+
repoSlug: parsed.repoSlug,
|
|
605
|
+
rewrittenCommand: `${canonicalHead} && ${TAIL_STRIP_PREFIX} ${posixSingleQuote(split.tail)}`,
|
|
797
606
|
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function tailContainsGitInvocation(tail: string): boolean {
|
|
610
|
+
return extractEvidenceGitSegments(tail).length > 0
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
type StrictCloneHead = { repoSlug: string; url: string; destination: string }
|
|
614
|
+
|
|
615
|
+
const STRICT_CLONE_HEAD_RE =
|
|
616
|
+
/^[ \t]*git[ \t]+clone[ \t]+(https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?)(?:\.git)?)[ \t]+([A-Za-z0-9._/-]+)[ \t]*$/
|
|
617
|
+
|
|
618
|
+
function parseStrictCloneHead(head: string): StrictCloneHead | null {
|
|
619
|
+
const match = STRICT_CLONE_HEAD_RE.exec(head)
|
|
620
|
+
if (match === null) return null
|
|
621
|
+
const url = match[1] as string
|
|
622
|
+
const repoSlug = match[2] as string
|
|
623
|
+
const destination = match[3] as string
|
|
624
|
+
if (destination.startsWith('-') || repoSlug.startsWith('/') || repoSlug.endsWith('/')) return null
|
|
625
|
+
return { repoSlug, url, destination }
|
|
626
|
+
}
|
|
798
627
|
|
|
628
|
+
function isNonGithubCloneHead(head: string): boolean {
|
|
629
|
+
if (!/^[ \t]*git[ \t]+clone[ \t]/.test(head)) return false
|
|
630
|
+
const url = /(?:^|[ \t])((?:https?:\/\/|git@|ssh:\/\/)[^ \t]+)/.exec(head)?.[1]
|
|
631
|
+
return url !== undefined && parseGithubRepoFromGitUrl(url) === null
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function splitCloneHeadAndTail(command: string): { head: string; tail: string } | null {
|
|
635
|
+
let quote: '"' | "'" | null = null
|
|
799
636
|
for (let i = 0; i < command.length; i++) {
|
|
800
|
-
const ch = command[i]
|
|
801
|
-
if (ch === undefined) continue
|
|
637
|
+
const ch = command[i] as string
|
|
802
638
|
if (quote !== null) {
|
|
803
639
|
if (ch === quote) quote = null
|
|
804
|
-
else current += ch
|
|
805
640
|
continue
|
|
806
641
|
}
|
|
807
|
-
if (ch === '"
|
|
642
|
+
if (ch === "'" || ch === '"') {
|
|
808
643
|
quote = ch
|
|
809
|
-
hasContent = true
|
|
810
|
-
continue
|
|
811
|
-
}
|
|
812
|
-
if (ch === ' ' || ch === '\t') {
|
|
813
|
-
flush()
|
|
814
|
-
continue
|
|
815
|
-
}
|
|
816
|
-
if (ch === '\n') {
|
|
817
|
-
flush()
|
|
818
|
-
tokens.push('\n')
|
|
819
644
|
continue
|
|
820
645
|
}
|
|
821
|
-
if (ch === '
|
|
822
|
-
|
|
823
|
-
const next = command[i + 1]
|
|
824
|
-
if ((ch === '|' && next === '|') || (ch === '&' && next === '&')) {
|
|
825
|
-
tokens.push(ch + ch)
|
|
826
|
-
i += 1
|
|
827
|
-
} else {
|
|
828
|
-
tokens.push(ch)
|
|
829
|
-
}
|
|
830
|
-
continue
|
|
646
|
+
if (ch === '&' && command[i + 1] === '&') {
|
|
647
|
+
return { head: command.slice(0, i), tail: command.slice(i + 2).trim() }
|
|
831
648
|
}
|
|
832
|
-
|
|
833
|
-
hasContent = true
|
|
649
|
+
if (ch === ';' || ch === '|' || ch === '&' || ch === '\n' || ch === '\r') return null
|
|
834
650
|
}
|
|
835
|
-
|
|
836
|
-
|
|
651
|
+
return null
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function posixSingleQuote(value: string): string {
|
|
655
|
+
return `'${value.replaceAll("'", "'\\''")}'`
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const HTTPS_GITHUB_RE = /^https:\/\/github\.com\/([^/\s:@]+)\/([^/\s?#]+?)(?:\.git)?\/?(?:[?#].*)?$/i
|
|
659
|
+
const SCP_GITHUB_RE = /^git@github\.com:([^/\s:?#]+)\/([^/\s?#]+?)(?:\.git)?$/i
|
|
660
|
+
const SSH_GITHUB_RE = /^ssh:\/\/git@github\.com\/([^/\s]+)\/([^/\s?#]+?)(?:\.git)?\/?(?:[?#].*)?$/i
|
|
661
|
+
|
|
662
|
+
export function parseGithubRepoFromGitUrl(raw: string): string | null {
|
|
663
|
+
const url = raw.trim()
|
|
664
|
+
for (const re of [HTTPS_GITHUB_RE, SCP_GITHUB_RE, SSH_GITHUB_RE]) {
|
|
665
|
+
const match = url.match(re)
|
|
666
|
+
if (match === null) continue
|
|
667
|
+
const owner = match[1]
|
|
668
|
+
const name = match[2]
|
|
669
|
+
if (owner === undefined || name === undefined || owner === '' || name === '') return null
|
|
670
|
+
return `${owner}/${name}`
|
|
671
|
+
}
|
|
672
|
+
return null
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function looksLikeUrl(token: string): boolean {
|
|
676
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(token) || /^[^@\s]+@[^:\s]+:/.test(token)
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
export async function resolveGhDefaultRepoFromCwd(cwd: string, resolvers: GitResolvers): Promise<string | null> {
|
|
680
|
+
const url = await resolvers.resolveRemoteUrl(cwd, 'origin', false)
|
|
681
|
+
return url === null ? null : parseGithubRepoFromGitUrl(url)
|
|
837
682
|
}
|