Skip to content
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

read stdin from hook script #645

Merged
merged 3 commits into from Jan 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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