Skip to content
Open
Changes from 1 commit
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
33 changes: 25 additions & 8 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar
* Respects user-defined core.hooksPath from Git config if present;
* otherwise defaults to <gitRoot>/.git/hooks.
*
* @param {string} gitRoot - The absolute path to the Git project root
* @param {string} projectRoot - The absolute path to the project root
* @returns {string} - The resolved absolute path to the hooks directory
* @private
*/
function _getHooksDirPath(projectRoot) {
const defaultHooksDirPath = path.join(projectRoot, '.git', 'hooks')
const gitRoot = getGitProjectRoot(projectRoot)

if (!gitRoot) {
console.info('[INFO] No `.git` root folder found, skipping')
return
}

const defaultHooksDirPath = path.join(gitRoot, 'hooks')
try {
const customHooksDirPath = execSync('git config core.hooksPath', {
cwd: projectRoot,
Expand All @@ -222,15 +229,19 @@ function _getHooksDirPath(projectRoot) {
* @private
*/
function _setHook(hook, command, projectRoot=process.cwd()) {
const gitRoot = getGitProjectRoot(projectRoot)

if (!gitRoot) {
console.info('[INFO] No `.git` root folder found, skipping')
const hookDirectory = _getHooksDirPath(projectRoot)
if (!hookDirectory) {
console.info('[INFO] No hooks folder found, skipping')
return
}

const hookCommand = PREPEND_SCRIPT + command
const hookDirectory = _getHooksDirPath(projectRoot)
let finalCommand = command;
if(hookDirectory !== path.join(projectRoot, '.git', 'hooks')) {
finalCommand = `pushd . && cd ${projectRoot} && ${command} && popd`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking into it, i think the failing test comes from this and the test needs to be adapted? The test expects this command without changes in the resulting hook file, but finds it with this

other than that, the fix looks good to me

}
const hookCommand = PREPEND_SCRIPT + finalCommand

const hookPath = path.join(hookDirectory, hook)

const normalizedHookDirectory = path.normalize(hookDirectory)
Expand Down Expand Up @@ -272,6 +283,12 @@ async function removeHooks(projectRoot = process.cwd()) {
*/
function _removeHook(hook, projectRoot=process.cwd()) {
const hookDirectory = _getHooksDirPath(projectRoot)

if (!hookDirectory) {
console.info('[INFO] No hooks folder found, skipping')
return
}

const hookPath = path.join(hookDirectory, hook)

if (fs.existsSync(hookPath)) {
Expand Down
Loading