Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/rstack/src/setup/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ export const installHooks = ({

const directory = path.join(cwd, resolvedDir, '_');
const files = Object.entries(createHookFiles());
const hooksPathMatches = path.resolve(cwd, configuredHooksPath) === directory;
// Skip all writes only when the config, generated content, and executable modes match.
const unchanged =
path.resolve(cwd, configuredHooksPath) === directory &&
hooksPathMatches &&
isCurrentFile(path.join(directory, '.gitignore'), gitignore) &&
files.every(([name, content]) => isCurrentFile(path.join(directory, name), content, true));

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

// Avoid rewriting .git/config when only the generated files needed repair.
if (hooksPathMatches) {
return { status: 'installed', hooksPath };
Comment thread
chenjiahan marked this conversation as resolved.
}

// Point Git at the generated directory only after every runtime file is ready.
const configured = runGit(cwd, ['config', '--local', 'core.hooksPath', hooksPath]);
if (configured.error || configured.status === null) {
Expand Down
12 changes: 12 additions & 0 deletions packages/rstack/tests/setup/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ test.runIf(process.platform !== 'win32')('restores executable mode on existing s
});
});

test('repairs generated files without rewriting an unchanged hooksPath', () => {
withRepository((cwd) => {
expect(installHooks({ cwd }).status).toBe('installed');
const runner = path.join(cwd, hooksPath, 'runner');
writeFileSync(runner, 'stale\n');
writeFileSync(path.join(cwd, '.git', 'config.lock'), 'locked');

expect(installHooks({ cwd })).toEqual({ status: 'installed', hooksPath });
expect(readFileSync(runner, 'utf8')).toBe(createHookFiles().runner);
});
});

test('skips non-Git directories without creating files', () => {
withDirectory((cwd) => {
expect(installHooks({ cwd })).toEqual({
Expand Down