Skip to content

Bug when path of git project is not the same as path of npm project #132

@MKlingner

Description

@MKlingner

There is a bug in the install script when the gitRoot is not the same as the projectRoot. My Project looks like:

  • .git
  • npm_project_1
    • package.json
  • npm_project_2
    • package.json
    • <- executing npx simple-git-hooks here

result:

  • .git
  • npm_project_1
    • package.json
  • npm_project_2
    • .git
      • hooks
        • pre-commit
    • package.json

expectation:

  • .git
    • hooks
      • pre-commit
  • npm_project_1
    • package.json
  • npm_project_2
    • package.json

To fix this you should get the gitRoot in the _getHooksDirPath:

function _getHooksDirPath(projectRoot) {
    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,
            encoding: 'utf8'
        }).trim()

        if (!customHooksDirPath) {
            return defaultHooksDirPath
        }

        return path.isAbsolute(customHooksDirPath)
            ? customHooksDirPath
            : path.resolve(projectRoot, customHooksDirPath)
    } catch {
        return defaultHooksDirPath
    }
}

and skipping in _setHook and _removeHook

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

and the path to the npm project must be set into the hook call:
from

npm run git:pre-commit

to

npm run git:pre-commit --prefix ./npm_project_2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions