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
40 changes: 19 additions & 21 deletions packages/rstack/src/setup/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,47 @@ export const installHooks = ({
}

// Check Git before touching the filesystem so non-repositories have no side effects.
const repository = runGit(cwd, ['rev-parse', '--is-inside-work-tree', '--show-prefix']);
const repository = runGit(cwd, [
'rev-parse',
'--is-inside-work-tree',
'--show-prefix',
'--git-path',
'hooks',
]);
if (repository.error || repository.status === null) {
return gitFailure(repository.error, repository.stderr);
}

const firstLineEnd = repository.stdout.indexOf('\n');
const insideWorkTree = (
firstLineEnd === -1 ? repository.stdout : repository.stdout.slice(0, firstLineEnd)
).trim();
const [insideWorkTree = '', repositoryPrefix, configuredHooksPath] = removeLineEnding(
repository.stdout,
).split(/\r?\n/u);

if (repository.status !== 0) {
if (insideWorkTree === 'true') {
if (insideWorkTree.trim() === 'true') {
return fail(
'git-command-failed',
`Failed to resolve the Git repository prefix: ${repository.stderr.trim()}`,
`Failed to resolve the Git repository paths: ${repository.stderr.trim()}`,
);
}
return { status: 'skipped', reason: 'not-git-repository' };
}

if (insideWorkTree !== 'true') {
if (insideWorkTree.trim() !== 'true') {
return { status: 'skipped', reason: 'not-git-repository' };
}

const prefix =
firstLineEnd === -1
? ''
: removeLineEnding(repository.stdout.slice(firstLineEnd + 1)).replaceAll('\\', '/');
const hooksPath = `${prefix}${resolvedDir}/_`;

const config = runGit(cwd, ['config', '--local', '--get', 'core.hooksPath']);
if (config.error || config.status === null) {
return gitFailure(config.error, config.stderr);
}
if (config.status !== 0 && config.status !== 1) {
return fail('git-config-failed', `Failed to read core.hooksPath: ${config.stderr.trim()}`);
if (repositoryPrefix === undefined || configuredHooksPath === undefined) {
return fail('git-command-failed', 'Failed to resolve the Git repository paths.');
}

const prefix = repositoryPrefix.replaceAll('\\', '/');
const hooksPath = `${prefix}${resolvedDir}/_`;

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

Expand Down
8 changes: 8 additions & 0 deletions packages/rstack/tests/setup/directories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ printf 'nested\\n' > nested-hook-ran
status: 'installed',
hooksPath: nestedHooksPath,
});
expect(installHooks({ cwd: projectDirectory })).toEqual({
status: 'unchanged',
hooksPath: nestedHooksPath,
});
expect(runGit(cwd, ['config', '--local', '--get', 'core.hooksPath'])).toBe(nestedHooksPath);
expect(existsSync(path.join(projectDirectory, hooksPath, 'runner'))).toBe(true);

Expand All @@ -57,6 +61,10 @@ test('installs a custom hooks directory from a nested project', () => {
status: 'installed',
hooksPath: 'frontend app/config/hooks/_',
});
expect(installHooks({ cwd: projectDirectory, hooksDir: 'config\\hooks' })).toEqual({
status: 'unchanged',
hooksPath: 'frontend app/config/hooks/_',
});
expect(runGit(cwd, ['config', '--local', '--get', 'core.hooksPath'])).toBe(
'frontend app/config/hooks/_',
);
Expand Down