Skip to content

Commit 80d9536

Browse files
committed
Revert "Pass standard input through to all hooks (tarmolov#21)"
This reverts commit 74c8549.
1 parent a6fc75d commit 80d9536

File tree

3 files changed

+36
-77
lines changed

3 files changed

+36
-77
lines changed

lib/git-hooks.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@ module.exports = {
115115
*
116116
* @param {String} filename Path to git hook.
117117
* @param {String[]} [args] Git hook arguments.
118-
* @param {String} input Input string to be passed into each hook's standard input.
119118
* @param {Function} callback
120119
*/
121-
run: function (filename, args, input, callback) {
120+
run: function (filename, args, callback) {
122121
var hookName = path.basename(filename);
123122
var hooksDirname = path.resolve(path.dirname(filename), '../../.githooks', hookName);
124123

@@ -128,7 +127,7 @@ module.exports = {
128127
return path.resolve(hooksDirname, hookName);
129128
});
130129
excludeIgnoredPaths(hooks, function (filteredHooks) {
131-
runHooks(filteredHooks, args, input, callback);
130+
runHooks(filteredHooks, args, callback);
132131
});
133132
} else {
134133
callback(0);
@@ -141,20 +140,19 @@ module.exports = {
141140
*
142141
* @param {String[]} hooks List of hook names to execute.
143142
* @param {String[]} args
144-
* @param {String} input Input string to be passed into each hook's standard input.
145143
* @param {Function} callback
146144
*/
147-
function runHooks(hooks, args, input, callback) {
145+
function runHooks(hooks, args, callback) {
148146
if (!hooks.length) {
149147
callback(0);
150148
return;
151149
}
152150

153151
try {
154-
var hook = spawnHook(hooks.shift(), args, input);
152+
var hook = spawnHook(hooks.shift(), args);
155153
hook.on('close', function (code) {
156154
if (code === 0) {
157-
runHooks(hooks, args, input, callback);
155+
runHooks(hooks, args, callback);
158156
} else {
159157
callback(code);
160158
}
@@ -179,18 +177,15 @@ function isExecutable(stats) {
179177
*
180178
* @param {String} hookName
181179
* @param {String[]} args
182-
* @param {String} input Input string to be passed into the hook's standard input.
183180
* @returns {ChildProcess}
184181
*/
185-
function spawnHook(hookName, args, input) {
182+
function spawnHook(hookName, args) {
186183
var stats = fs.statSync(hookName);
187184
var isHookExecutable = stats && stats.isFile() && isExecutable(stats);
188185
if (!isHookExecutable) {
189186
throw new Error('Cannot execute hook: ' + hookName + '. Please check file permissions.');
190187
}
191-
var childProcess = spawn(hookName, args, {stdio: ['pipe', process.stdout, process.stderr]});
192-
childProcess.stdin.end(input, 'utf8');
193-
return childProcess;
188+
return spawn(hookName, args, {stdio: 'inherit'});
194189
}
195190

196191
/**

lib/hook-template.js

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
#!/usr/bin/env node
22

3-
var input = '';
4-
5-
process.stdin.setEncoding('utf8');
6-
7-
process.stdin.on('readable', function () {
8-
var chunk;
9-
if ((chunk = process.stdin.read()) !== null) {
10-
input += chunk;
11-
}
12-
});
13-
14-
process.stdin.on('end', function () {
15-
try {
16-
/**
17-
* require('git-hooks') isn't used to support case when node_modules is put in subdirectory.
18-
* .git
19-
* .githooks
20-
* www
21-
* node_modules
22-
*/
23-
require('%s/git-hooks').run(__filename, process.argv.slice(2), input, function (code, error) {
24-
if (error) {
25-
console.error('[GIT-HOOKS ERROR] ' + error.message);
26-
}
27-
process.exit(code);
28-
});
29-
} catch (e) {
30-
console.error('[GIT-HOOKS ERROR] ' + e.message);
31-
32-
if (e.code === 'MODULE_NOT_FOUND') {
33-
console.error('[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error');
3+
try {
4+
/**
5+
* require('git-hooks') isn't used to support case when node_modules is put in subdirectory.
6+
* .git
7+
* .githooks
8+
* www
9+
* node_modules
10+
*/
11+
require('%s/git-hooks').run(__filename, process.argv.slice(2), function (code, error) {
12+
if (error) {
13+
console.error('[GIT-HOOKS ERROR] ' + error.message);
3414
}
15+
process.exit(code);
16+
});
17+
} catch (e) {
18+
console.error('[GIT-HOOKS ERROR] ' + e.message);
19+
20+
if (e.code === 'MODULE_NOT_FOUND') {
21+
console.error('[GIT-HOOKS ERROR] Please reinstall git-hooks to fix this error');
3522
}
36-
});
23+
}

tests/run.test.js

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require('chai').should();
22
var fs = require('fs');
3-
var exec = require('child_process').exec;
43
var gitHooks = require('../lib/git-hooks');
54
var fsHelpers = require('../lib/fs-helpers');
65

@@ -27,7 +26,7 @@ describe('git-hook runner', function () {
2726
});
2827

2928
it('should works without hooks', function (done) {
30-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code) {
29+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code) {
3130
code.should.equal(0);
3231
done();
3332
});
@@ -45,7 +44,7 @@ describe('git-hook runner', function () {
4544
});
4645

4746
it('should return an error', function (done) {
48-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code, error) {
47+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code, error) {
4948
code.should.equal(1);
5049
error.should.be.ok;
5150
done();
@@ -63,7 +62,7 @@ describe('git-hook runner', function () {
6362
});
6463

6564
it('should run it one by one', function (done) {
66-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code) {
65+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code) {
6766
code.should.equal(0);
6867
hooks.forEach(function (name) {
6968
var logFile = SANDBOX_PATH + name + '.log';
@@ -77,42 +76,20 @@ describe('git-hook runner', function () {
7776
describe('and work without errors', function () {
7877
var logFile = SANDBOX_PATH + 'hello.log';
7978
beforeEach(function () {
80-
createHook(
81-
PROJECT_PRECOMMIT_HOOK + 'hello',
82-
'input=`cat`; echo -e "Hello, world!\n${@:1}\n$input" > ' + logFile
83-
);
79+
createHook(PROJECT_PRECOMMIT_HOOK + 'hello', 'echo "Hello, world! ${@:1}" > ' + logFile);
8480
});
8581

8682
it('should pass all arguments to them', function (done) {
87-
gitHooks.run(PRECOMMIT_HOOK_PATH, ['I', 'am', 'working', 'properly!'], '', function () {
88-
fs.readFileSync(logFile).toString().trim().should.equal('Hello, world!\nI am working properly!');
83+
gitHooks.run(PRECOMMIT_HOOK_PATH, ['I', 'am', 'working', 'properly!'], function () {
84+
fs.readFileSync(logFile).toString().should.equal('Hello, world! I am working properly!\n');
8985
done();
9086
});
9187
});
9288

93-
describe('if standard input is passed in', function () {
94-
it('should read it properly', function (done) {
95-
exec('echo "I am working properly!" | ' + PRECOMMIT_HOOK_PATH, function () {
96-
fs.readFileSync(logFile).toString().trim()
97-
.should.equal('Hello, world!\n\nI am working properly!');
98-
done();
99-
});
100-
});
101-
102-
it('should pass it to them', function (done) {
103-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], 'I am working properly!', function () {
104-
fs.readFileSync(logFile).toString().trim()
105-
.should.equal('Hello, world!\n\nI am working properly!');
106-
done();
107-
});
108-
});
109-
110-
});
111-
11289
it('should run a hook with success status', function (done) {
113-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code) {
90+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code) {
11491
code.should.equal(0);
115-
fs.readFileSync(logFile).toString().trim().should.equal('Hello, world!');
92+
fs.readFileSync(logFile).toString().should.equal('Hello, world! \n');
11693
done();
11794
});
11895
});
@@ -124,7 +101,7 @@ describe('git-hook runner', function () {
124101
});
125102

126103
it('should run a hook and return error', function (done) {
127-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code) {
104+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code) {
128105
code.should.equal(255);
129106
done();
130107
});
@@ -142,7 +119,7 @@ describe('git-hook runner', function () {
142119
});
143120

144121
it('should ignore file with wrong permissions in hooks directory', function (done) {
145-
gitHooks.run(PRECOMMIT_HOOK_PATH, [], '', function (code) {
122+
gitHooks.run(PRECOMMIT_HOOK_PATH, [], function (code) {
146123
code.should.equal(0);
147124
done();
148125
});

0 commit comments

Comments
 (0)