Skip to content

Commit 680bb38

Browse files
authored
fix(setup): avoid redundant hooks config writes (#209)
1 parent 09e5b62 commit 680bb38

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

packages/rstack/src/setup/install.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ export const installHooks = ({
126126

127127
const directory = path.join(cwd, resolvedDir, '_');
128128
const files = Object.entries(createHookFiles());
129+
const hooksPathMatches = path.resolve(cwd, configuredHooksPath) === directory;
129130
// Skip all writes only when the config, generated content, and executable modes match.
130131
const unchanged =
131-
path.resolve(cwd, configuredHooksPath) === directory &&
132+
hooksPathMatches &&
132133
isCurrentFile(path.join(directory, '.gitignore'), gitignore) &&
133134
files.every(([name, content]) => isCurrentFile(path.join(directory, name), content, true));
134135

@@ -151,6 +152,11 @@ export const installHooks = ({
151152
return fail('write-failed', `Failed to write Git hook files: ${message}`);
152153
}
153154

155+
// Avoid rewriting .git/config when only the generated files needed repair.
156+
if (hooksPathMatches) {
157+
return { status: 'installed', hooksPath };
158+
}
159+
154160
// Point Git at the generated directory only after every runtime file is ready.
155161
const configured = runGit(cwd, ['config', '--local', 'core.hooksPath', hooksPath]);
156162
if (configured.error || configured.status === null) {

packages/rstack/tests/setup/install.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ test.runIf(process.platform !== 'win32')('restores executable mode on existing s
5050
});
5151
});
5252

53+
test('repairs generated files without rewriting an unchanged hooksPath', () => {
54+
withRepository((cwd) => {
55+
expect(installHooks({ cwd }).status).toBe('installed');
56+
const runner = path.join(cwd, hooksPath, 'runner');
57+
writeFileSync(runner, 'stale\n');
58+
writeFileSync(path.join(cwd, '.git', 'config.lock'), 'locked');
59+
60+
expect(installHooks({ cwd })).toEqual({ status: 'installed', hooksPath });
61+
expect(readFileSync(runner, 'utf8')).toBe(createHookFiles().runner);
62+
});
63+
});
64+
5365
test('skips non-Git directories without creating files', () => {
5466
withDirectory((cwd) => {
5567
expect(installHooks({ cwd })).toEqual({

0 commit comments

Comments
 (0)