Skip to content

Commit c17ffad

Browse files
committed
refactor: do not check local .githooks dir
Only check ".husky/_" and ".husky" directories, as they are used by the popular Git hooks tool Husky. Do not check ".githooks", as this directory is not standardized or used by any widely-adopted tool. Change-Id: I0044d890330619980331cbbfb716cfd2c1101e99 Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
1 parent 561fa96 commit c17ffad

2 files changed

Lines changed: 2 additions & 213 deletions

File tree

src/commands/install.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getHooksDir(gitDir: string): string {
7878
}
7979

8080
/**
81-
* Check for existing hooks directories (.husky or .githooks) and return appropriate path
81+
* Check for existing hooks directories (.husky/_ or .husky) and return appropriate path
8282
* @param gitDir The Git directory path
8383
* @returns The hooks directory path
8484
*/
@@ -101,7 +101,7 @@ function checkForExistingHooksDir(gitDir: string): string {
101101
const workDirRoot = workDirRootResult.stdout.trim();
102102

103103
// Define hooks directories to check in order of preference
104-
const hooksDirectories = ['.husky/_', '.husky', '.githooks'];
104+
const hooksDirectories = ['.husky/_', '.husky'];
105105

106106
// Loop through each hooks directory and check if it exists
107107
for (const hooksDir of hooksDirectories) {

test/commands/install.test.ts

Lines changed: 0 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,6 @@ describe('checkForExistingHooksDir function', () => {
594594
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
595595
if (filePath === expectedHuskyPath) return true;
596596
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
597-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
598597
return false;
599598
}) as FsExistsSyncMock);
600599

@@ -662,7 +661,6 @@ describe('checkForExistingHooksDir function', () => {
662661
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
663662
if (filePath === path.join(mockWorkDirRoot, '.husky/_')) return false;
664663
if (filePath === expectedHuskyPath) return true;
665-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
666664
return false;
667665
}) as FsExistsSyncMock);
668666

@@ -677,74 +675,6 @@ describe('checkForExistingHooksDir function', () => {
677675
expect(hooksDir).toBe(expectedHuskyPath);
678676
});
679677

680-
it('should return .githooks directory and set core.hooksPath when .githooks directory exists', () => {
681-
const expectedGithooksPath = path.join(mockWorkDirRoot, '.githooks');
682-
683-
// Mock git commands
684-
vi.mocked(spawnSync).mockImplementation(
685-
(command: string, args: string[]) => {
686-
if (
687-
command === 'git' &&
688-
args.includes('rev-parse') &&
689-
args.includes('--show-toplevel')
690-
) {
691-
return {
692-
stdout: mockWorkDirRoot,
693-
stderr: '',
694-
status: 0,
695-
error: undefined,
696-
pid: 12345,
697-
output: [null, mockWorkDirRoot, ''],
698-
signal: null,
699-
} as SpawnSyncReturns<string>;
700-
}
701-
if (
702-
command === 'git' &&
703-
args.includes('config') &&
704-
args.includes('core.hooksPath')
705-
) {
706-
// This is the call to set core.hooksPath
707-
return {
708-
stdout: '',
709-
stderr: '',
710-
status: 0,
711-
error: undefined,
712-
pid: 12345,
713-
output: [null, '', ''],
714-
signal: null,
715-
} as SpawnSyncReturns<string>;
716-
}
717-
return {
718-
stdout: '',
719-
stderr: '',
720-
status: 0,
721-
error: undefined,
722-
pid: 12345,
723-
output: [null, '', ''],
724-
signal: null,
725-
} as SpawnSyncReturns<string>;
726-
}
727-
);
728-
729-
// Mock fs.existsSync and fs.statSync
730-
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
731-
if (filePath === path.join(mockWorkDirRoot, '.husky/_')) return false;
732-
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
733-
if (filePath === expectedGithooksPath) return true;
734-
return false;
735-
}) as FsExistsSyncMock);
736-
737-
vi.mocked(fs.statSync).mockImplementation((filePath: fs.PathLike) => {
738-
if (filePath === expectedGithooksPath) {
739-
return { isDirectory: () => true } as fs.Stats;
740-
}
741-
return { isDirectory: () => false } as fs.Stats;
742-
});
743-
744-
const hooksDir = checkForExistingHooksDir(mockGitDir);
745-
expect(hooksDir).toBe(expectedGithooksPath);
746-
});
747-
748678
it('should return default hooks directory when no existing directories found', () => {
749679
// Mock git commands
750680
vi.mocked(spawnSync).mockImplementation(
@@ -780,7 +710,6 @@ describe('checkForExistingHooksDir function', () => {
780710
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
781711
if (filePath === path.join(mockWorkDirRoot, '.husky/_')) return false;
782712
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
783-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
784713
return false;
785714
}) as FsExistsSyncMock);
786715

@@ -892,7 +821,6 @@ describe('getHooksDir function', () => {
892821
// Mock fs.existsSync to return false for both directories
893822
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
894823
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
895-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
896824
return false;
897825
}) as FsExistsSyncMock);
898826

@@ -955,7 +883,6 @@ describe('getHooksDir function', () => {
955883
// Mock fs.existsSync and fs.statSync
956884
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
957885
if (filePath === expectedHuskyPath) return true;
958-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
959886
return false;
960887
}) as FsExistsSyncMock);
961888

@@ -970,143 +897,6 @@ describe('getHooksDir function', () => {
970897
expect(hooksDir).toBe(expectedHuskyPath);
971898
});
972899

973-
it('should return .githooks directory and set core.hooksPath when .githooks directory exists', () => {
974-
const mockWorkDirRoot = '/path/to/repo';
975-
const expectedGithooksPath = path.join(mockWorkDirRoot, '.githooks');
976-
977-
// Mock git commands
978-
vi.mocked(spawnSync).mockImplementation(
979-
(command: string, args: string[]) => {
980-
if (
981-
command === 'git' &&
982-
args.includes('config') &&
983-
args.includes('core.hooksPath')
984-
) {
985-
return {
986-
stdout: '',
987-
stderr: 'core.hooksPath not set',
988-
status: 1,
989-
error: new Error('core.hooksPath not set'),
990-
pid: 12345,
991-
output: [null, '', 'core.hooksPath not set'],
992-
signal: null,
993-
} as SpawnSyncReturns<string>;
994-
}
995-
if (
996-
command === 'git' &&
997-
args.includes('rev-parse') &&
998-
args.includes('--show-toplevel')
999-
) {
1000-
return {
1001-
stdout: mockWorkDirRoot,
1002-
stderr: '',
1003-
status: 0,
1004-
error: undefined,
1005-
pid: 12345,
1006-
output: [null, mockWorkDirRoot, ''],
1007-
signal: null,
1008-
} as SpawnSyncReturns<string>;
1009-
}
1010-
return {
1011-
stdout: '',
1012-
stderr: '',
1013-
status: 0,
1014-
error: undefined,
1015-
pid: 12345,
1016-
output: [null, '', ''],
1017-
signal: null,
1018-
} as SpawnSyncReturns<string>;
1019-
}
1020-
);
1021-
1022-
// Mock fs.existsSync and fs.statSync
1023-
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
1024-
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
1025-
if (filePath === expectedGithooksPath) return true;
1026-
return false;
1027-
}) as FsExistsSyncMock);
1028-
1029-
vi.mocked(fs.statSync).mockImplementation((filePath: fs.PathLike) => {
1030-
if (filePath === expectedGithooksPath) {
1031-
return { isDirectory: () => true } as fs.Stats;
1032-
}
1033-
return { isDirectory: () => false } as fs.Stats;
1034-
});
1035-
1036-
const hooksDir = getHooksDir(mockGitDir);
1037-
expect(hooksDir).toBe(expectedGithooksPath);
1038-
});
1039-
1040-
it('should prefer .husky directory over .githooks when both exist', () => {
1041-
const mockWorkDirRoot = '/path/to/repo';
1042-
const expectedHuskyPath = path.join(mockWorkDirRoot, '.husky');
1043-
1044-
// Mock git commands
1045-
vi.mocked(spawnSync).mockImplementation(
1046-
(command: string, args: string[]) => {
1047-
if (
1048-
command === 'git' &&
1049-
args.includes('config') &&
1050-
args.includes('core.hooksPath')
1051-
) {
1052-
return {
1053-
stdout: '',
1054-
stderr: 'core.hooksPath not set',
1055-
status: 1,
1056-
error: new Error('core.hooksPath not set'),
1057-
pid: 12345,
1058-
output: [null, '', 'core.hooksPath not set'],
1059-
signal: null,
1060-
} as SpawnSyncReturns<string>;
1061-
}
1062-
if (
1063-
command === 'git' &&
1064-
args.includes('rev-parse') &&
1065-
args.includes('--show-toplevel')
1066-
) {
1067-
return {
1068-
stdout: mockWorkDirRoot,
1069-
stderr: '',
1070-
status: 0,
1071-
error: undefined,
1072-
pid: 12345,
1073-
output: [null, mockWorkDirRoot, ''],
1074-
signal: null,
1075-
} as SpawnSyncReturns<string>;
1076-
}
1077-
return {
1078-
stdout: '',
1079-
stderr: '',
1080-
status: 0,
1081-
error: undefined,
1082-
pid: 12345,
1083-
output: [null, '', ''],
1084-
signal: null,
1085-
} as SpawnSyncReturns<string>;
1086-
}
1087-
);
1088-
1089-
// Mock fs.existsSync and fs.statSync
1090-
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
1091-
if (filePath === expectedHuskyPath) return true;
1092-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return true;
1093-
return false;
1094-
}) as FsExistsSyncMock);
1095-
1096-
vi.mocked(fs.statSync).mockImplementation((filePath: fs.PathLike) => {
1097-
if (
1098-
filePath === expectedHuskyPath ||
1099-
filePath === path.join(mockWorkDirRoot, '.githooks')
1100-
) {
1101-
return { isDirectory: () => true } as fs.Stats;
1102-
}
1103-
return { isDirectory: () => false } as fs.Stats;
1104-
});
1105-
1106-
const hooksDir = getHooksDir(mockGitDir);
1107-
expect(hooksDir).toBe(expectedHuskyPath);
1108-
});
1109-
1110900
it('should return custom hooks directory when core.hooksPath is set (relative path)', () => {
1111901
const mockWorkDirRoot = '/path/to/repo';
1112902

@@ -1247,7 +1037,6 @@ describe('getHooksDir function', () => {
12471037
// Mock fs.existsSync to return false for both directories
12481038
vi.mocked(fs.existsSync).mockImplementation(((filePath: fs.PathLike) => {
12491039
if (filePath === path.join(mockWorkDirRoot, '.husky')) return false;
1250-
if (filePath === path.join(mockWorkDirRoot, '.githooks')) return false;
12511040
return false;
12521041
}) as FsExistsSyncMock);
12531042

0 commit comments

Comments
 (0)