Skip to content

Commit d97ae53

Browse files
committed
fix(setup): preserve hook file paths in monorepos
1 parent b5087d9 commit d97ae53

4 files changed

Lines changed: 41 additions & 1 deletion

File tree

packages/rstack/src/setup/hooks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ if ! command -v node >/dev/null 2>&1 && [ -x "$rs_node_fallback" ]; then
5454
PATH="\${PATH:+$PATH:}\${rs_node_fallback%/*}"
5555
fi
5656
57+
# Keep message file paths valid after changing to the owning project.
58+
case "$rs_name" in
59+
*-msg)
60+
[ -n "\${1-}" ] || exit 1
61+
case "$1" in
62+
/*|[A-Za-z]:/*) ;;
63+
*)
64+
rs_file=$1
65+
shift
66+
set -- "$rs_root/$rs_file" "$@"
67+
;;
68+
esac
69+
;;
70+
esac
71+
5772
cd "$rs_root/$rs_project_path" || exit 1
5873
export PATH="node_modules/.bin\${PATH:+:$PATH}"
5974

packages/rstack/tests/setup/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const writeInit = (cwd: string, content: string): void => {
7070
export const runHook = (cwd: string, value?: string): SpawnSyncReturns<string> =>
7171
git(cwd, ['hook', 'run', 'pre-commit'], hookEnv(cwd, value));
7272

73+
export const runGitHook = (cwd: string, name: string, args: string[]): SpawnSyncReturns<string> =>
74+
git(cwd, ['hook', 'run', name, '--', ...args], hookEnv(cwd));
75+
7376
export const withRepository = (callback: (cwd: string) => void): void =>
7477
withDirectory((cwd) => {
7578
const globalConfig = process.env.GIT_CONFIG_GLOBAL;

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'n
22
import path from 'node:path';
33
import { expect, test } from 'rstack/test';
44
import { installHooks } from '../../src/setup/install.ts';
5-
import { runHook, withRepository, writeHook, writeInit } from './helpers.ts';
5+
import { runGitHook, runHook, withRepository, writeHook, writeInit } from './helpers.ts';
66

77
test('loads user init and project binaries', () => {
88
withRepository((cwd) => {
@@ -35,6 +35,26 @@ rstack-hook-command
3535
});
3636
});
3737

38+
test('preserves file arguments for hooks owned by a nested project', () => {
39+
withRepository((cwd) => {
40+
const projectDirectory = path.join(cwd, 'frontend');
41+
const messagePath = '.git/COMMIT_EDITMSG';
42+
mkdirSync(projectDirectory);
43+
writeFileSync(path.join(cwd, messagePath), 'commit message\n');
44+
45+
expect(installHooks({ cwd: projectDirectory }).status).toBe('installed');
46+
47+
for (const name of ['applypatch-msg', 'commit-msg', 'prepare-commit-msg']) {
48+
writeFileSync(path.join(cwd, '.rstack', 'hooks', name), 'cat "$1"\n');
49+
50+
expect(runGitHook(cwd, name, [messagePath])).toMatchObject({
51+
status: 0,
52+
stderr: 'commit message\n',
53+
});
54+
}
55+
});
56+
});
57+
3858
test('skips user hooks when disabled by the environment or init', () => {
3959
withRepository((cwd) => {
4060
writeHook(cwd, 'echo ran >> hook-ran\n');

scripts/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ applypatch
33
cdpath
44
clippy
55
dirents
6+
editmsg
67
errexit
8+
esac
79
extglob
810
fnames
911
huskyrc

0 commit comments

Comments
 (0)