Skip to content

Commit f1a2fd8

Browse files
committed
FIX: properly detect the git directory to ensure the git commands will use the right git info
1 parent 2ff66eb commit f1a2fd8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

hook/pre-commit.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ var spawnCmd = function ( cmd ) {
223223
var doExec = function ( cmd, cb ) {
224224
var _exec = require( 'child_process' ).exec;
225225
return new Promise( function ( resolve, reject ) {
226-
_exec( cmd, function ( err, stdout, stderr ) {
226+
_exec( cmd, { maxBuffer: Infinity }, function ( err, stdout, stderr ) {
227227
if ( !err ) {
228228
resolve( stdout );
229229
} else {
230-
reject( stderr );
230+
reject( { err: err, stderr: stderr } );
231231
}
232232
cb && cb( err, stdout, stderr );
233233
} );
@@ -519,6 +519,8 @@ var ignoreBranchFn = function ( branch ) {
519519
} ).length > 0;
520520
};
521521

522+
nodeProcess.chdir( opts.gitDirectory );
523+
522524
// check if can be applied to the given branch
523525
doExec( 'git name-rev --name-only HEAD', function ( err, stdout /*, stderr*/ ) {
524526
if ( err ) {

lib/hook.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ module.exports = {
77
var p = findGitDir();
88

99
return p.then( function ( gitDir ) {
10-
return path.resolve( gitDir, './.git/hooks/pre-commit' );
10+
return {
11+
hookFile: path.resolve( gitDir, './.git/hooks/pre-commit' ),
12+
gitDir: gitDir
13+
};
1114
} );
1215
},
1316
install: function ( cfg ) {
@@ -27,17 +30,22 @@ module.exports = {
2730

2831
prepushSource = prepushSource.replace( /require\(\s*'process'\s*\);/g, 'process;' )
2932
.replace( /require\(\s*'console'\s*\);/g, 'console;' )
30-
.replace( /require\(\s*'\.\/Promise'\s*\);/g, 'global.Promise;' )
31-
.replace( /require\(\s*'precommit-config'\s*\);/g, JSON.stringify( cfg, null, 2 ) + ';' );
33+
.replace( /require\(\s*'\.\/Promise'\s*\);/g, 'global.Promise;' );
34+
35+
36+
return this._pathToFile().then( function ( args ) {
37+
var pathToFile = args.hookFile;
38+
cfg.gitDirectory = args.gitDir;
39+
prepushSource = prepushSource.replace( /require\(\s*'precommit-config'\s*\);/g, JSON.stringify( cfg, null, 2 ) + ';' );
3240

33-
return this._pathToFile().then( function ( pathToFile ) {
3441
write( pathToFile, prepushSource );
3542
fs.chmodSync( pathToFile, '755' );
3643
return pathToFile;
3744
} );
3845
},
3946
remove: function () {
40-
return this._pathToFile().then( function ( pathToFile ) {
47+
return this._pathToFile().then( function ( args ) {
48+
var pathToFile = args.hookFile;
4149
var del = require( 'del' );
4250
del.sync( pathToFile, { force: true } );
4351

0 commit comments

Comments
 (0)