Skip to content

Commit 91916b6

Browse files
committed
fix: diagnose invalid git hooks paths
1 parent fe88ce9 commit 91916b6

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎src/doctor.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ function inspectGitHooks(root: string): DoctorIssue[] {
298298
];
299299
}
300300

301+
if (!isDirectoryPath(hookDir)) {
302+
return [
303+
createIssue(
304+
'git-hooks-not-directory',
305+
'git-hooks',
306+
'warning',
307+
'Git hooks are configured, but the configured hooks path is not a directory.',
308+
relative(root, hookDir),
309+
`Replace \`${relative(root, hookDir)}\` with a directory or update \`core.hooksPath\` to a valid hooks directory.`,
310+
),
311+
];
312+
}
313+
301314
if (hasInstalledHook(hookDir)) {
302315
return [];
303316
}
@@ -316,6 +329,19 @@ function inspectGitHooks(root: string): DoctorIssue[] {
316329

317330
const defaultHooksPath = runGit(['rev-parse', '--git-path', 'hooks'], root);
318331
const hookDir = resolve(root, defaultHooksPath);
332+
if (existsSync(hookDir) && !isDirectoryPath(hookDir)) {
333+
return [
334+
createIssue(
335+
'git-hooks-not-directory',
336+
'git-hooks',
337+
'warning',
338+
'The default git hooks path exists, but it is not a directory.',
339+
relative(root, hookDir),
340+
`Replace \`${relative(root, hookDir)}\` with a directory before relying on default git hooks.`,
341+
),
342+
];
343+
}
344+
319345
if (hasInstalledHook(hookDir)) {
320346
return [];
321347
}
@@ -446,7 +472,7 @@ function collectMarkdownFiles(root: string, maxDepth = 10): string[] {
446472
}
447473

448474
function hasInstalledHook(directory: string): boolean {
449-
if (!existsSync(directory)) {
475+
if (!isDirectoryPath(directory)) {
450476
return false;
451477
}
452478

‎tests/doctor.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,22 @@ describe('doctor engine', () => {
154154
code: 'git-hooks-unavailable',
155155
}));
156156
});
157+
158+
it('reports a configured hooks path that exists as a file without collapsing to git-hooks-unavailable.', () => {
159+
const root = createTempRoot();
160+
initWorkspace(root);
161+
execFileSync('git', ['init'], { cwd: root, stdio: 'ignore' });
162+
writeFileSync(join(root, 'hooks-file'), 'not a directory\n', 'utf8');
163+
execFileSync('git', ['config', 'core.hooksPath', 'hooks-file'], { cwd: root, stdio: 'ignore' });
164+
165+
const report = runDoctor(root);
166+
167+
expect(report.issues).toContainEqual(expect.objectContaining({
168+
code: 'git-hooks-not-directory',
169+
path: 'hooks-file',
170+
}));
171+
expect(report.issues).not.toContainEqual(expect.objectContaining({
172+
code: 'git-hooks-unavailable',
173+
}));
174+
});
157175
});

0 commit comments

Comments
 (0)