diff --git a/src/gitlab.ts b/src/gitlab.ts index 307bd9b..c526667 100644 --- a/src/gitlab.ts +++ b/src/gitlab.ts @@ -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) { diff --git a/test/checkout.test.ts b/test/checkout.test.ts new file mode 100644 index 0000000..17477dd --- /dev/null +++ b/test/checkout.test.ts @@ -0,0 +1,42 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { mkdtempSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { checkout, type MrInfo } from '../src/gitlab.ts'; + +const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...args], { cwd, encoding: 'utf8' }).trim(); + +test('an empty checkout folder inside another repository never touches that repository', async () => { + delete process.env.GITLAB_TOKEN; + const root = mkdtempSync(path.join(os.tmpdir(), 'labuh-co-')); + // The reviewed project, with the MR head under GitLab's ref name. + const src = path.join(root, 'src'); + mkdirSync(src); + git(src, 'init', '-q'); + writeFileSync(path.join(src, 'app.txt'), 'reviewed\n'); + git(src, 'add', '.'); + git(src, 'commit', '-qm', 'mr'); + const sha = git(src, 'rev-parse', 'HEAD'); + git(src, 'update-ref', 'refs/merge-requests/7/head', sha); + // LABUH_HOME is itself a repository, and the work folder already has an empty repo/. + const home = path.join(root, 'home'); + mkdirSync(home); + git(home, 'init', '-q'); + writeFileSync(path.join(home, 'own.txt'), 'labuh\n'); + git(home, 'add', '.'); + git(home, 'commit', '-qm', 'home'); + const homeHead = git(home, 'rev-parse', 'HEAD'); + const dir = path.join(home, '.work', 'p-7', 'repo'); + mkdirSync(dir, { recursive: true }); + + await checkout({ iid: 7, sha, sshUrl: src, httpUrl: src } as MrInfo, dir); + + assert.equal(git(dir, 'rev-parse', '--show-toplevel'), realpathSync(dir), 'the checkout is its own repository'); + assert.equal(git(dir, 'rev-parse', 'HEAD'), sha); + assert.equal(readFileSync(path.join(dir, 'app.txt'), 'utf8'), 'reviewed\n'); + assert.equal(git(home, 'rev-parse', 'HEAD'), homeHead, 'the outer repository keeps its HEAD'); + assert.equal(git(home, 'remote'), '', 'the outer repository gets no remote'); + assert.equal(readFileSync(path.join(home, 'own.txt'), 'utf8'), 'labuh\n'); +});