From b1aa91eeb72c5a798dd92bae2646ec9f4599ecb6 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Wed, 25 Apr 2018 13:00:49 +0000 Subject: [PATCH 1/3] Use git rev-parse --git-dir to find a git dir --- lib/git-hooks.js | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/lib/git-hooks.js b/lib/git-hooks.js index 1f98e63..2c720f7 100644 --- a/lib/git-hooks.js +++ b/lib/git-hooks.js @@ -1,6 +1,7 @@ var path = require('path'); var util = require('util'); var spawn = require('child_process').spawn; +var execSync = require('child_process').execSync; var fs = require('fs'); var fsHelpers = require('./fs-helpers'); @@ -165,27 +166,17 @@ function spawnHook(hookName, args) { } /** - * Returns the closest git directory. - * It starts looking from the current directory and does it up to the fs root. - * It returns undefined in case where the specified directory isn't found. + * Runs git rev-parse to find the git directory of the currentPath. * * @param {String} [currentPath] Current started path to search. * @returns {String|undefined} */ function getClosestGitPath(currentPath) { - currentPath = currentPath || process.cwd(); - - var dirnamePath = path.join(currentPath, '.git'); - - if (fsHelpers.exists(dirnamePath)) { - return dirnamePath; - } - - var nextPath = path.resolve(currentPath, '..'); - - if (nextPath === currentPath) { - return; + try { + var result = execSync('git rev-parse --git-dir', {cwd: currentPath}).toString(); + return result.replace(/\n/g, ''); + } catch (error) { + // No git dir? + return undefined; } - - return getClosestGitPath(nextPath); } From 3f0203a41a64f645bf559deb4f4901665aead1b6 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Wed, 25 Apr 2018 14:13:45 +0100 Subject: [PATCH 2/3] Exit code is 128 for no git dir --- lib/git-hooks.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/git-hooks.js b/lib/git-hooks.js index 2c720f7..325798b 100644 --- a/lib/git-hooks.js +++ b/lib/git-hooks.js @@ -176,7 +176,11 @@ function getClosestGitPath(currentPath) { var result = execSync('git rev-parse --git-dir', {cwd: currentPath}).toString(); return result.replace(/\n/g, ''); } catch (error) { - // No git dir? - return undefined; + if (error.status === 128) { + // No git dir? + return undefined; + } + // Throw any other errors + throw error; } } From 4b1ac7ca6dd87c24deb60613a56d435921a17154 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Wed, 25 Apr 2018 14:14:01 +0100 Subject: [PATCH 3/3] Always return full path to git dir --- lib/git-hooks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git-hooks.js b/lib/git-hooks.js index 325798b..aa5b9c2 100644 --- a/lib/git-hooks.js +++ b/lib/git-hooks.js @@ -173,7 +173,7 @@ function spawnHook(hookName, args) { */ function getClosestGitPath(currentPath) { try { - var result = execSync('git rev-parse --git-dir', {cwd: currentPath}).toString(); + var result = execSync('git rev-parse --absolute-git-dir', {cwd: currentPath}).toString(); return result.replace(/\n/g, ''); } catch (error) { if (error.status === 128) {