import { execFileSync } from 'node:child_process'; import { existsSync } from 'node:fs'; import { resolve } from 'node:path'; /** Literal argv inspection, not a shell interpreter or security sandbox. */ export function gitArgumentRisk(segment: string, cwd: string): 'git-checkout-path' | 'git-amend-or-bypass-hooks' | null { const words = (segment.match(/"[^"\r\n]*"|'[^'\r\n]*'|[^\s]+/g) ?? []) .map(word => /^(["']).*\1$/.test(word) ? word.slice(1, -1) : word); const start = words.findIndex(word => /(?:^|[\\/])git(?:\.exe)?$/i.test(word)); if (start < 0) return null; let directory = cwd; let signingDisabled = false; let customRepository = false; let index = start + 1; for (; index < words.length; index++) { const word = words[index]; if (!word.startsWith('-')) break; if (word === '-C') directory = resolve(directory, words[++index] ?? ''); else if (word.startsWith('-C')) directory = resolve(directory, word.slice(2)); else if (word === '-c' || word.startsWith('-c')) { const config = word === '-c' ? words[++index] ?? '' : word.slice(2); if (/^(?:commit|tag)\.gpgsign=(?:false|no|off|0)$/i.test(config) || /^core\.hookspath=/i.test(config)) signingDisabled = true; } else if (/^--(?:git-dir|work-tree)(?:=|$)/.test(word)) { customRepository = true; if (!word.includes('=')) index++; } else if (word === '--no-pager' || word === '--paginate' || word === '--literal-pathspecs') continue; else return null; } const subcommand = words[index++]; const args = words.slice(index); if (['commit', 'push', 'merge', 'tag'].includes(subcommand)) { if (signingDisabled) return 'git-amend-or-bypass-hooks'; for (let i = 0; i < args.length; i++) { const arg = args[i]; if (arg === '--') break; if (['-m', '-F', '--message', '--file', '-C', '-c', '--reuse-message', '--reedit-message'].includes(arg)) { i++; continue; } if (['--amend', '--no-verify', '--no-gpg-sign'].includes(arg) || (subcommand === 'commit' && /^-[^-]/.test(arg) && arg.slice(1).split(/[mFCcS]/, 1)[0].includes('n'))) return 'git-amend-or-bypass-hooks'; } } if (subcommand !== 'checkout') return null; if (args.includes('--help') || args.includes('-h')) return null; if (args.includes('--') || args.some(arg => ['--force', '-f', '--patch', '-p'].includes(arg) || arg.startsWith('-B'))) return 'git-checkout-path'; // Explicit creation has unambiguous branch semantics. -B above resets a ref. if (args.some(arg => arg === '-b' || /^-b.+/.test(arg) || arg === '--orphan' || arg.startsWith('--orphan='))) return null; const operands: string[] = []; for (let i = 0; i < args.length; i++) { if (args[i] === '--conflict') { i++; continue; } if (!args[i].startsWith('-') || args[i] === '-') operands.push(args[i]); } if (operands.length > 1) return 'git-checkout-path'; if (!operands.length || operands[0] === '-') return null; const target = operands[0]; // A slash is not evidence of a file. Resolve genuine branch/path ambiguity // against local refs using read-only argv calls, never shell interpolation. if (!customRepository && !/[\x00-\x1f]/.test(target)) { try { execFileSync('git', ['-C', directory, 'rev-parse', '--verify', '--quiet', `refs/heads/${target}`], { stdio: 'pipe', timeout: 1500, windowsHide: true }); return null; } catch { /* Not a local branch; examine the path next. */ } try { const tracked = execFileSync('git', ['-C', directory, 'ls-files', '--', target], { encoding: 'utf8', stdio: 'pipe', timeout: 1500, windowsHide: true, maxBuffer: 65536 }); if (tracked.trim()) return 'git-checkout-path'; } catch { /* Unavailable repository: conservatively check literal paths. */ } } return customRepository || existsSync(resolve(directory, target)) || /(?:^\.$|[\\/]$|\.[a-z0-9]+$)/i.test(target) ? 'git-checkout-path' : null; }