Keep git inside the checkout folder so an empty work dir cannot hijack the outer repo.

An empty .work/<mr>/repo inside a git checkout made rev-parse find the outer repository, so set-url, fetch and checkout --force ran there; GIT_CEILING_DIRECTORIES pins git to the folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 12:28:47 +07:00
parent 37e9e29c9a
commit 71962e853f
2 changed files with 47 additions and 1 deletions

View File

@@ -132,7 +132,11 @@ function gitAccess(mr: MrInfo): { url: string; env: NodeJS.ProcessEnv } {
export async function checkout(mr: MrInfo, dir: string) {
const access = gitAccess(mr);
const git = (...args: string[]) => run('git', args, { cwd: dir, env: access.env, maxBuffer: 16 * 1024 * 1024 });
// Git must never walk up from the checkout: an empty folder inside another repository (a
// LABUH_HOME that is itself a git checkout) would otherwise send init, set-url and
// `checkout --force` to that outer repository.
const env = { ...access.env, GIT_CEILING_DIRECTORIES: path.dirname(path.resolve(dir)) };
const git = (...args: string[]) => run('git', args, { cwd: dir, env, maxBuffer: 16 * 1024 * 1024 });
try {
const { stdout } = await git('rev-parse', 'HEAD');
if (stdout.trim() === mr.sha) {