diff --git a/install.js b/install.js index e761ba9..0f9975f 100644 --- a/install.js +++ b/install.js @@ -20,6 +20,17 @@ var git = path.resolve(root, '.git') , hooks = path.resolve(git, 'hooks') , precommit = path.resolve(hooks, 'pre-commit'); +// If the .git is a file it is probably a submodule +if (fs.lstatSync(git).isFile()) { + // the .git file should contain "gitdir: ../.git/modules/path" if we use submodules + var fileContents = fs.readFileSync(git, 'utf8'); + if (fileContents.match(/^gitdir:/)) { + git = path.resolve(root, fileContents.replace(/gitdir: /, '').replace(/^\s+|\s+$/g, '')); + hooks = path.resolve(git, 'hooks'); + 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. @@ -51,7 +62,6 @@ catch (e) {} // as stashing - unstashing the unstaged changes) // TODO: we could keep launching the old pre-commit scripts var hookRelativeUnixPath = hook.replace(root, '.'); - if(os.platform() === 'win32') { hookRelativeUnixPath = hookRelativeUnixPath.replace(/[\\\/]+/g, '/'); } diff --git a/uninstall.js b/uninstall.js index a0d2fd0..359c5c9 100644 --- a/uninstall.js +++ b/uninstall.js @@ -3,7 +3,21 @@ var fs = require('fs') , path = require('path') , exists = fs.existsSync || path.existsSync - , precommit = path.resolve(__dirname, '../..', '.git', 'hooks', 'pre-commit'); + , root = path.resolve(__dirname, '..', '..') + , git = path.resolve(root, '.git') + , hooks = path.resolve(git, 'hooks') + , precommit = path.resolve(hooks, 'pre-commit'); + +// If the .git is a file it is probably a submodule +if (fs.lstatSync(git).isFile()) { + // the .git file should contain "gitdir: ../.git/modules/path" if we use submodules + var fileContents = fs.readFileSync(git, 'utf8'); + if (fileContents.match(/^gitdir:/)) { + git = path.resolve(root, fileContents.replace(/gitdir: /, '').replace(/^\s+|\s+$/g, '')); + hooks = path.resolve(git, 'hooks'); + precommit = path.resolve(hooks, 'pre-commit'); + } +} // // Bail out if we don't have pre-commit file, it might be removed manually.