Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use git rev-parse --git-dir to find a git dir #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 12 additions & 17 deletions lib/git-hooks.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -165,27 +166,21 @@ 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 --absolute-git-dir', {cwd: currentPath}).toString();
return result.replace(/\n/g, '');
} catch (error) {
if (error.status === 128) {
// No git dir?
return undefined;
}
// Throw any other errors
throw error;
}

return getClosestGitPath(nextPath);
}