@brainervirus/workit-codex 8.12.2 → 8.13.0

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.
@@ -79,11 +79,11 @@ what and whether, the CLI does how and records what it observed.
79
79
  Native host allow/ask/deny, sandbox and org rules stay authoritative. Never
80
80
  switch paths to evade a denial, never claim enforcement a host cannot provide,
81
81
  and never fabricate an approval or verdict. A question answer is not
82
- host permission, and imported decisions or handoffs never grant authority.
82
+ host permission, and imported decisions or handoffs grant no authority.
83
83
 
84
84
  Autonomy contract:
85
85
  - Continue to the requested endpoint (commit, push, PR, green CI, verified,
86
- merged) without asking "continue?", running checks and repairs on the way.
86
+ merged) without asking "continue?", running checks and repairs.
87
87
  Stop only for a new consequential choice, a host denial, a conflicting edit
88
88
  or an unresolved blocker; report open gaps and uncertain workers.
89
89
  - No endpoint named: stop at a local commit on a policy-compliant branch and
@@ -93,7 +93,8 @@ Autonomy contract:
93
93
  never merging; \`merged\` also lands it with \`workit pr merge\`.
94
94
  Merge and release need a workspace grant only the user raises from their
95
95
  own terminal (a blocked verb names how); never edit workspaces.json or route
96
- around a grant_required.
96
+ around grant_required. Release: \`workit release start <v>\`, commits,
97
+ \`finish\`; \`publish\` if asked.
97
98
  - Ask only for a product or preference choice, or for authority you lack, with
98
99
  your recommended answer. Facts are yours: read, run or prototype.
99
100
  - Label claims measured (run and seen this session), inferred, or guess.
@@ -108,22 +109,22 @@ Autonomy contract:
108
109
  reconcile every requested item and observe it (\`workit verify-delivery\`).
109
110
 
110
111
  CLI first: if a step has one right answer, use the \`workit\` verb instead of
111
- hand-running git, gh or glab (\`workit git|pr|ci|check\`; \`workit help\` lists
112
- the rest; lacking \`workit\`, \`npx -y @brainervirus/workit-cli\`).
112
+ hand-running git, gh or glab (\`workit git|pr|ci|check|help\`;
113
+ else \`npx -y @brainervirus/workit-cli\`).
113
114
  Add Co-Authored-By via \`workit git commit --trailer "K: v" -- <paths>\`, never
114
115
  an amend.
115
116
  The task families (task, policy, evidence, finding, decision, worker, state)
116
117
  are optional continuity for tracked work; each branch has one implicit task,
117
- and a solo edit needs no task. A decision
118
- records a user choice; it never authorizes an effect. Judge work once with
118
+ and a solo edit needs no task. A decision records a user choice; it never
119
+ authorizes an effect. Judge work once with
119
120
  \`workit policy assess --judge risk= behavior= product-choice= plan=\`.
120
- Across repositories, bind each item to its checkout, branch and endpoint,
121
- resolve competing targets before a mutation, and reconcile an uncertain
122
- external effect before retrying it.
121
+ Across repositories, bind items to checkout, branch and endpoint, resolve
122
+ competing targets before a mutation, and reconcile an uncertain
123
+ external effect before a retry.
123
124
 
124
125
  Mid-task: never silently drop or resume an objective (workit-continue).
125
126
 
126
- Skills: load the skill, never act from memory of it (slash aliases /wk-<name>).
127
+ Skills: load the skill, never act from memory (aliases /wk-<name>).
127
128
  - brainstorm, plan, spec, grill, should we: workit-shape
128
129
  - implement, build, add a feature: workit-implement
129
130
  - review, blast radius: workit-review
@@ -12524,6 +12525,15 @@ var repoBranchPreset = (workspaceRoot) => {
12524
12525
  const detected = detectBranchPolicy(workspaceRoot);
12525
12526
  return detected.developBranch || detected.allowed.length ? detected.preset : null;
12526
12527
  };
12528
+ var releaseTagPrefixOf = (workspaceRoot, policyPrefix) => {
12529
+ if (typeof policyPrefix === "string")
12530
+ return policyPrefix.trim();
12531
+ const r = spawnSync5("git", ["config", "--get", "gitflow.prefix.versiontag"], {
12532
+ cwd: workspaceRoot,
12533
+ encoding: "utf8"
12534
+ });
12535
+ return r.status === 0 ? (r.stdout ?? "").trim() : "v";
12536
+ };
12527
12537
 
12528
12538
  // packages/workit-core/src/core/workspaces.ts
12529
12539
  var nonBlank = string2().refine((value) => value.trim().length > 0, "must not be blank");
@@ -18033,7 +18043,76 @@ var writeSessionMarker = (file, marker) => {
18033
18043
  };
18034
18044
 
18035
18045
  // packages/workit-core/src/hooks/shell-words.ts
18036
- var closingQuote = (command, start, dialect) => {
18046
+ class ShellAnalysisError extends Error {
18047
+ }
18048
+ var MAX_NESTING = 64;
18049
+ var OPEN = "";
18050
+ var COMMA = "";
18051
+ var CLOSE = "";
18052
+ var MARKERS = /[\uE000-\uE002]/u;
18053
+ var literal2 = (word) => word.replaceAll(OPEN, "{").replaceAll(COMMA, ",").replaceAll(CLOSE, "}");
18054
+ var BRACE_LIMIT = 64;
18055
+ var BRACE_PENDING = 512;
18056
+ var BRACE_DEPTH = 16;
18057
+ var braceGroup = (word) => {
18058
+ for (let start = 0;start < word.length; start++) {
18059
+ if (word[start] !== OPEN)
18060
+ continue;
18061
+ let depth = 0;
18062
+ let end = start;
18063
+ const commas = [];
18064
+ for (;end < word.length; end++) {
18065
+ if (word[end] === OPEN)
18066
+ depth++;
18067
+ else if (word[end] === CLOSE && --depth === 0)
18068
+ break;
18069
+ else if (word[end] === COMMA && depth === 1)
18070
+ commas.push(end);
18071
+ }
18072
+ if (end < word.length && commas.length)
18073
+ return { start, end, commas };
18074
+ }
18075
+ return null;
18076
+ };
18077
+ var expandBraces = (word) => {
18078
+ let depth = 0;
18079
+ for (const char of word) {
18080
+ if (char === OPEN && ++depth > BRACE_DEPTH)
18081
+ return [literal2(word)];
18082
+ if (char === CLOSE && depth > 0)
18083
+ depth--;
18084
+ }
18085
+ const done = new Set;
18086
+ const queue = [word];
18087
+ const queued = new Set(queue);
18088
+ for (let at = 0;at < queue.length; at++) {
18089
+ const current = queue[at];
18090
+ const group = braceGroup(current);
18091
+ if (!group) {
18092
+ const text = literal2(current);
18093
+ if (text)
18094
+ done.add(text);
18095
+ if (done.size > BRACE_LIMIT)
18096
+ return [literal2(word)];
18097
+ continue;
18098
+ }
18099
+ const pre = current.slice(0, group.start);
18100
+ const post = current.slice(group.end + 1);
18101
+ let from = group.start + 1;
18102
+ for (const comma of [...group.commas, group.end]) {
18103
+ const next = pre + current.slice(from, comma) + post;
18104
+ from = comma + 1;
18105
+ if (queued.has(next))
18106
+ continue;
18107
+ queued.add(next);
18108
+ queue.push(next);
18109
+ }
18110
+ if (queue.length - at > BRACE_PENDING)
18111
+ return [literal2(word)];
18112
+ }
18113
+ return [...done];
18114
+ };
18115
+ var closingQuote = (command, start, dialect, nesting = 0) => {
18037
18116
  const quote = command[start];
18038
18117
  for (let index = start + 1;index < command.length; index++) {
18039
18118
  const char = command[index];
@@ -18050,51 +18129,265 @@ var closingQuote = (command, start, dialect) => {
18050
18129
  index++;
18051
18130
  continue;
18052
18131
  }
18132
+ if (dialect === "posix" && char === "$" && command[index + 1] === "(") {
18133
+ index = substitutionEnd(command, index + 1, nesting + 1);
18134
+ continue;
18135
+ }
18136
+ if (dialect === "posix" && char === "`") {
18137
+ index = backtickEnd(command, index);
18138
+ continue;
18139
+ }
18053
18140
  if (char === '"')
18054
18141
  return index;
18055
18142
  }
18056
18143
  return -1;
18057
18144
  };
18145
+ var backtickEnd = (command, start) => {
18146
+ for (let index = start + 1;index < command.length; index++) {
18147
+ if (command[index] === "\\")
18148
+ index++;
18149
+ else if (command[index] === "`")
18150
+ return index;
18151
+ }
18152
+ return command.length;
18153
+ };
18154
+ var heredocDelimiter = (command, start) => {
18155
+ let index = start;
18156
+ if (command[index] === "-")
18157
+ index++;
18158
+ while (command[index] === " " || command[index] === "\t")
18159
+ index++;
18160
+ let delimiter = "";
18161
+ for (;index < command.length && !/[\s;&|<>()]/.test(command[index]); index++) {
18162
+ const char = command[index];
18163
+ if (char === "'" || char === '"') {
18164
+ const end = command.indexOf(char, index + 1);
18165
+ if (end < 0)
18166
+ break;
18167
+ delimiter += command.slice(index + 1, end);
18168
+ index = end;
18169
+ } else if (char === "\\")
18170
+ delimiter += command[++index] ?? "";
18171
+ else
18172
+ delimiter += char;
18173
+ }
18174
+ return { delimiter, end: index - 1 };
18175
+ };
18176
+ var skipHeredocs = (command, newline, delimiters) => {
18177
+ let index = newline;
18178
+ while (delimiters.length) {
18179
+ const delimiter = delimiters.shift();
18180
+ for (;; ) {
18181
+ const lineEnd = command.indexOf(`
18182
+ `, index + 1);
18183
+ const line = command.slice(index + 1, lineEnd < 0 ? undefined : lineEnd);
18184
+ index = lineEnd < 0 ? command.length : lineEnd;
18185
+ if (line.trim() === delimiter || lineEnd < 0)
18186
+ break;
18187
+ }
18188
+ }
18189
+ return index;
18190
+ };
18191
+ var substitutionEnd = (command, start, nesting = 0) => {
18192
+ if (nesting > MAX_NESTING)
18193
+ throw new ShellAnalysisError("substitutions nest too deep");
18194
+ let depth = 0;
18195
+ let wordStart = true;
18196
+ const heredocs = [];
18197
+ for (let index = start;index < command.length; index++) {
18198
+ const char = command[index];
18199
+ const atWordStart = wordStart;
18200
+ wordStart = /[\s;&|()]/.test(char);
18201
+ if (char === "\\")
18202
+ index++;
18203
+ else if (char === `
18204
+ ` && heredocs.length) {
18205
+ index = skipHeredocs(command, index, heredocs);
18206
+ wordStart = true;
18207
+ } else if (char === "'" || char === '"') {
18208
+ const end = closingQuote(command, index, "posix", nesting);
18209
+ if (end < 0)
18210
+ return command.length;
18211
+ index = end;
18212
+ } else if (char === "`")
18213
+ index = backtickEnd(command, index);
18214
+ else if (char === "#" && atWordStart) {
18215
+ const newline = command.indexOf(`
18216
+ `, index);
18217
+ index = newline < 0 ? command.length : newline - 1;
18218
+ } else if (char === "<" && command[index + 1] === "<" && command[index + 2] !== "<") {
18219
+ const { delimiter, end } = heredocDelimiter(command, index + 2);
18220
+ if (delimiter)
18221
+ heredocs.push(delimiter);
18222
+ index = end;
18223
+ } else if (char === "<" && command[index + 1] === "<")
18224
+ index += 2;
18225
+ else if (char === "$" && command[index + 1] === "(")
18226
+ index = substitutionEnd(command, index + 1, nesting + 1);
18227
+ else if (char === "(")
18228
+ depth++;
18229
+ else if (char === ")" && --depth === 0)
18230
+ return index;
18231
+ }
18232
+ return command.length;
18233
+ };
18058
18234
  var unquote = (body, quote, dialect) => {
18059
18235
  if (quote === "'")
18060
18236
  return dialect === "powershell" ? body.replaceAll("''", "'") : body;
18061
- return dialect === "posix" ? body.replace(/\\(["\\$`])/g, "$1") : body.replace(/`(.)/g, "$1");
18237
+ return dialect === "posix" ? body.replace(/\\\n/g, "").replace(/\\(["\\$`])/g, "$1") : body.replace(/`(.)/g, "$1");
18238
+ };
18239
+ var C_ESCAPES = {
18240
+ n: `
18241
+ `,
18242
+ t: "\t",
18243
+ r: "\r",
18244
+ a: "\x07",
18245
+ b: "\b",
18246
+ e: "\x1B",
18247
+ E: "\x1B",
18248
+ f: "\f",
18249
+ v: "\v"
18250
+ };
18251
+ var ansiC = (command, start) => {
18252
+ let text = "";
18253
+ let index = start + 1;
18254
+ for (;index < command.length && command[index] !== "'"; index++) {
18255
+ const char = command[index];
18256
+ if (char !== "\\" || index + 1 >= command.length) {
18257
+ text += char;
18258
+ continue;
18259
+ }
18260
+ const rest = command.slice(++index);
18261
+ const hex = /^x([0-9a-fA-F]{1,2})/.exec(rest);
18262
+ const octal = /^[0-7]{1,3}/.exec(rest);
18263
+ if (hex) {
18264
+ text += String.fromCharCode(Number.parseInt(hex[1], 16));
18265
+ index += hex[0].length - 1;
18266
+ } else if (octal) {
18267
+ text += String.fromCharCode(Number.parseInt(octal[0], 8));
18268
+ index += octal[0].length - 1;
18269
+ } else
18270
+ text += C_ESCAPES[rest[0]] ?? rest[0];
18271
+ }
18272
+ return { text, end: index };
18273
+ };
18274
+ var substitutions = (body) => {
18275
+ const found = [];
18276
+ for (let index = 0;index < body.length; index++) {
18277
+ const char = body[index];
18278
+ if (char === "\\")
18279
+ index++;
18280
+ else if (char === "`") {
18281
+ const end = backtickEnd(body, index);
18282
+ found.push(body.slice(index + 1, end).replace(/\\(.)/g, "$1"));
18283
+ index = end;
18284
+ } else if (char === "$" && body[index + 1] === "(") {
18285
+ const end = substitutionEnd(body, index + 1);
18286
+ found.push(body.slice(index + 2, end));
18287
+ index = end;
18288
+ }
18289
+ }
18290
+ return found;
18291
+ };
18292
+ var PLAIN_POSIX = /[^\s'"\\`$(){},<>|;&#]+/uy;
18293
+ var PLAIN_POWERSHELL = /[^\s'"`(){}<>|;&#]+/uy;
18294
+ var arithmeticEnd = (command, start) => {
18295
+ const end = substitutionEnd(command, start);
18296
+ return command[end - 1] === ")" && end > start + 2 ? end : 0;
18062
18297
  };
18063
18298
  var segmentsOf = (command, dialect = "posix") => {
18299
+ try {
18300
+ return tokenize(command, dialect);
18301
+ } catch (error) {
18302
+ const why = error instanceof ShellAnalysisError ? error.message : String(error);
18303
+ return [{ words: [], redirects: [], unanalyzable: why }];
18304
+ }
18305
+ };
18306
+ var tokenize = (command, dialect) => {
18307
+ const posix = dialect === "posix";
18064
18308
  const segments = [];
18065
18309
  let current = { words: [], redirects: [] };
18066
18310
  let word = "";
18067
18311
  let inWord = false;
18068
18312
  let pending = null;
18069
18313
  const heredocs = [];
18070
- const escape = dialect === "posix" ? "\\" : "`";
18314
+ const escape = posix ? "\\" : "`";
18071
18315
  const flushWord = () => {
18072
18316
  if (!inWord)
18073
18317
  return;
18318
+ const text = MARKERS.test(word) ? literal2(word) : word;
18074
18319
  if (pending === "redirect") {
18075
- if (!word.startsWith("&"))
18076
- current.redirects.push(word);
18320
+ if (!text.startsWith("&"))
18321
+ current.redirects.push(text);
18077
18322
  } else if (pending === "heredoc")
18078
- heredocs.push(word.replace(/^-/, ""));
18323
+ heredocs.push({ delimiter: text.replace(/^-/, ""), owner: current });
18324
+ else if (pending === "herestring")
18325
+ (current.stdin ??= []).push(text);
18079
18326
  else if (pending !== "input")
18080
- current.words.push(word);
18327
+ current.words.push(...MARKERS.test(word) ? expandBraces(word) : [word]);
18081
18328
  pending = null;
18082
18329
  word = "";
18083
18330
  inWord = false;
18084
18331
  };
18085
18332
  const flushSegment = () => {
18086
18333
  flushWord();
18087
- if (current.words.length || current.redirects.length)
18334
+ if (current.words.length || current.redirects.length || current.stdin || current.subs)
18088
18335
  segments.push(current);
18089
18336
  current = { words: [], redirects: [] };
18090
18337
  };
18338
+ const group = (kind) => {
18339
+ flushSegment();
18340
+ pending = null;
18341
+ segments.push({ words: [], redirects: [], group: kind });
18342
+ };
18343
+ const substitution = (inner, text) => {
18344
+ (current.subs ??= []).push(inner);
18345
+ word += text;
18346
+ inWord = true;
18347
+ };
18348
+ const plain = posix ? PLAIN_POSIX : PLAIN_POWERSHELL;
18091
18349
  for (let index = 0;index < command.length; index++) {
18350
+ plain.lastIndex = index;
18351
+ const run = plain.exec(command);
18352
+ if (run) {
18353
+ word += run[0];
18354
+ inWord = true;
18355
+ index += run[0].length - 1;
18356
+ continue;
18357
+ }
18092
18358
  const char = command[index];
18093
- if (char === "'" || char === '"') {
18359
+ const next = command[index + 1];
18360
+ if (char === escape && (next === `
18361
+ ` || next === "\r" && command[index + 2] === `
18362
+ `))
18363
+ index += next === "\r" ? 2 : 1;
18364
+ else if (char === "#" && !inWord) {
18365
+ const newline = command.indexOf(`
18366
+ `, index);
18367
+ index = (newline < 0 ? command.length : newline) - 1;
18368
+ } else if (!posix && char === "<" && next === "#") {
18369
+ const end = command.indexOf("#>", index + 2);
18370
+ index = end < 0 ? command.length : end + 1;
18371
+ } else if (posix && char === "$" && next === "'") {
18372
+ const { text, end } = ansiC(command, index + 1);
18373
+ word += text;
18374
+ inWord = true;
18375
+ index = end;
18376
+ } else if (posix && char === "$" && next === '"')
18377
+ continue;
18378
+ else if (char === "'" || char === '"') {
18094
18379
  const end = closingQuote(command, index, dialect);
18095
- word += unquote(command.slice(index + 1, end < 0 ? undefined : end), char, dialect);
18380
+ const body = command.slice(index + 1, end < 0 ? undefined : end);
18381
+ if (posix && char === '"' && /\$\(|`/.test(body))
18382
+ (current.subs ??= []).push(...substitutions(body));
18383
+ word += unquote(body, char, dialect);
18096
18384
  inWord = true;
18097
18385
  index = end < 0 ? command.length : end;
18386
+ } else if (posix && char === "`") {
18387
+ const end = backtickEnd(command, index);
18388
+ const inner = command.slice(index + 1, end);
18389
+ substitution(inner.replace(/\\(.)/g, "$1"), `\`${inner}\``);
18390
+ index = end;
18098
18391
  } else if (char === escape && index + 1 < command.length) {
18099
18392
  word += command[++index];
18100
18393
  inWord = true;
@@ -18102,26 +18395,56 @@ var segmentsOf = (command, dialect = "posix") => {
18102
18395
  `) {
18103
18396
  flushSegment();
18104
18397
  while (heredocs.length) {
18105
- const delimiter = heredocs.shift();
18398
+ const { delimiter, owner } = heredocs.shift();
18106
18399
  const lines = command.slice(index + 1).split(`
18107
18400
  `);
18108
18401
  const at = lines.findIndex((line) => line.trim() === delimiter);
18109
- const skipped = (at < 0 ? lines : lines.slice(0, at + 1)).join(`
18402
+ (owner.stdin ??= []).push((at < 0 ? lines : lines.slice(0, at)).join(`
18403
+ `));
18404
+ index += (at < 0 ? lines : lines.slice(0, at + 1)).join(`
18110
18405
  `).length + 1;
18111
- index += skipped;
18112
18406
  }
18113
18407
  } else if (/\s/.test(char))
18114
18408
  flushWord();
18115
- else if (char === "(" || char === ")") {
18409
+ else if (posix && char === "(" && inWord && word.endsWith("$")) {
18410
+ const end = substitutionEnd(command, index);
18411
+ const inner = command.slice(index + 1, end);
18412
+ substitution(inner, `(${inner})`);
18413
+ index = end;
18414
+ } else if (posix && char === "(" && (pending === "input" || pending === "redirect")) {
18415
+ const sign = pending === "input" ? "<" : ">";
18416
+ pending = null;
18417
+ const end = substitutionEnd(command, index);
18418
+ const inner = command.slice(index + 1, end);
18419
+ substitution(inner, `${sign}(${inner})`);
18420
+ index = end;
18421
+ } else if (posix && char === "(" && next === "(" && !inWord && arithmeticEnd(command, index)) {
18422
+ flushSegment();
18423
+ index = arithmeticEnd(command, index);
18424
+ } else if (char === "(" || char === ")") {
18116
18425
  if (char === "(" && inWord && word.endsWith("$")) {
18117
18426
  word = word.slice(0, -1);
18118
18427
  inWord = word !== "";
18119
18428
  }
18429
+ group(char === "(" ? "open" : "close");
18430
+ } else if (!posix && (char === "{" || char === "}")) {
18431
+ if (char === "{" && inWord && word.endsWith("@")) {
18432
+ word += char;
18433
+ continue;
18434
+ }
18120
18435
  flushSegment();
18121
- segments.push({ words: [], redirects: [], group: char === "(" ? "open" : "close" });
18122
18436
  } else if (";&|".includes(char)) {
18123
- if (char === "&" && command[index + 1] === ">")
18437
+ if (char === "&" && next === ">")
18124
18438
  continue;
18439
+ if (char === "|" && next === "|")
18440
+ index++;
18441
+ else if (char === "|") {
18442
+ flushWord();
18443
+ if (current.words.length)
18444
+ current.pipe = true;
18445
+ if (next === "&")
18446
+ index++;
18447
+ }
18125
18448
  flushSegment();
18126
18449
  } else if (char === ">") {
18127
18450
  if (inWord && /^\d$/.test(word)) {
@@ -18137,16 +18460,24 @@ var segmentsOf = (command, dialect = "posix") => {
18137
18460
  inWord = true;
18138
18461
  }
18139
18462
  pending = "redirect";
18140
- } else if (char === "<" && dialect === "posix") {
18463
+ } else if (char === "<" && posix) {
18141
18464
  flushWord();
18142
- if (command[index + 1] === "<") {
18465
+ if (next === "<" && command[index + 2] === "<") {
18466
+ index += 2;
18467
+ pending = "herestring";
18468
+ } else if (next === "<") {
18143
18469
  index++;
18144
- if (command[index + 1] === "<")
18145
- index++;
18146
18470
  pending = "heredoc";
18147
18471
  } else
18148
18472
  pending = "input";
18149
- } else {
18473
+ } else if (posix && char === "{" && !word.endsWith("$")) {
18474
+ word += OPEN;
18475
+ inWord = true;
18476
+ } else if (posix && char === "," && word.includes(OPEN))
18477
+ word += COMMA;
18478
+ else if (posix && char === "}" && word.includes(OPEN))
18479
+ word += CLOSE;
18480
+ else {
18150
18481
  word += char;
18151
18482
  inWord = true;
18152
18483
  }
@@ -18157,8 +18488,35 @@ var segmentsOf = (command, dialect = "posix") => {
18157
18488
 
18158
18489
  // packages/workit-core/src/hooks/raw-git.ts
18159
18490
  var NONE2 = { kind: "none" };
18160
- var SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh"]);
18161
- var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "nice", "&"]);
18491
+ var SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "ash", "mksh", "yash", "fish"]);
18492
+ var POWERSHELLS = new Set(["pwsh", "powershell"]);
18493
+ var EVALS = new Set(["eval", "iex", "invoke-expression"]);
18494
+ var WRAPPERS = {
18495
+ sudo: { values: "-u -g -h -p -C -D -R -T -r -t -U --user --group --host --prompt --chdir" },
18496
+ doas: { values: "-u -C" },
18497
+ command: { values: "" },
18498
+ builtin: { values: "" },
18499
+ env: { values: "-u -C --unset --chdir" },
18500
+ nohup: { values: "" },
18501
+ setsid: { values: "" },
18502
+ time: { values: "-f -o --format --output" },
18503
+ exec: { values: "-a" },
18504
+ nice: { values: "-n --adjustment" },
18505
+ ionice: { values: "-c -n -p --class --classdata" },
18506
+ stdbuf: { values: "-i -o -e --input --output --error" },
18507
+ chronic: { values: "" },
18508
+ unbuffer: { values: "" },
18509
+ wsl: { values: "-d -u --cd --distribution --user" },
18510
+ timeout: { values: "-s -k --signal --kill-after", operands: 1 },
18511
+ flock: { values: "-w -E --timeout --conflict-exit-code", operands: 1 },
18512
+ coproc: { values: "" },
18513
+ taskset: { values: "", operands: 1 },
18514
+ watch: { values: "-n -d --interval --differences -q --equexit" },
18515
+ "&": { values: "" }
18516
+ };
18517
+ var WRAPPER_VALUES = new Map(Object.entries(WRAPPERS).map(([name, wrapper]) => [name, new Set(wrapper.values.split(" "))]));
18518
+ var KEYWORDS = new Set(["if", "then", "else", "elif", "while", "until", "do", "!", "{"]);
18519
+ var DECLARATIONS = new Set(["export", "local", "declare", "readonly", "typeset"]);
18162
18520
  var SCRIPT_FLAG = /^-[a-zA-Z]*c[a-zA-Z]*$/;
18163
18521
  var CD = new Set(["cd", "chdir", "set-location", "sl"]);
18164
18522
  var PUSHD = new Set(["pushd", "push-location"]);
@@ -18172,13 +18530,21 @@ var GIT_VALUE_OPTIONS = new Set([
18172
18530
  "--config-env",
18173
18531
  "--super-prefix"
18174
18532
  ]);
18533
+ var GIT_CONFIG_OPTIONS = new Set(["-c", "--config-env", "--git-dir"]);
18534
+ var GIT_CONFIG_ENV = /^GIT_(?:CONFIG\w*|DIR|COMMON_DIR|WORK_TREE)$/;
18535
+ var ASSIGNMENT = /^([A-Za-z_]\w*)=([\s\S]*)$/;
18536
+ var MAX_DEPTH = 4;
18537
+ var MAX_EXPANSIONS = 16;
18175
18538
  var isAbsolute = (value) => path20.isAbsolute(value) || /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
18176
- var joinDir = (base, next) => {
18539
+ var HERE = /^(?:\$PWD|\$\{PWD\}|\$\(\s*pwd\s*\)|`pwd`|\$\(\s*git\s+rev-parse\s+--show-toplevel\s*\))(?=$|[\\/])/;
18540
+ var joinDir = (base, raw) => {
18541
+ const next = raw.replace(HERE, ".");
18177
18542
  if (base === undefined || /[$`*?]/.test(next) || next === "-")
18178
18543
  return;
18179
18544
  const expanded = next === "~" || next.startsWith("~/") ? path20.join(os5.homedir(), next.slice(1)) : next;
18180
18545
  return base === null || isAbsolute(expanded) ? expanded : path20.join(base, expanded);
18181
18546
  };
18547
+ var rebase = (base, inner) => inner === null ? base : inner === undefined ? undefined : joinDir(base, inner);
18182
18548
  var cdTarget = (words) => {
18183
18549
  const args = words.slice(1);
18184
18550
  const flag = args.findIndex((word) => /^-(?:literal)?path$/i.test(word));
@@ -18188,88 +18554,433 @@ var cdTarget = (words) => {
18188
18554
  };
18189
18555
  var stripPrefixes = (input) => {
18190
18556
  let words = input;
18557
+ const env = {};
18191
18558
  for (;; ) {
18192
18559
  const first = words[0];
18193
18560
  if (first === undefined)
18194
- return words;
18195
- if (/^\w+=/.test(first))
18561
+ return { words, env };
18562
+ const assignment = ASSIGNMENT.exec(first);
18563
+ if (assignment) {
18564
+ if (GIT_CONFIG_ENV.test(assignment[1]))
18565
+ env[assignment[1]] = assignment[2];
18196
18566
  words = words.slice(1);
18197
- else if (PREFIXES2.has(first)) {
18567
+ } else if (KEYWORDS.has(first))
18198
18568
  words = words.slice(1);
18199
- while (words[0]?.startsWith("-"))
18200
- words = words.slice(words[0] === "-u" ? 2 : 1);
18201
- } else if (first === "timeout") {
18569
+ else if (first === "function")
18570
+ words = words.slice(2);
18571
+ else if (Object.hasOwn(WRAPPERS, commandName(first))) {
18572
+ const name = commandName(first);
18573
+ const values = WRAPPER_VALUES.get(name);
18202
18574
  words = words.slice(1);
18203
- while (words[0]?.startsWith("-"))
18575
+ const takesValue = (option) => values.has(option) || /^-[a-zA-Z]{2,}$/.test(option) && values.has(`-${option.at(-1)}`);
18576
+ while (words[0]?.startsWith("-") && words[0] !== "--")
18577
+ words = words.slice(takesValue(words[0]) ? 2 : 1);
18578
+ if (words[0] === "--")
18204
18579
  words = words.slice(1);
18205
- words = words.slice(1);
18580
+ words = words.slice(WRAPPERS[name].operands ?? 0);
18206
18581
  } else
18207
- return words;
18582
+ return { words, env };
18208
18583
  }
18209
18584
  };
18210
18585
  var commandName = (word) => (word?.split(/[\\/]/).at(-1) ?? "").toLowerCase().replace(/\.exe$/, "");
18586
+ var VARIABLE = /\$(?:\{(\w+)\}|(\w+))/g;
18587
+ var expandWord = (word, vars) => {
18588
+ if (!word.includes("$"))
18589
+ return [word];
18590
+ let results = [""];
18591
+ let last = 0;
18592
+ for (const match of word.matchAll(VARIABLE)) {
18593
+ const values = vars.get(match[1] ?? match[2]) ?? [match[0]];
18594
+ const before = word.slice(last, match.index);
18595
+ results = results.flatMap((result) => values.map((value) => result + before + value)).slice(0, MAX_EXPANSIONS);
18596
+ last = match.index + match[0].length;
18597
+ }
18598
+ return results.map((result) => result + word.slice(last));
18599
+ };
18600
+ var substitute = (words, vars) => {
18601
+ if (vars.size === 0)
18602
+ return [words];
18603
+ let lines = [[]];
18604
+ for (const word of words) {
18605
+ const values = expandWord(word, vars);
18606
+ lines = lines.flatMap((line) => values.map((value) => [...line, value])).slice(0, MAX_EXPANSIONS);
18607
+ }
18608
+ return lines;
18609
+ };
18610
+ var assignments = (words, vars) => {
18611
+ if (words[0] === "for" && words[2] === "in" && /^\w+$/.test(words[1] ?? "")) {
18612
+ const values = words.slice(3).flatMap((word) => expandWord(word, vars));
18613
+ return [[words[1], values.slice(0, MAX_EXPANSIONS)]];
18614
+ }
18615
+ let rest = words;
18616
+ if (DECLARATIONS.has(rest[0] ?? "")) {
18617
+ rest = rest.slice(1);
18618
+ while (rest[0]?.startsWith("-"))
18619
+ rest = rest.slice(1);
18620
+ }
18621
+ if (rest.length === 0 || !rest.every((word) => ASSIGNMENT.test(word)))
18622
+ return null;
18623
+ return rest.map((word) => {
18624
+ const [, name, value] = ASSIGNMENT.exec(word);
18625
+ return [name, expandWord(value, vars)];
18626
+ });
18627
+ };
18628
+ var literalOutput = (segment) => {
18629
+ if (!segment?.pipe)
18630
+ return;
18631
+ const { words } = stripPrefixes(segment.words);
18632
+ const name = commandName(words[0]);
18633
+ if (name === "echo") {
18634
+ let args = words.slice(1);
18635
+ while (/^-[neE]+$/.test(args[0] ?? ""))
18636
+ args = args.slice(1);
18637
+ return args.join(" ");
18638
+ }
18639
+ if (name === "printf") {
18640
+ const [format = "", ...args] = words.slice(1);
18641
+ return (args.length ? args.join(`
18642
+ `) : format).replace(/\\n/g, `
18643
+ `);
18644
+ }
18645
+ if (name === "cat" && words.length === 1 && segment.stdin)
18646
+ return segment.stdin.join(`
18647
+ `);
18648
+ return;
18649
+ };
18650
+ var stdinOf = (segment, previous) => segment.stdin ? segment.stdin.join(`
18651
+ `) : literalOutput(previous);
18652
+ var XARGS_VALUES = new Set(["-n", "-L", "-l", "-P", "-d", "-E", "-e", "-s", "-a", "--arg-file", "--delimiter"].concat([
18653
+ "--max-args",
18654
+ "--max-lines",
18655
+ "--max-procs",
18656
+ "--eof",
18657
+ "--max-chars",
18658
+ "--process-slot-var"
18659
+ ]));
18660
+ var xargsCommands = (words, input) => {
18661
+ let replace = null;
18662
+ let index = 1;
18663
+ for (;index < words.length; index++) {
18664
+ const word = words[index];
18665
+ if (word === "--") {
18666
+ index++;
18667
+ break;
18668
+ }
18669
+ if (!word.startsWith("-"))
18670
+ break;
18671
+ if (word === "-I")
18672
+ replace = words[++index] ?? "{}";
18673
+ else if (word === "--replace" || word === "-i")
18674
+ replace = "{}";
18675
+ else if (word.startsWith("--replace="))
18676
+ replace = word.slice("--replace=".length);
18677
+ else if (/^-[Ii]./.test(word))
18678
+ replace = word.slice(2);
18679
+ else if (XARGS_VALUES.has(word))
18680
+ index++;
18681
+ }
18682
+ const command = words.slice(index);
18683
+ if (command.length === 0)
18684
+ return [];
18685
+ if (input === undefined)
18686
+ return [command];
18687
+ if (replace === null)
18688
+ return [[...command, ...input.split(/\s+/).filter(Boolean)]];
18689
+ const token = replace;
18690
+ return input.split(`
18691
+ `).map((line) => line.trim()).filter(Boolean).slice(0, MAX_EXPANSIONS).map((line) => command.map((word) => word.replaceAll(token, line)));
18692
+ };
18693
+ var findCommands = (words) => {
18694
+ const found = [];
18695
+ for (let index = 1;index < words.length; index++) {
18696
+ if (!/^-(?:exec|execdir|ok|okdir)$/.test(words[index]))
18697
+ continue;
18698
+ const end = words.findIndex((word, at) => at > index && (word === ";" || word === "+"));
18699
+ found.push(words.slice(index + 1, end < 0 ? undefined : end));
18700
+ if (end < 0)
18701
+ break;
18702
+ index = end;
18703
+ }
18704
+ return found;
18705
+ };
18706
+ var POWERSHELL_VALUES = /^-(?:f|file|ex|executionpolicy|wd|workingdirectory|config|configurationname|o|outputformat|if|inputformat|inp|w|windowstyle|settingsfile|custompipename|psconsolefile)$/;
18707
+ var powershellScript = (words, stdin) => {
18708
+ for (let index = 1;index < words.length; index++) {
18709
+ const word = words[index];
18710
+ if (!/^[-/]/.test(word))
18711
+ return null;
18712
+ const flag = word.replace(/^[-/]+/, "").toLowerCase();
18713
+ if (!flag)
18714
+ return null;
18715
+ if (flag === "e" || flag === "ec" || flag.length >= 2 && "encodedcommand".startsWith(flag)) {
18716
+ try {
18717
+ return Buffer.from(words[index + 1] ?? "", "base64").toString("utf16le");
18718
+ } catch {
18719
+ return null;
18720
+ }
18721
+ }
18722
+ if (flag === "cwa" || flag.length >= 8 && "commandwithargs".startsWith(flag))
18723
+ return words[index + 1] ?? null;
18724
+ if ("command".startsWith(flag))
18725
+ return words[index + 1] === "-" ? stdin ?? null : words.slice(index + 1).join(" ");
18726
+ if (POWERSHELL_VALUES.test(`-${flag}`)) {
18727
+ if (flag === "f" || flag === "file")
18728
+ return null;
18729
+ index++;
18730
+ }
18731
+ }
18732
+ return null;
18733
+ };
18734
+ var cmdScript = (words) => {
18735
+ const at = words.findIndex((word, index) => index > 0 && /^\/[ck]$/i.test(word));
18736
+ if (at > 0)
18737
+ return words.slice(at + 1).join(" ");
18738
+ const attached = words.find((word, index) => index > 0 && /^\/[ck]./i.test(word));
18739
+ return attached ? [attached.slice(2), ...words.slice(words.indexOf(attached) + 1)].join(" ") : null;
18740
+ };
18741
+ var processSubstitutionText = (word) => {
18742
+ const inner = /^<\(([\s\S]*)\)$/.exec(word ?? "")?.[1];
18743
+ if (inner === undefined)
18744
+ return;
18745
+ const segments = segmentsOf(inner);
18746
+ return segments.length === 1 ? literalOutput({ ...segments[0], pipe: true }) : undefined;
18747
+ };
18748
+ var shellScript = (words, stdin) => {
18749
+ const flag = words.findIndex((word, index) => index > 0 && SCRIPT_FLAG.test(word));
18750
+ if (flag > 0)
18751
+ return words[flag + 1] === "--" ? words[flag + 2] : words[flag + 1];
18752
+ const operand = words.slice(1).find((word) => !word.startsWith("-"));
18753
+ if (operand !== undefined)
18754
+ return processSubstitutionText(operand);
18755
+ return stdin;
18756
+ };
18757
+ var scriptCommand = (words) => {
18758
+ for (let index = 1;index < words.length; index++) {
18759
+ const word = words[index];
18760
+ if (word === "--command" || /^-[a-zA-Z]*c$/.test(word))
18761
+ return words[index + 1];
18762
+ if (word.startsWith("--command="))
18763
+ return word.slice("--command=".length);
18764
+ }
18765
+ return;
18766
+ };
18767
+ var PARALLEL_VALUES = new Set(["-j", "--jobs", "-N", "-n", "-S", "--sshlogin", "-P", "--delay"]);
18768
+ var parallelCommands = (words) => {
18769
+ let index = 1;
18770
+ while (index < words.length && words[index].startsWith("-"))
18771
+ index += PARALLEL_VALUES.has(words[index]) ? 2 : 1;
18772
+ const separator = words.indexOf(":::", index);
18773
+ const command = words.slice(index, separator < 0 ? undefined : separator);
18774
+ if (separator < 0)
18775
+ return command.length ? [command] : [];
18776
+ return words.slice(separator + 1).filter((arg) => !arg.startsWith(":::")).slice(0, MAX_EXPANSIONS).map((arg) => command.some((word) => word.includes("{}")) ? command.map((word) => word.replaceAll("{}", arg)) : [...command, arg]);
18777
+ };
18778
+ var START_PROCESS_VALUES = /^-(?:workingdirectory|wd|redirectstandard\w*|verb|windowstyle|credential|environment)$/i;
18779
+ var startProcessCommand = (words) => {
18780
+ let file;
18781
+ let args;
18782
+ const positionals = [];
18783
+ for (let index = 1;index < words.length; index++) {
18784
+ const word = words[index];
18785
+ const flag = word.startsWith("-") ? word.slice(1).toLowerCase() : null;
18786
+ if (flag === null)
18787
+ positionals.push(word);
18788
+ else if (flag && "filepath".startsWith(flag))
18789
+ file = words[++index];
18790
+ else if (flag && (flag === "args" || "argumentlist".startsWith(flag)))
18791
+ args = words[++index];
18792
+ else if (START_PROCESS_VALUES.test(word))
18793
+ index++;
18794
+ }
18795
+ file ??= positionals.shift();
18796
+ args ??= positionals.shift();
18797
+ if (!file)
18798
+ return null;
18799
+ return [file, ...(args ?? "").split(/[\s,]+/).filter(Boolean)];
18800
+ };
18801
+ var splitString = (line) => {
18802
+ if (commandName(line[0]) !== "env")
18803
+ return null;
18804
+ for (let index = 1;index < line.length; index++) {
18805
+ const word = line[index];
18806
+ if (word === "-S" || word === "--split-string")
18807
+ return line.slice(index + 1).join(" ");
18808
+ if (word.startsWith("--split-string="))
18809
+ return line.slice(index).join(" ").slice(15);
18810
+ if (/^-S./.test(word))
18811
+ return [word.slice(2), ...line.slice(index + 1)].join(" ");
18812
+ if (!word.startsWith("-") && !ASSIGNMENT.test(word))
18813
+ return null;
18814
+ }
18815
+ return null;
18816
+ };
18817
+ var CONFIG_READS = /^(?:--get\S*|-l|--list|--unset\S*|--remove-section|--rename-section|-e|--edit|--show-\S+|--name-only)$/;
18818
+ var configSetBy = (args) => {
18819
+ const positionals = args.filter((arg, index) => index > 0 && !arg.startsWith("-"));
18820
+ if (args[0] === "remote" && (positionals[0] === "add" || positionals[0] === "set-url")) {
18821
+ const [, name, url] = positionals;
18822
+ const key = args.includes("--push") ? "pushurl" : "url";
18823
+ return name && url ? ["-c", `remote.${name}.${key}=${url}`] : [];
18824
+ }
18825
+ if (args[0] !== "config" || args.some((arg) => CONFIG_READS.test(arg)))
18826
+ return [];
18827
+ const [key, value] = positionals[0] === "set" ? positionals.slice(1) : positionals;
18828
+ return key?.includes(".") && value !== undefined && positionals.length <= 3 ? ["-c", `${key}=${value}`] : [];
18829
+ };
18830
+ var gitInvocation = (words, dir, env = {}, stdin, inherited = []) => {
18831
+ let here = dir;
18832
+ const config = [...inherited];
18833
+ let gitDir = env.GIT_DIR;
18834
+ let index = 0;
18835
+ for (;index < words.length; index++) {
18836
+ const word = words[index];
18837
+ if (word === "-C")
18838
+ here = words[index + 1] ? joinDir(here, words[++index]) : here;
18839
+ else if (GIT_CONFIG_OPTIONS.has(word)) {
18840
+ if (word === "--git-dir")
18841
+ gitDir = words[index + 1];
18842
+ if (words[index + 1] !== undefined)
18843
+ config.push(word, words[++index]);
18844
+ } else if (/^--(?:config-env|git-dir)=/.test(word)) {
18845
+ if (word.startsWith("--git-dir="))
18846
+ gitDir = word.slice("--git-dir=".length);
18847
+ config.push(word);
18848
+ } else if (GIT_VALUE_OPTIONS.has(word))
18849
+ index++;
18850
+ else if (!word.startsWith("-"))
18851
+ break;
18852
+ }
18853
+ if (gitDir)
18854
+ here = joinDir(here, gitDir.replace(/(?:^|[\\/]+)\.git[\\/]*$/, "") || ".");
18855
+ return {
18856
+ tool: "git",
18857
+ dir: here,
18858
+ args: words.slice(index),
18859
+ ...config.length ? { config } : {},
18860
+ ...Object.keys(env).length ? { env } : {},
18861
+ ...stdin === undefined ? {} : { stdin }
18862
+ };
18863
+ };
18211
18864
  function rawInvocations(command, dialect = "posix", depth = 0) {
18865
+ if (depth > 0)
18866
+ return walk2(command, dialect, depth);
18867
+ const key = `${dialect}\x00${command}`;
18868
+ const cached = RECENT.get(key);
18869
+ if (cached)
18870
+ return cached;
18871
+ let found;
18872
+ try {
18873
+ found = walk2(command, dialect, depth);
18874
+ } catch (error) {
18875
+ throw error instanceof ShellAnalysisError ? error : new ShellAnalysisError(error instanceof Error ? error.message : String(error));
18876
+ }
18877
+ RECENT.set(key, found);
18878
+ if (RECENT.size > 8)
18879
+ RECENT.delete(RECENT.keys().next().value);
18880
+ return found;
18881
+ }
18882
+ var RECENT = new Map;
18883
+ function walk2(command, dialect, depth) {
18212
18884
  const found = [];
18213
18885
  let dir = null;
18214
18886
  const groups = [];
18215
18887
  let stack = [];
18216
- for (const segment of segmentsOf(command, dialect)) {
18217
- if (segment.group === "open") {
18218
- groups.push({ dir, stack: [...stack] });
18219
- continue;
18220
- }
18221
- if (segment.group === "close") {
18222
- const saved = groups.pop();
18223
- if (saved)
18224
- ({ dir, stack } = saved);
18225
- continue;
18226
- }
18227
- const words = stripPrefixes(segment.words);
18888
+ const vars = new Map;
18889
+ const exported = {};
18890
+ const sessionConfig = [];
18891
+ const nested = (script, inner) => {
18892
+ if (!script || depth >= MAX_DEPTH)
18893
+ return;
18894
+ for (const invocation of rawInvocations(script, inner, depth + 1))
18895
+ found.push({ ...invocation, dir: rebase(dir, invocation.dir) });
18896
+ };
18897
+ const visit = (input, stdin, hops) => {
18898
+ const line = /^\$[\w:]+$/.test(input[0] ?? "") && input[1] === "=" ? input.slice(2) : input;
18899
+ const split = splitString(line);
18900
+ if (split !== null)
18901
+ return nested(split, "posix");
18902
+ const { words, env } = stripPrefixes(line);
18228
18903
  const name = commandName(words[0]);
18904
+ if (!name)
18905
+ return;
18906
+ const runs = (lines) => {
18907
+ if (hops < MAX_DEPTH)
18908
+ for (const next of lines)
18909
+ visit(next, undefined, hops + 1);
18910
+ };
18229
18911
  if (CD.has(name)) {
18230
18912
  const target = cdTarget(words);
18231
18913
  dir = target === null ? os5.homedir() : joinDir(dir, target);
18232
- continue;
18233
- }
18234
- if (PUSHD.has(name)) {
18914
+ } else if (PUSHD.has(name)) {
18235
18915
  const target = cdTarget(words);
18236
18916
  stack.push(dir);
18237
18917
  dir = target === null || target === "-" ? undefined : joinDir(dir, target);
18238
- continue;
18239
- }
18240
- if (POPD.has(name)) {
18918
+ } else if (POPD.has(name))
18241
18919
  dir = stack.length ? stack.pop() : undefined;
18242
- continue;
18920
+ else if (EVALS.has(name)) {
18921
+ const args = /^-c(?:o(?:m(?:m(?:a(?:n(?:d)?)?)?)?)?)?$/i.test(words[1] ?? "") ? words.slice(2) : words.slice(1);
18922
+ nested(args.join(" "), dialect);
18923
+ } else if (SHELLS.has(name))
18924
+ nested(shellScript(words, stdin), "posix");
18925
+ else if ((name === "source" || name === ".") && dialect === "posix")
18926
+ nested(processSubstitutionText(words[1]), "posix");
18927
+ else if (name === "trap" && dialect === "posix" && !/^-[lp]$/.test(words[1] ?? ""))
18928
+ nested(words[1], "posix");
18929
+ else if (name === "script")
18930
+ nested(scriptCommand(words), "posix");
18931
+ else if (name === "parallel")
18932
+ runs(parallelCommands(words));
18933
+ else if (dialect === "powershell" && /^(?:start-process|saps|start)$/.test(name)) {
18934
+ const started = startProcessCommand(words);
18935
+ if (started)
18936
+ runs([started]);
18937
+ } else if (POWERSHELLS.has(name))
18938
+ nested(powershellScript(words, stdin), "powershell");
18939
+ else if (name === "cmd")
18940
+ nested(cmdScript(words), "powershell");
18941
+ else if (name === "xargs" || name === "find")
18942
+ runs(name === "xargs" ? xargsCommands(words, stdin) : findCommands(words));
18943
+ else if (name === "gh" || name === "glab")
18944
+ found.push({
18945
+ tool: name,
18946
+ dir,
18947
+ args: words.slice(1),
18948
+ ...stdin === undefined ? {} : { stdin }
18949
+ });
18950
+ else if (name === "git") {
18951
+ const invocation = gitInvocation(words.slice(1), dir, { ...exported, ...env }, stdin, sessionConfig);
18952
+ found.push(invocation);
18953
+ sessionConfig.push(...configSetBy(invocation.args));
18243
18954
  }
18244
- if (SHELLS.has(name) && depth < 3) {
18245
- const flag = words.findIndex((word, index) => index > 0 && SCRIPT_FLAG.test(word));
18246
- const script = flag > 0 ? words[flag + 1] : undefined;
18247
- if (script)
18248
- for (const inner of rawInvocations(script, "posix", depth + 1))
18249
- found.push({
18250
- ...inner,
18251
- dir: inner.dir === null ? dir : inner.dir === undefined ? undefined : joinDir(dir, inner.dir)
18252
- });
18955
+ };
18956
+ const segments = segmentsOf(command, dialect);
18957
+ for (const [index, segment] of segments.entries()) {
18958
+ if (segment.group === "open") {
18959
+ groups.push({ dir, stack: [...stack] });
18253
18960
  continue;
18254
18961
  }
18255
- if (name === "gh" || name === "glab") {
18256
- found.push({ tool: name, dir, args: words.slice(1) });
18962
+ if (segment.group === "close") {
18963
+ const saved = groups.pop();
18964
+ if (saved)
18965
+ ({ dir, stack } = saved);
18257
18966
  continue;
18258
18967
  }
18259
- if (name !== "git")
18968
+ if (segment.unanalyzable)
18969
+ throw new ShellAnalysisError(segment.unanalyzable);
18970
+ for (const sub of segment.subs ?? [])
18971
+ nested(sub, "posix");
18972
+ const set = assignments(segment.words, vars);
18973
+ if (set) {
18974
+ for (const [name, values] of set) {
18975
+ vars.set(name, values);
18976
+ if (segment.words[0] === "export" && GIT_CONFIG_ENV.test(name) && values[0] !== undefined)
18977
+ exported[name] = values[0];
18978
+ }
18260
18979
  continue;
18261
- let here = dir;
18262
- let index = 1;
18263
- for (;index < words.length; index++) {
18264
- const word = words[index];
18265
- if (word === "-C")
18266
- here = words[index + 1] ? joinDir(here, words[++index]) : here;
18267
- else if (GIT_VALUE_OPTIONS.has(word))
18268
- index++;
18269
- else if (!word.startsWith("-"))
18270
- break;
18271
18980
  }
18272
- found.push({ tool: "git", dir: here, args: words.slice(index) });
18981
+ const stdin = stdinOf(segment, segments[index - 1]);
18982
+ for (const line of substitute(segment.words, vars))
18983
+ visit(line, stdin, 0);
18273
18984
  }
18274
18985
  return found;
18275
18986
  }
@@ -18305,6 +19016,55 @@ function workitInvocations(command, dialect = "posix", depth = 0) {
18305
19016
  return found;
18306
19017
  }
18307
19018
  var PUSH_VALUE_OPTIONS = new Set(["-o", "--push-option", "--receive-pack", "--exec", "--repo"]);
19019
+ var CURRENT_BRANCH = /^\$\(\s*git\s+(?:branch\s+--show-current|rev-parse\s+--abbrev-ref\s+HEAD|symbolic-ref\s+--short\s+HEAD)\s*\)$/;
19020
+ var pushDestination = (destination) => {
19021
+ if (destination === "HEAD" || destination === "@" || CURRENT_BRANCH.test(destination))
19022
+ return { name: null };
19023
+ if (destination.startsWith("refs/heads/"))
19024
+ return { name: destination.slice(11) };
19025
+ if (destination.startsWith("refs/tags/"))
19026
+ return { name: undefined, tag: destination.slice(10) };
19027
+ if (destination.startsWith("refs/"))
19028
+ return { name: undefined };
19029
+ return { name: destination };
19030
+ };
19031
+ var pushRefspecs = (specs) => {
19032
+ let force = false;
19033
+ let matching = false;
19034
+ const targets = [];
19035
+ const tags = [];
19036
+ const deleted = [];
19037
+ const globs = [];
19038
+ for (const raw of specs) {
19039
+ if (raw.startsWith("+"))
19040
+ force = true;
19041
+ const spec = raw.replace(/^\+/, "");
19042
+ if (spec.startsWith("^"))
19043
+ continue;
19044
+ if (spec === ":") {
19045
+ matching = true;
19046
+ continue;
19047
+ }
19048
+ const colon = spec.indexOf(":");
19049
+ const source = colon >= 0 ? spec.slice(0, colon) : spec;
19050
+ const destination = colon >= 0 ? spec.slice(colon + 1) : spec;
19051
+ if (!destination)
19052
+ continue;
19053
+ if (destination.includes("*")) {
19054
+ globs.push({ source, destination });
19055
+ continue;
19056
+ }
19057
+ const { name, tag } = pushDestination(destination);
19058
+ if (tag !== undefined)
19059
+ tags.push(tag);
19060
+ if (name === undefined)
19061
+ continue;
19062
+ targets.push(name);
19063
+ if (colon === 0 && name !== null)
19064
+ deleted.push(name);
19065
+ }
19066
+ return { force, matching, targets, tags, deleted, globs };
19067
+ };
18308
19068
  var pushAction = (args) => {
18309
19069
  let force = false;
18310
19070
  let lease = false;
@@ -18314,6 +19074,7 @@ var pushAction = (args) => {
18314
19074
  let allBranches = false;
18315
19075
  let mirror = false;
18316
19076
  let remote = null;
19077
+ let dryRun = false;
18317
19078
  const positionals = [];
18318
19079
  for (let index = 1;index < args.length; index++) {
18319
19080
  const arg = args[index];
@@ -18321,9 +19082,11 @@ var pushAction = (args) => {
18321
19082
  positionals.push(...args.slice(index + 1));
18322
19083
  break;
18323
19084
  }
18324
- if (arg === "--dry-run" || /^-[a-zA-Z]*n[a-zA-Z]*$/.test(arg))
18325
- return null;
18326
- if (arg === "--repo")
19085
+ if (/^--dr(?:y(?:-(?:r(?:un?)?)?)?)?$/.test(arg) || /^-[a-zA-Z]*n[a-zA-Z]*$/.test(arg))
19086
+ dryRun = true;
19087
+ else if (/^--no-dr(?:y(?:-(?:r(?:un?)?)?)?)?$/.test(arg))
19088
+ dryRun = false;
19089
+ else if (arg === "--repo")
18327
19090
  remote = args[++index] ?? null;
18328
19091
  else if (arg.startsWith("--repo="))
18329
19092
  remote = arg.slice("--repo=".length);
@@ -18344,41 +19107,28 @@ var pushAction = (args) => {
18344
19107
  else if (!arg.startsWith("-"))
18345
19108
  positionals.push(arg);
18346
19109
  }
19110
+ if (dryRun)
19111
+ return null;
18347
19112
  const specs = remote === null ? positionals.slice(1) : positionals;
18348
19113
  remote ??= positionals[0] ?? null;
18349
- const targets = [];
18350
- const tags = [];
18351
- const deleted = [];
18352
- for (const raw of specs) {
18353
- if (raw.startsWith("+"))
18354
- force = true;
18355
- const spec = raw.replace(/^\+/, "");
18356
- const colon = spec.indexOf(":");
18357
- const destination = colon >= 0 ? spec.slice(colon + 1) : spec;
18358
- if (!destination)
18359
- continue;
18360
- const name = destination === "HEAD" ? null : destination.startsWith("refs/heads/") ? destination.slice(11) : destination.startsWith("refs/") ? undefined : destination;
18361
- if (destination.startsWith("refs/tags/"))
18362
- tags.push(destination.slice(10));
18363
- if (name === undefined)
18364
- continue;
18365
- targets.push(name);
18366
- if (colon === 0 && name !== null)
18367
- deleted.push(name);
18368
- }
18369
- if (specs.length === 0 && !wholesale)
18370
- targets.push(null);
19114
+ const parsed = pushRefspecs(specs);
19115
+ const implicit = specs.length === 0 && !wholesale;
19116
+ if (implicit)
19117
+ parsed.targets.push(null);
18371
19118
  return {
18372
19119
  kind: "push",
18373
- targets,
18374
- blindForce: force && !lease,
19120
+ targets: parsed.targets,
19121
+ blindForce: (force || parsed.force) && !lease,
18375
19122
  remote,
18376
- ...tags.length ? { tags } : {},
19123
+ ...parsed.tags.length ? { tags: parsed.tags } : {},
18377
19124
  ...allTags ? { allTags: true } : {},
18378
19125
  ...deletes ? { deletes: true } : {},
18379
- ...deleted.length ? { deleted } : {},
19126
+ ...parsed.deleted.length ? { deleted: parsed.deleted } : {},
18380
19127
  ...allBranches ? { allBranches: true } : {},
18381
- ...mirror ? { mirror: true } : {}
19128
+ ...mirror ? { mirror: true } : {},
19129
+ ...implicit ? { implicit: true } : {},
19130
+ ...parsed.globs.length ? { globs: parsed.globs } : {},
19131
+ ...parsed.matching ? { matching: true } : {}
18382
19132
  };
18383
19133
  };
18384
19134
  var VALUE_OPTIONS = {
@@ -18424,22 +19174,110 @@ var asksHelp = (invocation) => {
18424
19174
  }
18425
19175
  return false;
18426
19176
  };
19177
+ var API_VALUES = new Set(["--method", "--header", "--raw-field", "--field", "--jq", "--template", "--input"].concat([
19178
+ "--hostname",
19179
+ "--preview",
19180
+ "--cache",
19181
+ "--output"
19182
+ ]));
19183
+ var API_SHORT_VALUES = "XHfFqtp";
19184
+ var API_FIELDS = new Set(["-f", "-F", "--raw-field", "--field"]);
19185
+ var API_MERGE = {
19186
+ gh: /(?:^|\/)(?:pulls\/[^/?#]+\/merge|merges)\/?(?:[?#]|$)/i,
19187
+ glab: /(?:^|\/)merge_requests\/[^/?#]+\/merge\/?(?:[?#]|$)/i
19188
+ };
19189
+ var GRAPHQL_MERGE = /\b(?:mergePullRequest|enablePullRequestAutoMerge|mergeBranch|mergeRequestAccept)\b/;
19190
+ var apiMerges = (tool, args, stdin) => {
19191
+ const request = { method: null, body: false };
19192
+ let endpoint = null;
19193
+ const values = [];
19194
+ const take = (name, value) => {
19195
+ if (name === "-X" || name === "--method")
19196
+ request.method = value.toUpperCase();
19197
+ if (API_FIELDS.has(name) || name === "--input")
19198
+ request.body = true;
19199
+ values.push(value);
19200
+ const fromStdin = name === "--input" ? value === "-" : /^(?:[^=]*=)?@-$/.test(value);
19201
+ if (fromStdin && stdin !== undefined)
19202
+ values.push(stdin);
19203
+ };
19204
+ for (let index = 1;index < args.length; index++) {
19205
+ const arg = args[index];
19206
+ if (arg === "--") {
19207
+ endpoint ??= args[index + 1] ?? null;
19208
+ break;
19209
+ }
19210
+ if (arg.startsWith("--")) {
19211
+ const eq = arg.indexOf("=");
19212
+ const name = eq > 0 ? arg.slice(0, eq) : arg;
19213
+ if (API_VALUES.has(name))
19214
+ take(name, eq > 0 ? arg.slice(eq + 1) : args[++index] ?? "");
19215
+ } else if (arg.startsWith("-") && arg.length > 1) {
19216
+ for (let at = 1;at < arg.length; at++) {
19217
+ if (!API_SHORT_VALUES.includes(arg[at]))
19218
+ continue;
19219
+ take(`-${arg[at]}`, at + 1 < arg.length ? arg.slice(at + 1) : args[++index] ?? "");
19220
+ break;
19221
+ }
19222
+ } else
19223
+ endpoint ??= arg;
19224
+ }
19225
+ if (!endpoint)
19226
+ return false;
19227
+ endpoint = endpoint.replace(/(?<!:)\/{2,}/g, "/");
19228
+ if (/(?:^|\/)graphql\/?$/i.test(endpoint))
19229
+ return values.some((value) => GRAPHQL_MERGE.test(value));
19230
+ const verb = request.method ?? (request.body ? "POST" : "GET");
19231
+ return verb !== "GET" && verb !== "HEAD" && API_MERGE[tool].test(endpoint);
19232
+ };
19233
+ var subtreePush = (args) => {
19234
+ const positionals = [];
19235
+ for (let index = 2;index < args.length; index++) {
19236
+ const arg = args[index];
19237
+ if (arg === "-P" || arg === "--prefix" || arg === "-m" || arg === "--message")
19238
+ index++;
19239
+ else if (!arg.startsWith("-"))
19240
+ positionals.push(arg);
19241
+ }
19242
+ const [remote, branch] = positionals;
19243
+ return remote && branch ? pushAction(["push", remote, `HEAD:refs/heads/${branch}`]) : null;
19244
+ };
18427
19245
  function rawAction(invocation) {
18428
19246
  const args = invocation.args;
19247
+ if (invocation.tool !== "git" && args[0] === "api")
19248
+ return apiMerges(invocation.tool, args, invocation.stdin) ? { kind: "merge", forge: invocation.tool, via: "api" } : null;
18429
19249
  if (asksHelp(invocation))
18430
19250
  return null;
18431
- const [first, second] = args;
19251
+ const [first] = args;
18432
19252
  if (invocation.tool === "git") {
19253
+ const second = args[1];
18433
19254
  if (first === "commit")
18434
19255
  return args.includes("--dry-run") ? null : { kind: "commit", amend: args.includes("--amend") };
18435
19256
  if (first === "push")
18436
19257
  return pushAction(args);
19258
+ if (first === "send-pack")
19259
+ return pushAction([
19260
+ "push",
19261
+ ...args.slice(1).filter((arg) => arg !== "--stdin"),
19262
+ ...args.includes("--stdin") ? (invocation.stdin ?? "").split(/\s+/).filter(Boolean) : []
19263
+ ]);
19264
+ if (first === "subtree" && second === "push")
19265
+ return subtreePush(args);
18437
19266
  return null;
18438
19267
  }
18439
19268
  const forge = invocation.tool;
18440
19269
  const noun = forge === "gh" ? "pr" : "mr";
18441
19270
  if (first !== noun)
18442
19271
  return null;
19272
+ const values = VALUE_OPTIONS[forge];
19273
+ let second;
19274
+ for (let index = 1;index < args.length && second === undefined; index++) {
19275
+ const arg = args[index];
19276
+ if (values.has(arg))
19277
+ index++;
19278
+ else if (!arg.startsWith("-"))
19279
+ second = arg;
19280
+ }
18443
19281
  if (second === "merge")
18444
19282
  return { kind: "merge", forge };
18445
19283
  if (second === "create")
@@ -18471,16 +19309,6 @@ var guardedBranch = (dir, branch) => {
18471
19309
  return false;
18472
19310
  }
18473
19311
  };
18474
- var toPushRemote = (repo, remote) => {
18475
- if (remote === null)
18476
- return true;
18477
- try {
18478
- const pushRemote = pushRemoteName(repo.dir, repo.branch);
18479
- return pushRemote === null || pushRemote === remote;
18480
- } catch {
18481
- return false;
18482
- }
18483
- };
18484
19312
  var headSha2 = (repo) => {
18485
19313
  const git = repo.location.git;
18486
19314
  if (!git || git.reftable)
@@ -18502,13 +19330,172 @@ var headSha2 = (repo) => {
18502
19330
  return null;
18503
19331
  }
18504
19332
  };
19333
+ var gitConfig = (dir, invocation, cache) => {
19334
+ const key = JSON.stringify([dir, invocation.config ?? [], invocation.env ?? {}]);
19335
+ const cached = cache.get(key);
19336
+ if (cached)
19337
+ return cached;
19338
+ const config = new Map;
19339
+ const listed = spawnSync11("git", [...invocation.config ?? [], "config", "-z", "--list"], {
19340
+ cwd: dir,
19341
+ encoding: "utf8",
19342
+ timeout: 3000,
19343
+ windowsHide: true,
19344
+ env: { ...process.env, ...invocation.env, GIT_TERMINAL_PROMPT: "0", GIT_OPTIONAL_LOCKS: "0" }
19345
+ });
19346
+ for (const entry of (listed.status === 0 ? listed.stdout : "").split("\x00")) {
19347
+ if (!entry)
19348
+ continue;
19349
+ const newline = entry.indexOf(`
19350
+ `);
19351
+ const name = newline < 0 ? entry : entry.slice(0, newline);
19352
+ config.set(name, [
19353
+ ...config.get(name) ?? [],
19354
+ newline < 0 ? "true" : entry.slice(newline + 1)
19355
+ ]);
19356
+ }
19357
+ cache.set(key, config);
19358
+ return config;
19359
+ };
19360
+ var configValue = (config, key) => config.get(key)?.at(-1);
19361
+ var GIT_COMMANDS = new Set(("add am annotate apply archive bisect blame branch bugreport bundle cat-file check-attr " + "check-ignore check-mailmap check-ref-format checkout checkout-index cherry cherry-pick " + "citool clean clone column commit commit-graph commit-tree config count-objects credential " + "daemon describe diagnose diff diff-files diff-index diff-tree difftool fast-export " + "fast-import fetch fetch-pack filter-branch fmt-merge-msg for-each-ref for-each-repo " + "format-patch fsck gc get-tar-commit-id grep gui hash-object help hook index-pack init " + "instaweb interpret-trailers lfs log ls-files ls-remote ls-tree mailinfo mailsplit " + "maintenance merge merge-base merge-file merge-index merge-tree mergetool mktag mktree " + "multi-pack-index mv name-rev notes pack-objects pack-refs patch-id prune pull push " + "range-diff read-tree rebase reflog remote repack replace request-pull rerere reset " + "restore rev-list rev-parse revert rm send-email shortlog show show-branch show-ref " + "sparse-checkout stage stash status stripspace submodule subtree switch symbolic-ref tag " + "update-index update-ref var verify-commit verify-pack verify-tag version whatchanged " + "worktree write-tree").split(" "));
19362
+ var JUDGED_COMMANDS = [
19363
+ "push",
19364
+ "commit",
19365
+ "switch",
19366
+ "checkout",
19367
+ "branch",
19368
+ "worktree",
19369
+ "stash",
19370
+ "update-ref",
19371
+ "symbolic-ref",
19372
+ "fetch",
19373
+ "pull",
19374
+ "subtree"
19375
+ ];
19376
+ var editDistance2 = (a, b) => {
19377
+ let before = [];
19378
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
19379
+ for (let i = 1;i <= a.length; i++) {
19380
+ const row = [i];
19381
+ for (let j = 1;j <= b.length; j++) {
19382
+ row[j] = Math.min(previous[j] + 1, row[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
19383
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
19384
+ row[j] = Math.min(row[j], before[j - 2] + 1);
19385
+ }
19386
+ [before, previous] = [previous, row];
19387
+ }
19388
+ return previous[b.length];
19389
+ };
19390
+ var autocorrected = (name, config) => {
19391
+ const setting = (configValue(config, "help.autocorrect") ?? "0").toLowerCase();
19392
+ if (setting !== "immediate" && !/^-?[1-9]\d*$/.test(setting))
19393
+ return null;
19394
+ let best = null;
19395
+ let bestDistance = 3;
19396
+ let tie = false;
19397
+ for (const command of JUDGED_COMMANDS) {
19398
+ const distance = editDistance2(name, command);
19399
+ if (distance < bestDistance)
19400
+ [best, bestDistance, tie] = [command, distance, false];
19401
+ else if (distance === bestDistance)
19402
+ tie = true;
19403
+ }
19404
+ return tie ? null : best;
19405
+ };
19406
+ var shellQuote = (word) => `'${word.replaceAll("'", "'\\''")}'`;
19407
+ var positional = (script, args) => {
19408
+ const word = (arg) => /^[\w./@+:=-]+$/.test(arg) ? arg : shellQuote(arg);
19409
+ return script.replace(/\$\{?([1-9])\}?/g, (_, n) => word(args[Number(n) - 1] ?? "")).replace(/\$\{?[@*]\}?/g, args.map(word).join(" "));
19410
+ };
19411
+ var expandGitAliases = (root, invocation, cache = new Map, depth = 0) => {
19412
+ const sub = invocation.args[0];
19413
+ if (invocation.tool !== "git" || invocation.dir === undefined || !sub || sub.startsWith("-") || GIT_COMMANDS.has(sub) || depth >= MAX_DEPTH)
19414
+ return [invocation];
19415
+ const dir = path20.resolve(root, invocation.dir ?? ".");
19416
+ if (!fs16.existsSync(dir))
19417
+ return [invocation];
19418
+ const config = gitConfig(dir, invocation, cache);
19419
+ const alias = configValue(config, `alias.${sub.toLowerCase()}`);
19420
+ if (alias === undefined) {
19421
+ const corrected = autocorrected(sub, config);
19422
+ return corrected ? [{ ...invocation, args: [corrected, ...invocation.args.slice(1)] }] : [invocation];
19423
+ }
19424
+ const rest = invocation.args.slice(1);
19425
+ const expanded = alias.startsWith("!") ? rawInvocations([positional(alias.slice(1), rest), ...rest.map(shellQuote)].join(" ")).map((inner) => ({
19426
+ ...inner,
19427
+ dir: rebase(invocation.dir, inner.dir),
19428
+ ...inner.tool === "git" && (invocation.config || inner.config) ? { config: [...invocation.config ?? [], ...inner.config ?? []] } : {},
19429
+ ...inner.tool === "git" && (invocation.env || inner.env) ? { env: { ...invocation.env, ...inner.env } } : {}
19430
+ })) : [
19431
+ gitInvocation([...segmentsOf(alias)[0]?.words ?? [], ...rest], invocation.dir, invocation.env, invocation.stdin, invocation.config)
19432
+ ];
19433
+ return expanded.flatMap((inner) => expandGitAliases(root, inner, cache, depth + 1));
19434
+ };
19435
+ var normalizedUrl = (url, dir) => {
19436
+ const trimmed = url.trim();
19437
+ if (/^file:\/\//i.test(trimmed))
19438
+ return path20.resolve(dir, trimmed.slice(7)).replace(/\/+$/, "").replace(/\.git$/, "");
19439
+ const scheme = /^[a-z][a-z0-9+.-]*:\/\//i.exec(trimmed);
19440
+ const scp = !scheme && /^(?:[^@/\\]+@)?[^/\\:]{2,}:/.test(trimmed);
19441
+ if (!scheme && !scp)
19442
+ return path20.resolve(dir, trimmed).replace(/\/+$/, "").replace(/\.git$/, "");
19443
+ const value = scheme ? trimmed.slice(scheme[0].length).replace(/^[^@/]*@/, "").replace(/^([^/:]+):\d+(?=\/)/, "$1") : trimmed.replace(/^[^@/]*@/, "").replace(":", "/");
19444
+ return value.toLowerCase().replace(/\/{2,}/g, "/").replace(/^www\./, "").replace(/\/+$/, "").replace(/\.git$/, "");
19445
+ };
19446
+ var rewrittenUrl = (url, config) => {
19447
+ for (const kind of ["pushinsteadof", "insteadof"]) {
19448
+ let best = null;
19449
+ for (const [key, prefixes] of config) {
19450
+ if (!key.startsWith("url.") || !key.endsWith(`.${kind}`))
19451
+ continue;
19452
+ const base = key.slice(4, -(kind.length + 1));
19453
+ for (const prefix of prefixes)
19454
+ if (url.startsWith(prefix) && prefix.length > (best?.prefix.length ?? -1))
19455
+ best = { base, prefix };
19456
+ }
19457
+ if (best)
19458
+ return best.base + url.slice(best.prefix.length);
19459
+ }
19460
+ return url;
19461
+ };
19462
+ var pushUrl = (config, remote, dir) => {
19463
+ const configured = configValue(config, `remote.${remote}.pushurl`) ?? configValue(config, `remote.${remote}.url`);
19464
+ return normalizedUrl(rewrittenUrl(configured ?? remote, config), dir);
19465
+ };
19466
+ var pushRemoteOf = (config, branch) => {
19467
+ const remotes = new Set;
19468
+ for (const key of config.keys()) {
19469
+ const name = /^remote\.(.+)\.url$/.exec(key)?.[1];
19470
+ if (name)
19471
+ remotes.add(name);
19472
+ }
19473
+ for (const candidate of [
19474
+ branch ? configValue(config, `branch.${branch}.pushremote`) : undefined,
19475
+ configValue(config, "remote.pushdefault"),
19476
+ branch ? configValue(config, `branch.${branch}.remote`) : undefined
19477
+ ])
19478
+ if (candidate && candidate !== "." && remotes.has(candidate))
19479
+ return candidate;
19480
+ if (remotes.has("origin"))
19481
+ return "origin";
19482
+ return remotes.size === 1 ? [...remotes][0] : null;
19483
+ };
19484
+ var toPushRemote = (repo, remote, config) => {
19485
+ if (remote === null || /[$`]/.test(remote))
19486
+ return true;
19487
+ const pushRemote = pushRemoteOf(config, repo.branch);
19488
+ if (pushRemote === null || pushRemote === remote)
19489
+ return true;
19490
+ return pushUrl(config, remote, repo.dir) === pushUrl(config, pushRemote, repo.dir);
19491
+ };
18505
19492
  var quote = (text) => `\`${text}\``;
18506
19493
  var refusal = (action, branch, dir) => {
18507
19494
  if (action.kind === "merge")
18508
- return `workit: raw ${quote(action.forge === "gh" ? "gh pr merge" : "glab mr merge")} bypasses the merge grant and the verdict gate. Run ${quote("workit pr merge")} instead (it checks both; ${quote("workit help pr")}).`;
19495
+ return `workit: raw ${quote(action.via === "api" ? `${action.forge} api` : action.forge === "gh" ? "gh pr merge" : "glab mr merge")}${action.via === "api" ? " merge" : ""} bypasses the merge grant and the verdict gate. Run ${quote("workit pr merge")} instead (it checks both; ${quote("workit help pr")}).`;
18509
19496
  const plainPush = action.kind === "push" && !action.blindForce && !action.deletes;
18510
19497
  if (dir && branch && plainPush && releaseBranch(dir, branch))
18511
- return `workit: raw ${quote("git push")} onto ${branch}, a protected release branch, bypasses workit's protected-branch check. To finish a release or hotfix, push it with ${quote(`workit git push --release ${branch} [--release v<version>]`)}: it pushes ${branch} (fast-forward only) and the release tag, verifies the remote and records it, under the ${quote("release")} grant (if refused, ask the user to run ${quote("workit grant set <workspace> release=true")}). Feature work goes through ${quote("workit git push")} and ${quote("workit pr merge")}.`;
19498
+ return `workit: raw ${quote("git push")} onto ${branch}, a protected release branch, bypasses workit's protected-branch check. To finish a release or hotfix, push it with ${quote(`workit git push --release ${branch} [--release ${releaseTagPrefixOf(dir)}<version>]`)}: it pushes ${branch} (fast-forward only) and the release tag, verifies the remote and records it, under the ${quote("release")} grant (if refused, ask the user to run ${quote("workit grant set <workspace> release=true")}). Feature work goes through ${quote("workit git push")} and ${quote("workit pr merge")}.`;
18512
19499
  return `workit: raw ${quote("git push")}${action.kind === "push" && action.blindForce ? " --force" : ""} onto ${branch}, a protected branch or the default target, bypasses workit's protected-branch check. Push your feature branch with ${quote("workit git push")} (${quote("workit git push --force-with-lease")} to rewrite it) and land it with ${quote("workit pr merge")}.`;
18513
19500
  };
18514
19501
  var refFacts = (repo, remote) => {
@@ -18526,35 +19513,112 @@ var refFacts = (repo, remote) => {
18526
19513
  }
18527
19514
  return facts;
18528
19515
  };
19516
+ var globBranches = (repo, glob) => {
19517
+ const source = glob.source.startsWith("refs/") ? glob.source : `refs/heads/${glob.source}`;
19518
+ const [sourcePrefix, sourceSuffix = ""] = source.split("*");
19519
+ const [destinationPrefix, destinationSuffix = ""] = glob.destination.split("*");
19520
+ const listed = spawnSync11("git", [
19521
+ "for-each-ref",
19522
+ "--format=%(refname)",
19523
+ sourcePrefix.slice(0, sourcePrefix.lastIndexOf("/") + 1)
19524
+ ], { cwd: repo.dir, encoding: "utf8", timeout: 5000, windowsHide: true });
19525
+ const names = [];
19526
+ for (const ref of (listed.stdout ?? "").split(`
19527
+ `)) {
19528
+ if (!ref.startsWith(sourcePrefix) || !ref.endsWith(sourceSuffix))
19529
+ continue;
19530
+ const middle = ref.slice(sourcePrefix.length, ref.length - sourceSuffix.length);
19531
+ const { name } = pushDestination(destinationPrefix + middle + destinationSuffix);
19532
+ if (name)
19533
+ names.push(name);
19534
+ }
19535
+ return names;
19536
+ };
19537
+ var pushedBranches = (action, repo, config, facts, remote) => {
19538
+ const targets = action.implicit ? [] : [...action.targets];
19539
+ const globs = [...action.globs ?? []];
19540
+ let matching = action.matching === true;
19541
+ const configured = action.implicit ? config.get(`remote.${remote}.push`) : undefined;
19542
+ if (configured?.length) {
19543
+ const parsed = pushRefspecs(configured);
19544
+ targets.push(...parsed.targets);
19545
+ globs.push(...parsed.globs);
19546
+ matching ||= parsed.matching;
19547
+ } else if (action.implicit) {
19548
+ const mode = (configValue(config, "push.default") ?? "simple").toLowerCase();
19549
+ const merge = repo.branch ? configValue(config, `branch.${repo.branch}.merge`) : undefined;
19550
+ if (mode === "matching")
19551
+ matching = true;
19552
+ else if ((mode === "upstream" || mode === "tracking") && merge?.startsWith("refs/heads/"))
19553
+ targets.push(merge.slice(11));
19554
+ else if (mode !== "nothing")
19555
+ targets.push(null);
19556
+ }
19557
+ if (action.allBranches)
19558
+ targets.push(...facts.heads);
19559
+ if (matching) {
19560
+ for (const head of facts.heads)
19561
+ if (facts.remoteHeads.has(head))
19562
+ targets.push(head);
19563
+ }
19564
+ for (const glob of globs)
19565
+ targets.push(...globBranches(repo, glob));
19566
+ return targets;
19567
+ };
19568
+ var literalReadings = (name) => {
19569
+ if (!/[$`]/.test(name))
19570
+ return [name];
19571
+ const withDefaults = name.replace(/\$\{\w+:?[-=]([^}$`]*)\}/g, "$1").replace(/\$\{\w+\}|\$\w+/g, "");
19572
+ return /[$`]/.test(withDefaults) || !withDefaults ? [] : [withDefaults];
19573
+ };
19574
+ var guardedTarget = (dir, target) => {
19575
+ for (const branch of literalReadings(target)) {
19576
+ if (guardedBranch(dir, branch))
19577
+ return branch;
19578
+ const bare = branch.startsWith("heads/") ? branch.slice(6) : null;
19579
+ if (bare && guardedBranch(dir, bare))
19580
+ return bare;
19581
+ }
19582
+ return null;
19583
+ };
18529
19584
  var isTag = (facts, name) => facts.tags.has(name) && !facts.heads.has(name);
18530
- var tagRefusal = (action, facts) => {
19585
+ var tagRefusal = (action, facts, dir) => {
18531
19586
  const tags = [
18532
19587
  ...action.tags ?? [],
18533
19588
  ...action.targets.filter((name) => name !== null && isTag(facts, name))
18534
19589
  ];
18535
19590
  if (!tags.length && !action.allTags)
18536
19591
  return null;
18537
- const named = tags.length ? tags.join(" --release ") : "v<version>";
18538
- return `workit: raw ${quote(`git push${action.allTags ? " --tags" : ""}`)} of ${tags.length ? `tag ${tags.join(", ")}` : "tags"} bypasses the release grant. Push a release tag with ${quote(`workit git push --release ${named}`)} (v<version> tags whose commit the production branch already has; a tag never moves), under the ${quote("release")} grant (if refused, ask the user to run ${quote("workit grant set <workspace> release=true")}).`;
19592
+ const shape = `${releaseTagPrefixOf(dir)}<version>`;
19593
+ const named = tags.length ? tags.join(" --release ") : shape;
19594
+ return `workit: raw ${quote(`git push${action.allTags ? " --tags" : ""}`)} of ${tags.length ? `tag ${tags.join(", ")}` : "tags"} bypasses the release grant. Push a release tag with ${quote(`workit git push --release ${named}`)} (${shape} tags whose commit the production branch already has; a tag never moves), under the ${quote("release")} grant (if refused, ask the user to run ${quote("workit grant set <workspace> release=true")}).`;
18539
19595
  };
18540
- var remoteHas = (repo, remote, branch) => {
18541
- const listed = spawnSync11("git", ["ls-remote", "--heads", remote, `refs/heads/${branch}`], {
19596
+ var remoteHeads = (repo, remote) => {
19597
+ const listed = spawnSync11("git", ["ls-remote", "--heads", remote], {
18542
19598
  cwd: repo.dir,
18543
19599
  encoding: "utf8",
18544
19600
  timeout: 3000,
19601
+ windowsHide: true,
18545
19602
  env: { ...process.env, GIT_TERMINAL_PROMPT: "0" }
18546
19603
  });
18547
- return listed.status !== 0 || (listed.stdout ?? "").trim() !== "";
19604
+ if (listed.status !== 0)
19605
+ return null;
19606
+ return new Set((listed.stdout ?? "").split(`
19607
+ `).map((line) => line.split("\t")[1] ?? "").filter((ref) => ref.startsWith("refs/heads/")).map((ref) => ref.slice(11)));
18548
19608
  };
18549
19609
  var offPolicyPush = (action, repo, facts, remote) => {
18550
19610
  const names = action.allBranches ? [...facts.heads] : action.targets.map((target) => target ?? repo.branch);
19611
+ let listed;
19612
+ const remoteHas = (branch) => (listed === undefined ? listed = remoteHeads(repo, remote) : listed)?.has(branch) ?? true;
18551
19613
  for (const branch of names) {
18552
- if (!branch || action.deleted?.includes(branch) || isTag(facts, branch))
19614
+ if (!branch || /[$`]/.test(branch))
19615
+ continue;
19616
+ if (action.deleted?.includes(branch) || isTag(facts, branch))
18553
19617
  continue;
18554
19618
  if (facts.remoteHeads.has(branch))
18555
19619
  continue;
18556
19620
  const check = validateBranchNameFor(repo.dir, branch);
18557
- if (check.ok || check.rule !== "allowed_pattern" || remoteHas(repo, remote, branch))
19621
+ if (check.ok || check.rule !== "allowed_pattern" || remoteHas(branch))
18558
19622
  continue;
18559
19623
  return `workit: raw ${quote("git push")} onto ${branch}: ${check.error}`;
18560
19624
  }
@@ -18576,23 +19640,50 @@ var nudgeFor = (action, host, session) => {
18576
19640
  return null;
18577
19641
  }
18578
19642
  };
18579
- var actionsIn = (cwd, command, dialect) => {
19643
+ var pushRefusal = (action, repo, config) => {
19644
+ if (!toPushRemote(repo, action.remote, config))
19645
+ return null;
19646
+ if (action.mirror)
19647
+ return `workit: raw ${quote("git push --mirror")} rewrites and deletes every remote ref, protected branches and tags included. Push one branch with ${quote("workit git push")}.`;
19648
+ const remote = action.remote ?? pushRemoteOf(config, repo.branch) ?? "origin";
19649
+ const facts = refFacts(repo, remote);
19650
+ const targets = pushedBranches(action, repo, config, facts, remote);
19651
+ const pushed = { ...action, targets };
19652
+ const offPolicy = action.allBranches ? offPolicyPush(pushed, repo, facts, remote) : null;
19653
+ if (offPolicy)
19654
+ return offPolicy;
19655
+ for (const target of targets) {
19656
+ const guarded = guardedTarget(repo.dir, target ?? repo.branch ?? "");
19657
+ if (guarded)
19658
+ return refusal(action, guarded, repo.dir);
19659
+ }
19660
+ return tagRefusal(pushed, facts, repo.dir) ?? (action.deletes ? null : offPolicyPush(pushed, repo, facts, remote));
19661
+ };
19662
+ var actionsIn = (cwd, command, dialect, cache = new Map) => {
18580
19663
  const out = [];
18581
19664
  const repos = new Map;
18582
- for (const invocation of rawInvocations(command, dialect)) {
18583
- if (invocation.dir === undefined)
18584
- continue;
18585
- const action = rawAction(invocation);
18586
- if (!action)
18587
- continue;
18588
- const dir = path20.resolve(cwd, invocation.dir ?? ".");
19665
+ const repoFor = (relative) => {
19666
+ const dir = path20.resolve(cwd, relative ?? ".");
18589
19667
  if (!repos.has(dir)) {
18590
19668
  const repo = repoAt(dir);
18591
19669
  repos.set(dir, repo && isWorkitWorkspace(repo) ? repo : null);
18592
19670
  }
18593
- const repo = repos.get(dir);
18594
- if (repo)
18595
- out.push({ action, repo });
19671
+ return repos.get(dir) ?? null;
19672
+ };
19673
+ for (const raw of rawInvocations(command, dialect)) {
19674
+ if (raw.dir === undefined)
19675
+ continue;
19676
+ const known = raw.tool !== "git" || GIT_COMMANDS.has(raw.args[0] ?? "");
19677
+ if (!known && !repoFor(raw.dir))
19678
+ continue;
19679
+ for (const invocation of known ? [raw] : expandGitAliases(cwd, raw, cache)) {
19680
+ if (invocation.dir === undefined)
19681
+ continue;
19682
+ const action = rawAction(invocation);
19683
+ const repo = action ? repoFor(invocation.dir) : null;
19684
+ if (action && repo)
19685
+ out.push({ action, repo, invocation });
19686
+ }
18596
19687
  }
18597
19688
  return out;
18598
19689
  };
@@ -18621,31 +19712,18 @@ var unseenNudges = (input, nudges, store) => {
18621
19712
  };
18622
19713
  function rawGitPre(input, command, options = {}) {
18623
19714
  try {
18624
- if (!mentionsRawTool(command))
18625
- return NONE2;
18626
19715
  const dialect = dialectOf(input);
18627
19716
  const nudges = new Map;
18628
19717
  let store = null;
18629
- for (const { action, repo } of actionsIn(input.cwd, command, dialect)) {
19718
+ const cache = new Map;
19719
+ for (const { action, repo, invocation } of actionsIn(input.cwd, command, dialect, cache)) {
18630
19720
  store ??= repo.location.dir;
18631
19721
  if (action.kind === "merge")
18632
19722
  return deny(refusal(action, null));
18633
- if (action.kind === "push" && toPushRemote(repo, action.remote)) {
18634
- if (action.mirror)
18635
- return deny(`workit: raw ${quote("git push --mirror")} rewrites and deletes every remote ref, protected branches and tags included. Push one branch with ${quote("workit git push")}.`);
18636
- for (const target of action.targets) {
18637
- const branch = target ?? repo.branch;
18638
- if (branch && guardedBranch(repo.dir, branch))
18639
- return deny(refusal(action, branch, repo.dir));
18640
- }
18641
- const remote = action.remote ?? pushRemoteName(repo.dir, repo.branch) ?? "origin";
18642
- const facts = refFacts(repo, remote);
18643
- const tagDeny = tagRefusal(action, facts);
18644
- if (tagDeny)
18645
- return deny(tagDeny);
18646
- const nameDeny = action.deletes ? null : offPolicyPush(action, repo, facts, remote);
18647
- if (nameDeny)
18648
- return deny(nameDeny);
19723
+ if (action.kind === "push") {
19724
+ const refused = pushRefusal(action, repo, gitConfig(repo.dir, invocation, cache));
19725
+ if (refused)
19726
+ return deny(refused);
18649
19727
  }
18650
19728
  const nudge = nudgeFor(action, input.host, input.session.id);
18651
19729
  if (nudge && !nudges.has(nudgeKind(action)))
@@ -18658,11 +19736,17 @@ function rawGitPre(input, command, options = {}) {
18658
19736
  const shown = unseenNudges(input, nudges, store);
18659
19737
  return shown.length ? { kind: "context", text: shown.join(`
18660
19738
  `) } : NONE2;
18661
- } catch {
18662
- return NONE2;
19739
+ } catch (error) {
19740
+ return error instanceof ShellAnalysisError ? analysisDenial(error) : NONE2;
18663
19741
  }
18664
19742
  }
18665
19743
  var deny = (reason) => ({ kind: "deny", reason, unblock: null });
19744
+ var SPLIT_IT = "Split it into simpler commands (no deeply nested substitutions or huge brace lists) and run them one at a time.";
19745
+ var analysisDenial = (error) => ({
19746
+ kind: "deny",
19747
+ reason: `workit: the shell hook could not analyze this command (${error.message}), so it does not run. ${SPLIT_IT}`,
19748
+ unblock: SPLIT_IT
19749
+ });
18666
19750
  var NEXT_COMMAND = "next";
18667
19751
  var MARKER_TTL_SECONDS = 3600;
18668
19752
  var markerFile = (repo, session, key) => path20.join(repo.location.dir, "hooks", `commit-${createHash14("sha256").update(`${session}\x00${key}`).digest("hex").slice(0, 32)}.json`);
@@ -18763,20 +19847,28 @@ var UNKNOWABLE = /[$`*?[\]{}~]/u;
18763
19847
  var SWITCH_CREATE = new Set(["-c", "--create", "-C", "--force-create", "--orphan"]);
18764
19848
  var CHECKOUT_CREATE = new Set(["-b", "-B", "--orphan"]);
18765
19849
  var WORKTREE_CREATE = new Set(["-b", "-B"]);
19850
+ var SUBTREE_CREATE = new Set(["-b", "--branch"]);
18766
19851
  var BRANCH_NO_CREATE = new Set(["-d", "-D", "--delete", "-l", "--list", "-a", "--all", "-r", "--remotes", "--show-current"].concat(["-v", "-vv", "--verbose", "--contains", "--no-contains", "--merged", "--no-merged"]).concat(["--points-at", "--sort", "--format", "--column", "--no-column", "-u"]).concat(["--set-upstream-to", "--unset-upstream", "--edit-description", "-i", "--ignore-case"]));
18767
19852
  var BRANCH_RENAME = new Set(["-m", "-M", "--move", "-c", "-C", "--copy"]);
19853
+ var longOption = (name, flags) => flags.has(name) || name.length >= 3 && [...flags].some((flag) => flag.startsWith("--") && flag.startsWith(name));
19854
+ var shortsOf = (flags) => [...flags].filter((flag) => /^-[^-]$/u.test(flag)).join("").replaceAll("-", "");
18768
19855
  var flagValue = (args, flags) => {
19856
+ const shorts = shortsOf(flags);
18769
19857
  for (let index = 1;index < args.length; index++) {
18770
19858
  const arg = args[index];
18771
19859
  if (arg === "--")
18772
19860
  return null;
18773
- if (flags.has(arg))
18774
- return args[index + 1] ?? null;
18775
- const eq = arg.indexOf("=");
18776
- if (arg.startsWith("--") && eq > 0 && flags.has(arg.slice(0, eq)))
18777
- return arg.slice(eq + 1);
18778
- if (!arg.startsWith("--") && arg.length > 2 && flags.has(arg.slice(0, 2)))
18779
- return arg.slice(2);
19861
+ if (arg.startsWith("--")) {
19862
+ const eq = arg.indexOf("=");
19863
+ if (!longOption(eq > 0 ? arg.slice(0, eq) : arg, flags))
19864
+ continue;
19865
+ return eq > 0 ? arg.slice(eq + 1) : args[index + 1] ?? null;
19866
+ }
19867
+ if (!arg.startsWith("-") || arg.length < 2)
19868
+ continue;
19869
+ for (let at = 1;at < arg.length; at++)
19870
+ if (shorts.includes(arg[at]))
19871
+ return at + 1 < arg.length ? arg.slice(at + 1) : args[index + 1] ?? null;
18780
19872
  }
18781
19873
  return null;
18782
19874
  };
@@ -18809,62 +19901,128 @@ var branchCommandTarget = (args) => {
18809
19901
  positionals.push(...args.slice(index + 1));
18810
19902
  break;
18811
19903
  }
18812
- const flag = arg.split("=", 1)[0];
18813
- if (BRANCH_NO_CREATE.has(flag))
18814
- return null;
18815
- if (BRANCH_RENAME.has(flag))
18816
- rename = true;
18817
- else if (!arg.startsWith("-"))
19904
+ if (arg.startsWith("--")) {
19905
+ const flag = arg.split("=", 1)[0];
19906
+ if (longOption(flag, BRANCH_NO_CREATE))
19907
+ return null;
19908
+ if (longOption(flag, BRANCH_RENAME))
19909
+ rename = true;
19910
+ } else if (arg.startsWith("-") && arg.length > 1) {
19911
+ const shorts = arg.slice(1).split("");
19912
+ if (shorts.some((short) => BRANCH_NO_CREATE.has(`-${short}`)))
19913
+ return null;
19914
+ if (shorts.some((short) => BRANCH_RENAME.has(`-${short}`)))
19915
+ rename = true;
19916
+ } else
18818
19917
  positionals.push(arg);
18819
19918
  }
18820
19919
  if (positionals.length < 1 || positionals.length > 2)
18821
19920
  return null;
18822
19921
  return rename ? positionals.at(-1) ?? null : positionals[0];
18823
19922
  };
18824
- var gitBranchCreation = (args) => {
18825
- const [sub, second] = args;
18826
- const target = sub === "switch" ? flagValue(args, SWITCH_CREATE) : sub === "checkout" ? flagValue(args, CHECKOUT_CREATE) : sub === "branch" ? branchCommandTarget(args) : sub === "worktree" && second === "add" ? worktreeTarget(args.slice(1)) : null;
18827
- return target && !target.startsWith("-") && !UNKNOWABLE.test(target) ? target : null;
19923
+ var positionalsOf = (args, skip, values = new Set) => {
19924
+ const found = [];
19925
+ for (let index = skip;index < args.length; index++) {
19926
+ const arg = args[index];
19927
+ if (arg === "--")
19928
+ return [...found, ...args.slice(index + 1)];
19929
+ if (values.has(arg))
19930
+ index++;
19931
+ else if (!arg.startsWith("-") || arg === "-")
19932
+ found.push(arg);
19933
+ }
19934
+ return found;
18828
19935
  };
18829
- var shellBranchTargets = (command, dialect = "posix") => {
18830
- if (!/\bgit\b/u.test(command))
19936
+ var localHead = (ref) => ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : null;
19937
+ var updateRefTargets = (args, stdin) => {
19938
+ if (args.some((arg) => arg === "-d" || arg === "--delete"))
18831
19939
  return [];
18832
- const found = [];
18833
- for (const invocation of rawInvocations(command, dialect)) {
18834
- if (invocation.tool !== "git")
19940
+ if (args.includes("--stdin"))
19941
+ return (stdin ?? "").split(/[\n\0]/u).map((line) => /^\s*(?:create|update)\s+(\S+)/u.exec(line)?.[1] ?? "").map(localHead).filter((name) => name !== null);
19942
+ const [ref] = positionalsOf(args, 1, new Set(["-m"]));
19943
+ const name = ref ? localHead(ref) : null;
19944
+ return name ? [name] : [];
19945
+ };
19946
+ var symbolicRefTarget = (args) => {
19947
+ if (args.some((arg) => arg === "-d" || arg === "--delete"))
19948
+ return null;
19949
+ const [, ref] = positionalsOf(args, 1, new Set(["-m"]));
19950
+ return ref ? localHead(ref) : null;
19951
+ };
19952
+ var FETCH_VALUES = new Set(["--depth", "--deepen", "--shallow-since", "--shallow-exclude", "--refmap", "-j", "--jobs"].concat(["--upload-pack", "-o", "--server-option", "--negotiation-tip", "--filter"]).concat(["--recurse-submodules-default", "--submodule-prefix", "-s", "--strategy", "-X"]).concat(["--strategy-option"]));
19953
+ var fetchTargets = (args, stdin) => {
19954
+ if (args.includes("--all") || args.includes("--multiple"))
19955
+ return [];
19956
+ const specs = args.includes("--stdin") ? (stdin ?? "").split(/\s+/u).filter(Boolean) : positionalsOf(args, 1, FETCH_VALUES).slice(1);
19957
+ const names = [];
19958
+ for (const raw of specs) {
19959
+ const colon = raw.indexOf(":");
19960
+ const destination = colon >= 0 ? raw.slice(colon + 1) : "";
19961
+ if (!destination || destination.includes("*"))
18835
19962
  continue;
18836
- const branch = gitBranchCreation(invocation.args);
18837
- if (branch)
18838
- found.push({ branch, dir: invocation.dir });
19963
+ const name = destination.startsWith("refs/") ? localHead(destination) : destination;
19964
+ if (name)
19965
+ names.push(name);
18839
19966
  }
18840
- return found;
19967
+ return names;
19968
+ };
19969
+ var localPushTargets = (args) => {
19970
+ const action = pushAction(args);
19971
+ if (action?.kind !== "push" || action.remote !== "." || action.deletes)
19972
+ return [];
19973
+ return action.targets.filter((name) => name !== null);
19974
+ };
19975
+ var gitBranchCreations = (args, stdin) => {
19976
+ const [sub, second] = args;
19977
+ const targets = sub === "switch" ? [flagValue(args, SWITCH_CREATE)] : sub === "checkout" ? [flagValue(args, CHECKOUT_CREATE)] : sub === "branch" ? [branchCommandTarget(args)] : sub === "worktree" && second === "add" ? [worktreeTarget(args.slice(1))] : sub === "stash" && second === "branch" ? positionalsOf(args, 2).slice(0, 1) : sub === "update-ref" ? updateRefTargets(args, stdin) : sub === "symbolic-ref" ? [symbolicRefTarget(args)] : sub === "fetch" || sub === "pull" ? fetchTargets(args, stdin) : sub === "push" ? localPushTargets(args) : sub === "subtree" && second === "split" ? [flagValue(args.slice(1), SUBTREE_CREATE)] : [];
19978
+ return targets.filter((target) => target !== null && target !== "" && !target.startsWith("-") && !UNKNOWABLE.test(target));
18841
19979
  };
19980
+ var targetsOf = (invocations) => invocations.flatMap((invocation) => invocation.tool === "git" ? gitBranchCreations(invocation.args, invocation.stdin).map((branch) => ({
19981
+ branch,
19982
+ dir: invocation.dir
19983
+ })) : []);
18842
19984
  var localBranchExists = (dir, branch) => spawnSync12("git", ["show-ref", "--verify", "--quiet", `refs/heads/${branch}`], {
18843
19985
  cwd: dir,
18844
19986
  stdio: "ignore",
18845
19987
  timeout: 5000
18846
19988
  }).status === 0;
19989
+ var judgedDir = (root, dir) => {
19990
+ if (dir === undefined)
19991
+ return null;
19992
+ if (dir === null)
19993
+ return root;
19994
+ const resolved = path21.resolve(root, dir);
19995
+ return existsSync6(resolved) && inWorkitWorkspace(resolved) ? resolved : null;
19996
+ };
18847
19997
  var shellBranchPolicyViolation = (root, command, dialect = "posix") => {
18848
19998
  let judged = false;
18849
- for (const target of shellBranchTargets(command, dialect)) {
18850
- if (target.dir === undefined)
18851
- continue;
18852
- const dir = target.dir === null ? root : path21.resolve(root, target.dir);
18853
- if (target.dir !== null && (!existsSync6(dir) || !inWorkitWorkspace(dir)))
19999
+ const cache = new Map;
20000
+ for (const raw of rawInvocations(command, dialect)) {
20001
+ if (raw.tool !== "git" || judgedDir(root, raw.dir) === null)
18854
20002
  continue;
18855
- if (localBranchExists(dir, target.branch))
18856
- continue;
18857
- judged = true;
18858
- const check = validateBranchNameFor(dir, target.branch);
18859
- if (!check.ok)
18860
- return check;
20003
+ for (const target of targetsOf(expandGitAliases(root, raw, cache))) {
20004
+ const dir = judgedDir(root, target.dir);
20005
+ if (dir === null || localBranchExists(dir, target.branch))
20006
+ continue;
20007
+ judged = true;
20008
+ const check = validateBranchNameFor(dir, target.branch);
20009
+ if (!check.ok)
20010
+ return check;
20011
+ }
18861
20012
  }
18862
20013
  return judged ? { ok: true } : null;
18863
20014
  };
18864
20015
 
18865
20016
  // packages/workit-core/src/hooks/policy.ts
18866
20017
  var shellPolicy = (cwd, command, dialect = "posix") => {
18867
- const policy = shellBranchPolicyViolation(cwd, command, dialect);
20018
+ let policy;
20019
+ try {
20020
+ policy = shellBranchPolicyViolation(cwd, command, dialect);
20021
+ } catch (error) {
20022
+ if (error instanceof ShellAnalysisError)
20023
+ return analysisDenial(error);
20024
+ throw error;
20025
+ }
18868
20026
  return policy && !policy.ok ? { kind: "deny", reason: `branch_policy_denied: ${policy.error}`, unblock: null } : { kind: "none" };
18869
20027
  };
18870
20028
  // packages/workit-core/src/hooks/write-gate.ts
@@ -18893,11 +20051,11 @@ var exempt = (cwd, root, file, plans) => {
18893
20051
  const segments = posix.split("/");
18894
20052
  return /\.mdx?$/i.test(posix) || segments[0] === "docs" || segments.includes("plans") || [...plans].some((plan) => fold(plan) === posix);
18895
20053
  };
18896
- var PREFIXES3 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs"]);
20054
+ var PREFIXES2 = new Set(["sudo", "command", "env", "nohup", "time", "exec", "xargs"]);
18897
20055
  var operands = (words) => words.filter((item) => !item.startsWith("-"));
18898
20056
  var editsOf = (input) => {
18899
20057
  let words = input;
18900
- while (words.length && (PREFIXES3.has(words[0]) || /^\w+=/.test(words[0])))
20058
+ while (words.length && (PREFIXES2.has(words[0]) || /^\w+=/.test(words[0])))
18901
20059
  words = words.slice(1);
18902
20060
  const [verb, ...args] = words;
18903
20061
  const name = (verb?.split(/[\\/]/).at(-1) ?? "").toLowerCase();
@@ -20194,7 +21352,7 @@ var CURSOR_DESCRIPTOR = {
20194
21352
  "compact.pre": { support: "partial", native: "preCompact" },
20195
21353
  stop: { support: "native", native: "stop" }
20196
21354
  },
20197
- shellPolicy: { deny: "native", channel: "exit2+json", failClosed: true },
21355
+ shellPolicy: { deny: "native", channel: "exit2+json", failClosed: false },
20198
21356
  context: {
20199
21357
  sessionStart: "native",
20200
21358
  perTurn: "none",