Skip to content

Commit caa21bd

Browse files
committed
Ignore GIT_* environment when reading the local build from git
A git hook runs with GIT_DIR set, and with it git treats any directory as the top of that repository, so gitBuild() reported a commit for directories outside a checkout and the build test failed under the pre-push hook. Run git with every GIT_* variable removed so discovery depends only on the directory, and test the GIT_DIR case.
1 parent e75e811 commit caa21bd

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/hosted/build.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { describe, it, expect } from 'vitest';
1+
import { describe, it, expect, vi } from 'vitest';
2+
import { execFileSync } from 'node:child_process';
23
import { mkdtempSync } from 'node:fs';
34
import { tmpdir } from 'node:os';
45
import { join } from 'node:path';
@@ -41,4 +42,20 @@ describe('server build', () => {
4142
expect(gitBuild(__dirname)).toBeUndefined();
4243
expect(gitBuild(mkdtempSync(join(tmpdir(), 'no-git-')))).toBeUndefined();
4344
});
45+
46+
it('ignores GIT_DIR in the environment, as a git hook sets it', () => {
47+
const root = packageRoot(__dirname)!;
48+
const gitDir = execFileSync('git', ['rev-parse', '--absolute-git-dir'], {
49+
cwd: root,
50+
encoding: 'utf8'
51+
}).trim();
52+
vi.stubEnv('GIT_DIR', gitDir);
53+
try {
54+
expect(gitBuild(root)).toMatch(/^[0-9a-f]{7,40}(-dirty)?$/);
55+
expect(gitBuild(__dirname)).toBeUndefined();
56+
expect(gitBuild(mkdtempSync(join(tmpdir(), 'no-git-')))).toBeUndefined();
57+
} finally {
58+
vi.unstubAllEnvs();
59+
}
60+
});
4461
});

‎src/hosted/git-build.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ import { dirname, join } from 'node:path';
1111

1212
const PACKAGE_NAME = '@modelcontextprotocol/conformance';
1313

14+
/**
15+
* The environment without git's own GIT_* variables. A git hook runs with
16+
* GIT_DIR (and others) set, and with GIT_DIR set git treats any directory
17+
* as the top of that repository; without them, discovery depends only on
18+
* the directory git runs in.
19+
*/
20+
function gitEnv(): NodeJS.ProcessEnv {
21+
return Object.fromEntries(
22+
Object.entries(process.env).filter(
23+
([name]) => !name.toUpperCase().startsWith('GIT_')
24+
)
25+
);
26+
}
27+
1428
function git(dir: string, args: string[]): string {
1529
return execFileSync('git', args, {
1630
cwd: dir,
31+
env: gitEnv(),
1732
encoding: 'utf8',
1833
stdio: ['ignore', 'pipe', 'ignore'],
1934
timeout: 2000

0 commit comments

Comments
 (0)