Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .githooks/pre-commit/test
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn;
var exec = require('child_process').execSync;
var gitCachedFiles = exec('git diff --cached --name-only --diff-filter=ACMR').toString().trim();
var CP = require('child_process');
var gitCachedFiles = CP.execSync('git diff --cached --name-only --diff-filter=ACMR').toString().trim();
var opts = {stdio: 'inherit'};

if (gitCachedFiles) {
spawn('npm', ['test'], {stdio: 'inherit'}).on('close', process.exit.bind(process));
(process.platform === 'win32' ?
CP.exec('npm test', opts) : CP.spawn('npm', ['test'], opts)).on('close', process.exit.bind(process));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using execSync for executing npm test is a bad idea because the script can reach buffer limit. Moreover, execSync doesn't proxy any output during execution; so, a user watches nothing but an empty screen. It's a bad UX.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec().stdout.on('data') resolves problem. There is error occur if i spawn npm(npm enoent).

}
25 changes: 21 additions & 4 deletions lib/git-hooks.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var util = require('util');
var spawn = require('child_process').spawn;
var fs = require('fs');
var fsHelpers = require('./fs-helpers');
var isWin32 = process.platform === 'win32';

var HOOKS_DIRNAME = 'hooks';
var HOOKS_OLD_DIRNAME = 'hooks.old';
Expand Down Expand Up @@ -51,7 +52,12 @@ module.exports = {
}

var hookTemplate = fs.readFileSync(__dirname + '/' + HOOKS_TEMPLATE_FILE_NAME);
var pathToGitHooks = path.relative(hooksPath, __dirname);
var pathToGitHooks = path.join(path.relative(hooksPath, __dirname), 'git-hooks');

if (isWin32) {
pathToGitHooks = pathToGitHooks.replace(/\\/g, '\\\\');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backslash, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
without it will be '....\node_modules\git-hooks\lib\git-hooks'

}

var hook = util.format(hookTemplate.toString(), pathToGitHooks);

fsHelpers.makeDir(hooksPath);
Expand Down Expand Up @@ -160,11 +166,22 @@ function isExecutable(stats) {
*/
function spawnHook(hookName, args) {
var stats = fs.statSync(hookName);
var isHookExecutable = stats && stats.isFile() && isExecutable(stats);
if (!isHookExecutable) {
var command = hookName;
var opts = args;
var hook;

if (isWin32) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to move all windows specific code into a separate module.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explain more detail

hook = fs.readFileSync(hookName).toString();
if (!require('shebang-regex').test(hook)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't merge the pr with extra modules.

If you really want to help, let's list all current problems with windows version and find out the best solution for each of them. Discussion first, then code.

We can start with your description: #10 (comment)

throw new Error('Cannot find shebang in hook: ' + hookName + '.');
}
command = require('shebang-command')(require('shebang-regex').exec(hook)[0]);
opts = [hookName].concat(opts);
} else if (!(stats && stats.isFile() && isExecutable(stats))) {
throw new Error('Cannot execute hook: ' + hookName + '. Please check file permissions.');
}
return spawn(hookName, args, {stdio: 'inherit'});

return spawn(command, opts, {stdio: 'inherit'});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/hook-template.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

try {
require('%s/git-hooks').run(__filename, process.argv[2], function (code) {
require('%s').run(__filename, process.argv[2], function (code) {
process.exit(code);
});
} catch (e) {
Expand Down
4 changes: 4 additions & 0 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"examples",
"lib"
],
"dependencies": {
"shebang-command": "1.0.0",
"shebang-regex": "2.0.0"
},
"devDependencies": {
"chai": "2.3.0",
"istanbul": "0.3.17",
Expand Down
10 changes: 9 additions & 1 deletion tests/run.test.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('chai').should();
var fs = require('fs');
var gitHooks = require('../lib/git-hooks');
var fsHelpers = require('../lib/fs-helpers');
var isWin32 = process.platform === 'win32';

var SANDBOX_PATH = __dirname + '/tmp-sandbox/';
var GIT_ROOT = SANDBOX_PATH + '.git/';
Expand Down Expand Up @@ -39,7 +40,8 @@ describe('git-hook runner', function () {
describe('and a hook is unexecutable', function () {
beforeEach(function () {
var logFile = SANDBOX_PATH + 'hello.log';
fs.writeFileSync(PROJECT_PRECOMMIT_HOOK + 'hello', '#!/bin/bash\n' + 'echo hello > ' + logFile);
var hook = (isWin32 ? '' : '#!/bin/bash\n') + 'echo hello > ' + logFile;
fs.writeFileSync(PROJECT_PRECOMMIT_HOOK + 'hello', hook);
});

it('should return an error', function () {
Expand All @@ -55,6 +57,9 @@ describe('git-hook runner', function () {
beforeEach(function () {
hooks.forEach(function (name) {
var logFile = SANDBOX_PATH + name + '.log';
if (isWin32) {
logFile = logFile.replace(/\\/g, '\\\\');
}
createHook(PROJECT_PRECOMMIT_HOOK + name, 'echo ' + name + '> ' + logFile);
});
});
Expand All @@ -73,6 +78,9 @@ describe('git-hook runner', function () {

describe('and works without errors', function () {
var logFile = SANDBOX_PATH + 'hello.log';
if (isWin32) {
logFile = logFile.replace(/\\/g, '\\\\');
}
beforeEach(function () {
createHook(PROJECT_PRECOMMIT_HOOK + 'hello', 'echo Hello, world! > ' + logFile);
});
Expand Down