-
Notifications
You must be signed in to change notification settings - Fork 0
feat(setup): add Git hook dispatcher generation #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+130
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Keep this list aligned with the client-side hooks generated by Husky v9. | ||
| // `pre-auto-gc` is included even though it is not listed in Husky's documentation. | ||
| const hookNames = [ | ||
| 'pre-commit', | ||
| 'pre-merge-commit', | ||
| 'prepare-commit-msg', | ||
| 'commit-msg', | ||
| 'post-commit', | ||
| 'applypatch-msg', | ||
| 'pre-applypatch', | ||
| 'post-applypatch', | ||
| 'pre-rebase', | ||
| 'post-rewrite', | ||
| 'post-checkout', | ||
| 'post-merge', | ||
| 'pre-push', | ||
| 'pre-auto-gc', | ||
| ] as const; | ||
|
|
||
| type HookFileName = (typeof hookNames)[number] | 'runner'; | ||
|
|
||
| // Generated shims live in `<hooks-directory>/_`. When a shim sources this | ||
| // dispatcher, `$0` still points to the shim, so the user hook is one level up. | ||
| const dispatcher = `#!/usr/bin/env sh | ||
|
|
||
| name=$(basename "$0") | ||
| dir=$(dirname "$(dirname "$0")") | ||
| hook="$dir/$name" | ||
|
|
||
| [ -f "$hook" ] || exit 0 | ||
|
|
||
| sh -e "$hook" "$@" | ||
| status=$? | ||
| exit "$status" | ||
| `; | ||
|
|
||
| // Every generated Git hook sources the same dispatcher to keep runtime behavior | ||
| // consistent and make future initialization changes local to one file. | ||
| const shim = `#!/usr/bin/env sh | ||
| . "$(dirname "$0")/runner" | ||
| `; | ||
|
|
||
| export const createHookFiles = (): Record<HookFileName, string> => { | ||
| const files: Record<string, string> = { | ||
| runner: dispatcher, | ||
| }; | ||
|
|
||
| for (const name of hookNames) { | ||
| files[name] = shim; | ||
| } | ||
|
|
||
| return files as Record<HookFileName, string>; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
| import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { expect, test } from 'rstack/test'; | ||
| import { createHookFiles } from '../../src/hooks.ts'; | ||
|
|
||
| test('generates the dispatcher and all client-side Git hook shims', () => { | ||
| const { runner, ...shims } = createHookFiles(); | ||
|
|
||
| expect(Object.keys(shims)).toEqual([ | ||
| 'pre-commit', | ||
| 'pre-merge-commit', | ||
| 'prepare-commit-msg', | ||
| 'commit-msg', | ||
| 'post-commit', | ||
| 'applypatch-msg', | ||
| 'pre-applypatch', | ||
| 'post-applypatch', | ||
| 'pre-rebase', | ||
| 'post-rewrite', | ||
| 'post-checkout', | ||
| 'post-merge', | ||
| 'pre-push', | ||
| 'pre-auto-gc', | ||
| ]); | ||
| expect(runner).toBeTruthy(); | ||
| expect(new Set(Object.values(shims)).size).toBe(1); | ||
| }); | ||
|
|
||
| test.runIf(process.platform !== 'win32')('runs generated hooks', () => { | ||
| const directory = mkdtempSync(path.join(tmpdir(), 'rstack hooks ')); | ||
| const hooksDirectory = path.join(directory, 'hooks with spaces'); | ||
| const generatedDirectory = path.join(hooksDirectory, '_'); | ||
| const generatedHook = path.join(generatedDirectory, 'pre-commit'); | ||
| const userHook = path.join(hooksDirectory, 'pre-commit'); | ||
| const files = createHookFiles(); | ||
|
|
||
| try { | ||
| mkdirSync(generatedDirectory, { recursive: true }); | ||
| writeFileSync(path.join(generatedDirectory, 'runner'), files.runner); | ||
| writeFileSync(generatedHook, files['pre-commit']); | ||
|
|
||
| expect(spawnSync('sh', [generatedHook]).status).toBe(0); | ||
|
|
||
| writeFileSync( | ||
| userHook, | ||
| `read -r input | ||
| printf '%s\\n' "$1|$input" | ||
| exit 23 | ||
| `, | ||
| ); | ||
| const result = spawnSync('sh', [generatedHook, 'argument with spaces'], { | ||
| encoding: 'utf8', | ||
| input: 'standard input\n', | ||
| }); | ||
|
|
||
| expect(result.status).toBe(23); | ||
| expect(result.stdout).toBe('argument with spaces|standard input\n'); | ||
|
|
||
| writeFileSync( | ||
| userHook, | ||
| `false | ||
| printf 'unreachable\\n' | ||
| `, | ||
| ); | ||
| const errexitResult = spawnSync('sh', [generatedHook], { encoding: 'utf8' }); | ||
|
|
||
| expect(errexitResult.status).toBe(1); | ||
| expect(errexitResult.stdout).toBe(''); | ||
| } finally { | ||
| rmSync(directory, { force: true, recursive: true }); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| # Custom Dictionary Words | ||
| applypatch | ||
| errexit | ||
| fnames | ||
| llms | ||
| oxfmt | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.