mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-07 19:06:11 +00:00
Ignore Cursor's Using worktree stdout banner without masking other non-JSON ACP frames (markClosed + kill). Skip --cursor-worktree when spawn directory is already a linked git worktree so ACP can initialize. Fixes #1085 Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { execFileSync } from 'node:child_process';
|
|
import { realpathSync } from 'node:fs';
|
|
import { isAbsolute, resolve } from 'node:path';
|
|
|
|
/**
|
|
* True when `directory` is a linked Git worktree (`.git` is a file pointing at
|
|
* the common object store), not the repository's primary working tree.
|
|
*
|
|
* Cursor `agent --worktree` nested inside an existing linked worktree hangs ACP
|
|
* initialize (see tiann/hapi#1085). HAPI skips `--cursor-worktree` in that case
|
|
* and runs in the given directory instead.
|
|
*/
|
|
export function isLinkedGitWorktree(directory: string): boolean {
|
|
try {
|
|
const isInside = runGit(['rev-parse', '--is-inside-work-tree'], directory);
|
|
if (isInside !== 'true') {
|
|
return false;
|
|
}
|
|
|
|
const gitDir = runGit(['rev-parse', '--git-dir'], directory);
|
|
const gitCommonDir = runGit(['rev-parse', '--git-common-dir'], directory);
|
|
if (!gitDir || !gitCommonDir) {
|
|
return false;
|
|
}
|
|
|
|
return normalizePath(gitDir, directory) !== normalizePath(gitCommonDir, directory);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function runGit(args: string[], cwd: string): string | null {
|
|
try {
|
|
const output = execFileSync('git', args, {
|
|
cwd,
|
|
encoding: 'utf8',
|
|
stdio: ['ignore', 'pipe', 'ignore']
|
|
}).trim();
|
|
return output.length > 0 ? output : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function normalizePath(rawPath: string, cwd: string): string {
|
|
const resolved = isAbsolute(rawPath) ? rawPath : resolve(cwd, rawPath);
|
|
try {
|
|
return realpathSync(resolved);
|
|
} catch {
|
|
return resolved;
|
|
}
|
|
}
|