Skip to content

Commit

Permalink
read stdin from hook script (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 14, 2020
1 parent e84f407 commit 0eb20f2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 54 deletions.
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "husky",
"version": "4.0.7",
"version": "4.0.8",
"description": "Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)",
"bin": {
"husky-run": "./run.js",
Expand Down Expand Up @@ -52,7 +52,6 @@
"chalk": "^3.0.0",
"ci-info": "^2.0.0",
"cosmiconfig": "^6.0.0",
"get-stdin": "^7.0.0",
"opencollective-postinstall": "^2.0.2",
"pkg-dir": "^4.2.0",
"please-upgrade-node": "^3.2.0",
Expand Down
9 changes: 7 additions & 2 deletions src/installer/__tests__/__snapshots__/getScript.ts.snap
Expand Up @@ -4,7 +4,7 @@ exports[`hookScript should match snapshot 1`] = `
"#!/bin/sh
# husky
# Hook created by Husky v4.0.7 (https://github.com/typicode/husky#readme)
# Hook created by Husky v4.0.8 (https://github.com/typicode/husky#readme)
# At: <locale date string>
# From: /home/typicode/projects/foo-package (https://github.com/foo/foo-package)
# With: npm
Expand Down Expand Up @@ -45,7 +45,7 @@ run_command () {
fi
}
debug \\"husky v4.0.7 (created at <locale date string>)\\"
debug \\"husky v4.0.8 (created at <locale date string>)\\"
debug \\"$hookName hook started\\"
debug \\"Current working directory is \`pwd\`\\"
Expand All @@ -68,6 +68,11 @@ fi
cd \\".\\"
case $hookName in
\\"pre-push\\"|\\"pre-receive\\"|\\"post-receive\\"|\\"post-rewrite\\")
export HUSKY_GIT_STDIN=\`cat\`;;
esac
if command_exists winpty && test -t 1; then
exec < /dev/tty
fi
Expand Down
5 changes: 5 additions & 0 deletions src/installer/getScript.ts
Expand Up @@ -94,6 +94,11 @@ fi
cd "${relativeUserPkgDir}"
case $hookName in
"pre-push"|"pre-receive"|"post-receive"|"post-rewrite")
export HUSKY_GIT_STDIN=\`cat\`;;
esac
if command_exists winpty && test -t 1; then
exec < /dev/tty
fi
Expand Down
29 changes: 1 addition & 28 deletions src/runner/__tests__/index.ts
Expand Up @@ -107,32 +107,6 @@ describe('run', (): void => {
expect(status).toBe(0)
})

it('should set HUSKY_GIT_STDIN env for some hooks', async (): Promise<
void
> => {
const dir = tempy.directory()

fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-push': 'echo success'
}
}
})
)

const status = await index(['', '', 'pre-push'], {
cwd: dir,
getStdinFn: (): Promise<string> => Promise.resolve('foo')
})
expectSpawnSyncToHaveBeenCalledWith(dir, 'echo success', {
HUSKY_GIT_STDIN: 'foo'
})
expect(status).toBe(0)
})

it('should set HUSKY_GIT_PARAMS', async (): Promise<void> => {
const dir = tempy.directory()

Expand Down Expand Up @@ -160,8 +134,7 @@ describe('run', (): void => {
it("should not throw if there's no package.json", async (): Promise<void> => {
const dir = tempy.directory()
await index(['', '', 'pre-push'], {
cwd: dir,
getStdinFn: (): Promise<string> => Promise.resolve('foo')
cwd: dir
})
})
})
17 changes: 1 addition & 16 deletions src/runner/index.ts
@@ -1,6 +1,5 @@
import chalk from 'chalk'
import { spawnSync } from 'child_process'
import getStdin from 'get-stdin'
import getConf from '../getConf'
import { readPkg } from '../read-pkg'

Expand Down Expand Up @@ -70,10 +69,7 @@ function runCommand(
*/
export default async function run(
[, , hookName = '', HUSKY_GIT_PARAMS]: string[],
{
cwd = process.cwd(),
getStdinFn = getStdin
}: { cwd?: string; getStdinFn?: () => Promise<string> } = {}
{ cwd = process.cwd() }: { cwd?: string } = {}
): Promise<number> {
const oldCommand = getOldCommand(cwd, hookName)
const command = getCommand(cwd, hookName)
Expand All @@ -85,17 +81,6 @@ export default async function run(
env.HUSKY_GIT_PARAMS = HUSKY_GIT_PARAMS
}

// Read stdin and add HUSKY_GIT_STDIN
const hooksWithStdin = [
'pre-push',
'pre-receive',
'post-receive',
'post-rewrite'
]
if (hooksWithStdin.includes(hookName)) {
env.HUSKY_GIT_STDIN = await getStdinFn()
}

if (command) {
return runCommand(cwd, hookName, command, env)
}
Expand Down

0 comments on commit 0eb20f2

Please sign in to comment.