Skip to content

Commit

Permalink
Merge b3fa1b1 into f25888f
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogesh Lonkar committed Nov 17, 2018
2 parents f25888f + b3fa1b1 commit 8fc81d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
npm-debug.log
coverage
.tern-port
*.swp
63 changes: 15 additions & 48 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,27 @@ var fs = require('fs')
// `pre-commit` file. The path needs to be absolute in order for the symlinking
// to work correctly.
//

var git = getGitFolderPath(root);

// Function to recursively finding .git folder
function getGitFolderPath(currentPath) {
var git = path.resolve(currentPath, '.git')

if (!exists(git) || !fs.lstatSync(git).isDirectory()) {
console.log('pre-commit:');
console.log('pre-commit: Not found .git folder in', git);

var newPath = path.resolve(currentPath, '..');

// Stop if we on top folder
if (currentPath === newPath) {
return null;
}

return getGitFolderPath(newPath);
}

console.log('pre-commit:');
console.log('pre-commit: Found .git folder in', git);
return git;
}

//
// Resolve git directory for submodules
//
var git = path.resolve(root, '.git')
if (exists(git) && fs.lstatSync(git).isFile()) {
var gitinfo = fs.readFileSync(git).toString()
, gitdirmatch = /gitdir: (.+)/.exec(gitinfo)
, gitdir = gitdirmatch.length == 2 ? gitdirmatch[1] : null;

if (gitdir !== null) {
git = path.resolve(root, gitdir);
hooks = path.resolve(git, 'hooks');
precommit = path.resolve(hooks, 'pre-commit');
}
var gitdir = fs.readFileSync(git).toString();
if (gitdir) {
var submodule = gitdir.replace(/^.*gitdir: /,'').trim();
submodule = path.resolve(root, submodule);
if (exists(submodule) && fs.lstatSync(submodule).isDirectory()) {
console.info('pre-commit: Detected submodule ' + submodule);
git = submodule;
}
}
}

var hooks = path.resolve(git, 'hooks')
var precommit = path.resolve(hooks, 'pre-commit');

//
// Bail out if we don't have an `.git` directory as the hooks will not get
// triggered. If we do have directory create a hooks folder if it doesn't exist.
//
if (!git) {
console.log('pre-commit:');
console.log('pre-commit: Not found any .git folder for installing pre-commit hook');
return;
}

var hooks = path.resolve(git, 'hooks')
, precommit = path.resolve(hooks, 'pre-commit');

if (!exists(git) || !fs.lstatSync(git).isDirectory()) return;
if (!exists(hooks)) fs.mkdirSync(hooks);

//
Expand Down Expand Up @@ -101,7 +68,7 @@ if(os.platform() === 'win32') {
hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/');
}

var precommitContent = '#!/usr/bin/env bash' + os.EOL
var precommitContent = '#!/bin/bash' + os.EOL
+ hookRelativeUnixPath + os.EOL
+ 'RESULT=$?' + os.EOL
+ '[ $RESULT -ne 0 ] && exit 1' + os.EOL
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"homepage": "https://github.com/observing/pre-commit",
"license": "MIT",
"dependencies": {
"cross-spawn": "^5.0.1",
"spawn-sync": "^1.0.15",
"which": "1.2.x"
"cross-spawn": "^6.0.5",
"spawn-sync": "^2.0.0",
"which": "1.3.1"
},
"devDependencies": {
"assume": "~1.5.0",
"assume": "2.1.0",
"istanbul": "0.4.x",
"mocha": "~3.3.0",
"mocha": "5.2.0",
"pre-commit": "git://github.com/observing/pre-commit.git"
}
}

0 comments on commit 8fc81d5

Please sign in to comment.